Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: computer science and mathematics
Description: Have well detailed notes on various on computer science with units such as data structures and algorithms, object oriented programming, databases, internet application programming, system designs... mathematics note like calculus 1,2,3,4 ; algebra, number theory, descrete maths, statistics etc

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Inheritance
A class can be a sub-type of another class
The inheriting class contains all the
methods and fields of the class it inherited
from plus any methods and fields it defines

Inheritance

The inheriting class can override the
definition of existing methods by providing
its own implementation
The code of the inheriting class consists
only of the changes and additions to the
base class
Version 2 - June 2008

Example
Class Employee{
string name;
double wage;
void incrementWage(){…}
}
Class Manager extends Employee{
string managedUnit;
void changeUnit(){…}
}
Manager m = new Manager();
m
...
In this way, there is
minimal repetition of the same code
Localization of code
Fixing a bug in the base class automatically fixes
it in the subclasses
Adding functionality in the base class
automatically adds it in the subclasses

A new design created by the
modification of an already existing
design
The new design consists of only the
changes or additions from the base
design

CoolPhoneBook inherits PhoneBook

Less chances of different (and inconsistent)
implementations of the same operation

Example of inheritance tree

Add mail address and cell number

Inheritance terminology
Class one above

Living species

Parent class

Animal

vegetal

Child class

Human being
Flower
Professor
SalesMan
Student
Travel Agent

Class one below
Class one or more above
Superclass, Ancestor class, Base class

Class one or more below

Subclass, Descendent class

Inheritance and polymorphism

Inheritance and polymorphism

Class Employee{
private string name;

Employee e1 = new Employee();

Employee e1 = new Employee();

Employee e2 = new Manager();
public void print(){
System
...
println(name); e1
...
print(); // name and unit

Employee e2 = new Manager(); //ok, is_a
e1
...
print(); // name and unit

Class Manager extends Employee{
private string managedUnit;
public void print(){ //overrides
System
...
println(name); //un-optimized!
System
...
println(managedUnit);
}
}

Inheritance in few words

Inheritance in Java: extends
class Car {
class Car {

Subclass

Car

Inherits attributes and methods
Can modify inherited attributes and
methods (override)
Can add new attributes and methods

color
isOn
licencePlate
turnOn
paint

ElectricCar
cellsAreCharged

String color;
String color;
boolean isOn;
boolean isOn;
String licencePlate;
String licencePlate;
void paint(String color) {
void paint(String color) {
this
...
color = color;
}
}
class ElectricCar extends Car
class ElectricCar extends Car
void turnOn() { {
void turnOn() { {
isOn=true;
isOn=true;
boolean cellsAreCharged;
}
boolean cellsAreCharged;
}
}
void recharge() {
}
void recharge() {
cellsAreCharged = true;
cellsAreCharged = true;
}
}

recharge
turnOn

11

}
}

void turnOn() {
void turnOn() {
if(cellsAreCharged )
if(cellsAreCharged )
isOn=true;
isOn=true;
}
}
12

Inheritance in Java: extends

ElectricCar

class Car {
class Car {
Car
color
isOn
licencePlate
turnOn
paint

ElectricCar
cellsAreCharged

Inherits

String color;
String color;
boolean isOn;
boolean isOn;
String licencePlate;
String licencePlate;

attributes (color, isOn, licencePlate)
methods (paint)

void paint(String color) {
void paint(String color) {
this
...
color = color;
}
}
class ElectricCar extends Car
class ElectricCar extends Car
void turnOn() { {
void turnOn() { {
isOn=true;
isOn=true;
boolean cellsAreCharged;
boolean cellsAreCharged;
}
}
}
void recharge() {
}
void recharge() {
cellsAreCharged = true;
cellsAreCharged = true;
}
}

recharge
turnOn

}
}

Modifies (overrides)
turnOn()

Adds

attributes (cellsAreCharged)
Methods (recharge)

void turnOn() {
void turnOn() {
if(cellsAreCharged )
if(cellsAreCharged )
isOn=true;
isOn=true;
}
}
13

14

Example

Visibility (scope)

class Employee {
private String name;
private double wage;
}
class Manager extends Employee {
void print() {
System
...
println(“Manager” +
name + “ ” + wage);
}
}
Not visible
16

Protected

In summary

Attributes and methods marked as

Method of
another
Method in
the same class in the
same
class
package

public are always accessible
private are accessible within the class
only
protected are accessible within the class
and its subclasses

Method
of
subclass

Method of
another
public class
in the
outside
world

private
package
protected
public
17

Super (reference)

Example
class Car {
class Car {

“this” is a reference to the current
object

Car
color
isOn
licencePlate

“super” is a reference to the parent
class

turnOn
paint

ElectricCar
cellsAreCharged
recharge
turnOn

19

String color;
String color;
boolean isOn;
boolean isOn;
String licencePlate;
String licencePlate;
void paint(String color) {
void paint(String color) {
this
...
color = color;
}
}
class ElectricCar extends Car{
class ElectricCar extends Car{
void turnOn() {
void turnOn() {
boolean cellsAreCharged;
boolean cellsAreCharged;
isOn=true;
isOn=true;
}
}
void recharge() {
void recharge() {
}
}
cellsAreCharged = true;
cellsAreCharged = true;
}
}

was

if(cellsAreCharged)
isOn = true;
}
}

void turnOn() {
void turnOn() {
if( cellsAreCharged )
if( cellsAreCharged )
super
...
turnOn();
}
}
20

Attributes redefinition
Class Parent{
protected int attr = 7;
}
Class Child{
protected String attr = “hello”;

Inheritance and
constructors

void print(){
System
...
println(super
...
out
...
print();
}
}

21

Construction of child objects

Construction of child objects

Since each object “contains” an
instance of the parent class, the latter
must be initialized

Execution of constructors proceeds
top-down in the inheritance hierarchy

Java compiler automatically inserts a
call to default constructor (no params)
of parent class

In this way, when a method of the
child class is executed (constructor
included), the super-class is
completely initialized already

The call is inserted as the first
statement of each child constructor

23

24

Example

Example (cont’d)

class ArtWork {
class ArtWork {
ArtWork() {
ArtWork() {

Cartoon obj = new Cartoon();

System
...
println(“New ArtWork”); }
System
...
println(“New ArtWork”); }

}
}
class Drawing extends ArtWork {
class Drawing extends ArtWork {
Drawing() {
Drawing() {

new
new
new
new
new
new

System
...
println(“New Drawing”); }
System
...
println(“New Drawing”); }

}
}

ArtWork
ArtWork
Drawing
Drawing
Cartoon
Cartoon

class Cartoon extends Drawing {
class Cartoon extends Drawing {
Cartoon() {
Cartoon() {
System
...
println(“New Cartoon”); }
System
...
println(“New Cartoon”); }

}
}
25

A word of advice

26

Super
If you define custom constructors
with arguments

Default constructor “disappears” if
custom constructors are defined

and default constructor is not defined
explicitly

class Parent{
class Parent{
Parent(int i){}
Parent(int i){}
}
}
class Child extends Parent{ }
class Child extends Parent{ }
// error!
// error!
class Parent{
class Parent{
Parent(int i){}
Parent(int i){}
Parent(){} //explicit default
Parent(){} //explicit default
}
}

the compiler cannot insert the call
automatically

class Child extends Parent { }
class Child extends Parent { }
// ok!
// ok!
27

28

Super

Example

Child class constructor must call the
right constructor of the parent class,
explicitly

class Employee {
class Employee {
private String name;
private String name;
private double wage;
private double wage;
???
???
Employee(String n, double w){
Employee(String n, double w){
name = n;
name = n;
wage = w;
wage = w;
}}
class Manager extends Employee {
class Manager extends Employee {
}
private int unit;
}
private int unit;

Use super() to identify constructors of
parent class

Manager(String n,
Manager(String n,
super(); ERROR
super(); ERROR
unit = u;
unit = u;
}
}

First statement in child constructors

double w, int u) {
double w, int u) {
!!!
!!!

}
}
29

30

Example
class Employee {
class Employee {
private String name;
private String name;
private double wage;
private double wage;

}
}

Employee(String
Employee(String
name = n;
name = n;
wage = w;
wage = w;
}}

Dynamic binding/
polymorphism

n, double w){
n, double w){

class Manager
class Manager
private int
private int

extends Employee {
extends Employee {
unit;
unit;

Manager(String n, double w, int u) {
Manager(String n, double w, int u) {
super(n,w);
super(n,w);
unit = u;
unit = u;
}
}
}
}
31

Example

Binding
Association message/method

Car[] garage = new Car[4];

Constraint

garage[0] = new Car();

Car

Same signature

garage[1] = new ElectricCar();
garage[2] = new ElectricCar();

color
isOn
licencePlate

Car a;
for(int i=0; i ...
turnOn();
}

garage[3] = new Car();
for(int i=0; i ...
turnOn();
}

turnOn
paint

ElectricCar
cellsAreCharged

message

method

recharge
turnOn

33

34

Java Object
java
...
Object
All classes are subtypes of Object

Object

Object

extends Object
Vertebrate
hasSpine

Bird
canFly

Implicitly

class Vertebrate {
class Vertebrate {


}
}
class Bird extends Vertebrate{
class Bird extends Vertebrate{


}
}
36

Java Object

Java Object
Each instance can be seen as an Object
instance (see Collection)

toString()
Returns a string
uniquely identifying
the object

Object defines some services, which are
useful for all classes
Often, they are overridden in sub-classes

equals()
Tests equality of
values

Object
toString() : String
equals(Object) : boolean

37

print methods implicitly invoke
toString() on all object parameters
class Car{ String toString(){…} }
System
...
print(c);

Casting

// same as
...
System
...
print(c
...
out
...
out
...
e
...
7; // legal
f = “string”; // illegal
Car c;
c = new Car(); // legal
c = new String(); // illegal

class Car{};
class ElectricCar extends Car{};
Car c = new Car();
ElectricCar ec = new ElectricCar ();

41

Specialization - 2

42

Specialization - 3

New case…

Legal!

Specialization defines a sub-typing
relationship (is a)
ElectricCar type is a subset of Car type

class Car{};
class ElectricCar extends Car{};
Car a = new ElectricCar (); // legal??

Car

Car(s)

ElectricCar(s)

ElectricCar

All electric cars are cars too
43

44

Cast

Upcast
Assignment from a more specific type
(subtype) to a more general type (supertype)
class Car{};
class ElectricCar extends Car{};
Car c = new ElectricCar ();

Type conversion (explicit or
implicit)
int i = 44;
float f = i;
// implicit cast 2c -> fp

Note well - reference type and object type
are separate concepts
Object referenced by ‘c’ continues to be of
ElectricCar type

f = (float) 44;
// explicit cast
45

Upcast

Downcast
Assignment from a more general type
(super-type) to a more specific type
(sub-type)

It is dependable

It is always true that an electric car is a car too

It is automatic

As above, reference type and object type
do not change

Car c = new Car();
ElectricCar ec = new ElectricCar ();
c = ec;

MUST be explicit

It’s a risky operation, no automatic
conversion provided by the compiler (it’s
up to you!)

ec

c
Up-casting:
Object type does NOT change

46

3

2

1

Car

ElectricCar
47

48

Downcast - Example I

Dowcast – Example II

Car c = new ElectricCar(); // impl
...
upcast

Car c = new Car();
Car c = new Car();

c
...
recharge(); // wrong!

c
...
rechage(); // wrong!

// explicit
// explicit
ElectricCar
ElectricCar

// explicit
// explicit
ElectricCar
ElectricCar

downcast
downcast
ec = (ElectricCar)c;
ec = (ElectricCar)c;

ec
...
recharge(); // ok

Run-time error

downcast
downcast
ec = (ElectricCar)c;
ec = (ElectricCar)c;

ec
...
recharge(); // wrong!

YOU know they are compatible types
(compiler trusts you)

YOU might be wrong (risk)
49

Visually

50

Messy example

All electric cars are cars too

ec2

Car c, cc;
ElectricCar ec, ec2;

Not all cars are electric cars are too

c

Car

downcast

ec = new ElectricCar (); // 2

ElectricCar(s)

ec
...
recharge(); // NO

Car(s)

ec

cc

c = new Car (); // 1

Car

c= ec; // 4 Upcasting
c
...
recharge();

// OK

5

3

ec2 = (ElectricCar ) cc ;
ec2
...
recharge();
}

Car

was
((ElectricCar)c)
...
color = color;
this
...
equals(s);
return licencePlate
...
equals(s);
return name
...
length; i++)
for(int i=0; i< objects
...
isEqual(s)
if(objects[i]
...

interfaces
}
65

66

Homework

A word of advice

Defining a class that contains abstract
methods only is not illegal

See the doc of java
...
Comparable

You should use interfaces instead

public interface Comparable{
int compareTo(Object obj);
}

Overriding methods in subclasses can
maintain or extend the visibility of
overridden superclass’s methods

e
...
protected int m() can’t be overridden by
– private int m()
– int m()

Only protected or public are allowed
67

Returns a negative integer, 0, or a
positive integer as this object is less
than, equal, or greater than obj
68

Homework (cont’d)

Homework (cont’d)

Define Employee, which implements
Comparable (order by ID)

public static void main(String args[]){
int size = 3; // array size
OrderedArray oa = new OrderedArray(size);

Define OrderedArray class

oa
...
add( new Employee(“Andrew”, 12345) );
oa
...
print();

Test it with the following main

}

Name

97563) );
ID

69

Wrap-up session
Inheritance

Objects defined as sub-types of already existing
objects
...
g
Title: computer science and mathematics
Description: Have well detailed notes on various on computer science with units such as data structures and algorithms, object oriented programming, databases, internet application programming, system designs... mathematics note like calculus 1,2,3,4 ; algebra, number theory, descrete maths, statistics etc