object-oriented programming more inheritance · object-oriented programming more inheritance ewan...

45
. . . . . . . . . Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 1 / 45

Upload: vothien

Post on 27-Jul-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

.

.

. ..

.

.

Object-Oriented ProgrammingMore Inheritance

Ewan Klein

School of Informatics

Inf1 :: 2009/10

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 1 / 45

Page 2: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

.. .1 Inheritance

Flat HierarchyLayered Hierarchy

.. .2 Polymorphism

.. .3 Overriding and Overloading

.. .4 Abstract Classes

.. .5 Recap

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 2 / 45

Page 3: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Flat Animal Hierarchy

sleep()makeNoise()roam()

Animal

makeNoise()

Lion

makeNoise()

Cat makeNoise()

Wolf

makeNoise()

Dog

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 3 / 45

Page 4: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 1

Our base class: Animal.Animal..

.

. ..

.

.

public class Animal {public void sleep() {

System.out.println(”Sleeping: Zzzzz”);}public void makeNoise() {

System.out.println(”Noises...”);}public void roam() {

System.out.println(”Roamin’ on the plain.”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 4 / 45

Page 5: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 2

...1 Lion IS-A Animal

...2 Override the makeNoise() method.

.Lion..

.

. ..

.

.

public class Lion extends Animal {public void makeNoise() {

System.out.println(”Roaring: Rrrrrr!”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 5 / 45

Page 6: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 3

...1 Cat IS-A Animal

...2 Override the makeNoise() method.

.Cat..

.

. ..

.

.

public class Cat extends Animal {public void makeNoise() {

System.out.println(”Miaowing: Miaooo!”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 6 / 45

Page 7: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 4

...1 Wolf IS-A Animal

...2 Override the makeNoise() method.

.Wolf..

.

. ..

.

.

public class Wolf extends Animal {public void makeNoise() {

System.out.println(”Howling: Ouooooo!”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 7 / 45

Page 8: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 5

...1 Dog IS-A Animal

...2 Override the makeNoise() method.

.Dog

..

.

. ..

.

.

public class Dog extends Animal {public void makeNoise() {

System.out.println(”Barking: Woof Woof!”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 8 / 45

Page 9: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 6.The Launcher..

.

. ..

.

.

public class AnimalLauncher {public static void main(String[] args) {

System.out.println(”\nWolf\n=====”);Wolf wolfie = new Wolf();wolfie.makeNoise() ; // from Wolfwolfie.roam(); // from Animalwolfie.sleep(); // from Animal

System.out.println(”\nLion\n=====”);Lion leo = new Lion();leo.makeNoise(); // from Lionleo.roam(); // from Animalleo.sleep(); // from Animal

}}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 9 / 45

Page 10: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 7

.Output..

.

. ..

.

.

Wolf=====Howling: Ouooooo!Roamin’ on the plain.Sleeping: Zzzzz

Lion=====Roaring: Rrrrrr!Roamin’ on the plain.Sleeping: Zzzzz

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 10 / 45

Page 11: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Nested Animal Hierarchy

Lions and cats can be grouped together into Felines, with common roam()behaviours.

Dogs and wolves can be grouped together into Canines, with commonroam() behaviours.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 11 / 45

Page 12: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Nested Animal Hierarchy

sleep()makeNoise()roam()

Animal

makeNoise()

LionmakeNoise()

Cat

makeNoise()

Wolf

makeNoise()

Dog

roam()

Feline

roam()

Canine

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 12 / 45

Page 13: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 1

Same as before.

.Animal..

.

. ..

.

.

public class Animal {public void sleep() {

System.out.println(”Sleeping: Zzzzz”);}public void makeNoise() {

System.out.println(”Noises...”);}public void roam() {

System.out.println(”Roamin’ on the plain.”);}

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 13 / 45

Page 14: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 2

The new class Feline

.Feline..

.

. ..

.

.

public class Feline extends Animal {public void roam() {

// Override roam()System.out.println(”Roaming: I’m roaming alone.”);

}}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 14 / 45

Page 15: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 3

The new class Canine

.Canine..

.

. ..

.

.

public class Canine extends Animal {public void roam() {

// Override roam()System.out.println(”Roaming: I’m with my pack.”);

}}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 15 / 45

Page 16: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 4

...1 Lion IS-A Feline

...2 Override the makeNoise() method.

.Lion..

.

. ..

.

.

public class Lion extends Feline {public void makeNoise() {

System.out.println(”Roaring: Rrrrrr!”);}

}

Similarly for Cat.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 16 / 45

Page 17: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 5

...1 Wolf IS-A Canine

...2 Override the makeNoise() method.

.Wolf..

.

. ..

.

.

public class Wolf extends Canine {public void makeNoise() {

System.out.println(”Howling: Ouooooo!”);}

}

Similarly for Dog.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 17 / 45

Page 18: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Which method gets called?

sleep()makeNoise()roam()

Animal

makeNoise()

Wolf

roam()

Canine

1. Wolf wolfie = new Wolf();

2. wolfie.makeNoise(); 3. wolfie.roam();

4. wolfie.sleep();

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 18 / 45

Page 19: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 6.The Launcher..

.

. ..

.

.

public class AnimalLauncher {public static void main(String[] args) {

System.out.println(”\nWolf\n=====”);Wolf wolfie = new Wolf();wolfie.makeNoise(); // from Wolfwolfie.roam(); // from Caninewolfie.sleep(); // from Animal

System.out.println(”\nLion\n=====”);Lion leo = new Lion();leo.makeNoise(); // from Lionleo.roam(); // from Felineleo.sleep(); // from Animal

}}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 19 / 45

Page 20: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animals Example, 7

.Output..

.

. ..

.

.

Wolf=====Howling: Ouooooo!Roaming: I’m with my pack.Sleeping: Zzzzz

Lion=====Roaring: Rrrrrr!Roaming: I’m roaming alone.Sleeping: Zzzzz

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 20 / 45

Page 21: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Typing and Polymorphism

polymorphism (= ‘many shapes’): the same piece of code can be assignedmultiple types.

A class defines a type, namely the signatures of its methods.

S is a subtype of T, written S <: T, if a value of type S can be used in anycontext where a value of type T is expected.

The relation <: is reflexive: T <: TThe relation <: is transitive: if S <: T and T <: U, then S <: U.

NB: We say T is a supertype of S if S is a subtype of T.

Inclusion polymorphism: objects of different types S1, S2, …may be treateduniformly as instances of a common supertype T.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 21 / 45

Page 22: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Declaring and Initializing a Reference Variable

Wolf wolfie = new Wolf();

create a Wolf object

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 22 / 45

Page 23: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Declaring and Initializing a Reference Variable

Wolf wolfie = new Wolf();

declare a reference variable

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 23 / 45

Page 24: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Declaring and Initializing a Reference Variable

Wolf wolfie = new Wolf();

link the object to the reference

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 24 / 45

Page 25: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Declaring and Initializing a Reference Variable

Animal wolfie = new Wolf();

supertypeobject of subtype

Reference type can be supertype of the object type.

E.g., Wolf <: Animal.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 25 / 45

Page 26: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Polymorphic ArrayList

.The Launcher..

.

. ..

.

.

public class AnimalLauncher2 {public static void main(String[] args) {

Wolf wolfie = new Wolf();Lion leo = new Lion();Cat felix = new Cat();Dog rover = new Dog();ArrayList< Animal > animals = new ArrayList<Animal>();animals.add(wolfie);animals.add(leo);animals.add(felix);animals.add(rover);for ( Animal a : animals) {

a.makeNoise();}

}}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 26 / 45

Page 27: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Polymorphic Arrays

ArrayList<Animal> is polymorphic.

animals.add(wolfie)add an object of type Wolf. OK since Wolf <: Animal.

for (Animal a : animals)for each object a of type T such that T <: Animal …

a.makeNoise()if a is of type T, use T’s makeNoise() method.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 27 / 45

Page 28: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Overriding, 1

If a class C overrides a method m of superclass D, then:

Parameter lists must be same and return type must be compatible:...1 signature of m in C must be same as signature of m in D; i.e. same name, same

parameter list, and...2 return type S of m in C must such that S <: T, where T is return type of m in D.

m must be at least as accessible in C as m is in D

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 28 / 45

Page 29: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Overriding, 2

.method in Animal..

.

. ..

.

.

public void makeNoise() {...}

.Wrong: method in Wolf..

.

. ..

.

.

public void makeNoise( int volume ) {...}

.Wrong: method in Wolf..

.

. ..

.

.

private void makeNoise() {...}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 29 / 45

Page 30: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Overloading, 1

Overloading: two methods with same name but different parameter lists..Overloaded makeNoise..

.

. ..

.

.

public void makeNoise() {...}public void makeNoise(int volume) {...}

.Overloaded println..

.

. ..

.

.

System.out.println(3); // intSystem.out.println(3.0); // doubleSystem.out.println((float) 3.0); // cast to floatSystem.out.println(”3.0”); // String

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 30 / 45

Page 31: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Overloading, 2

...1 Return types can be different.

...2 You can’t just change the return type — gets treated as an invalid override.

...3 Access levels can be varied up or down.

.Incorrect override of makeNoise..

.

. ..

.

.

public String makeNoise() {String howl = ”Ouooooo!”;return howl;}Exception in thread ”main” java.lang.Error: Unresolved compila-tion problem:

The return type is incompatible with Animal.makeNoise()

at week06.Wolf.makeNoise(Wolf.java:15)at week06.AnimalLauncher.main(AnimalLauncher.java:11)

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 31 / 45

Page 32: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Animal Objects?

.Creating new objects..

.

. ..

.

.

Wolf wolfie = new Wolf();

Animal leo = new Lion();

Animal weird = new Animal();

Animal class is meant to contain information that all animals have incommon.

But this is not enough to define any one specific animal.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 32 / 45

Page 33: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Concrete vs. Abstract

Concrete

Examples: Cat, WolfSpecific enough to beinstantiated.

Abstract

Examples: Animal, FelineNot intended to haveinstances.

Only useful if extended.

Any ’instances’ will have tobe instances of a subclass ofthe abstract class.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 33 / 45

Page 34: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

The Abstract Animal, 1

.Animal..

.

. ..

.

.

abstract class Animal {public void sleep() {

System.out.println(”Sleeping: Zzzzz”);}public void makeNoise() {

System.out.println(”Noises...”);}public void roam() {

System.out.println(”Roamin’ on the plain.”);}

}

Just put the keyword abstract before the class declaration.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 34 / 45

Page 35: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

The Abstract Animal, 2

An abstract class can be extended by other abstract classes.Canine and Feline can (and should) both be abstract.

.Animal..

.

. ..

.

.

abstract class Animal {public void sleep() {

System.out.println(”Sleeping: Zzzzz”);}public void makeNoise() {

System.out.println(”Noises...”);}public void roam() {

System.out.println(”Roamin’ on the plain.”);}

}

Just put the keyword abstract before the class declaration.Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 35 / 45

Page 36: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

The Abstract Animal, 2

.Animal..

.

. ..

.

.

abstract class Animal {public void sleep() {

System.out.println(””Sleeping: Zzzzz””);public abstract void roam();public abstract void makeNoise();}

}

Now has abstract methods!

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 36 / 45

Page 37: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

The Abstract Animal, 3

roam() and makeNoise() are abstract methods (we’ve already seen these ininterfaces):

I no body;I must be implemented in any concrete subclass (implemented ∼ overriden);I don’t have to be implemented by an abstract subclass;I can only be declared in an abstract class;

sleep() is not abstract, so can be straightforwardly inherited.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 37 / 45

Page 38: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Abstract Classes in Animal Hierarchy

sleep()makeNoise()roam()

<<Abstract>>

Animal

makeNoise()

LionmakeNoise()

Cat

makeNoise()

Wolf

makeNoise()

Dog

roam()

<<Abstract>>

Feline

roam()

<<Abstract>>

Canine

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 38 / 45

Page 39: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Using Abstract Classes

Use an abstract class when you have several similar classes that:I have a lot in common — the implemented parts of the abstract classI have some differences — the abstract methods.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 39 / 45

Page 40: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Digression: Constructor Chaining

Constructor of the immediate superclass invoked with super()If you don’t explicitly call this, the compiler will.

Case 1 No constructor explicitly declared. Compiler inserts

public Foo() {super();

}Case 2 You have already declared one or more constructors. Compiler

inserts the following as first statement in each one:

super();

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 40 / 45

Page 41: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Recap: Implementing/Overriding, 1

If a class C implements or overrides a method m of superclass D, then:

Parameter lists must be same and return type must be compatible:...1 signature of m in C must be same as signature of m in D; i.e. same name, same

parameter list, and...2 return type S of m in C must such that S <: T, where T is return type of m in D.

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 41 / 45

Page 42: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Implementation, 1

.Abstract method in Animal.... ..

.

.public abstract void makeNoise();

.Wrong: method in Wolf..

.

. ..

.

.

public void makeNoise( int volume ) {...}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 42 / 45

Page 43: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Overloading

Overloading: two methods with same name but different parameter lists..Overloaded makeNoise..

.

. ..

.

.

public void makeNoise(int volume) {...}public void makeNoise() {

makeNoise(int 1);}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 43 / 45

Page 44: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Implementation, 2

You cannot implement or override a method by providing just an overloadedmethod..Declaration of sleep() in Animal..

.

. ..

.

.

public void sleep() {System.out.println(”Sleeping: Zzzzz”);

}

.Right: overriding sleep() in Wolf..

.

. ..

.

.

public void sleep() {System.out.println(”Sleeping: XXXXX”);

}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 44 / 45

Page 45: Object-Oriented Programming More Inheritance · Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics) OOP:

. . . . . .

Method Implementation, 3

.Wrong: overloading sleep() in Wolf..

.

. ..

.

.

public void sleep(int duration) {String sleepNoise = ”Sleeping: Zzzzz”;if (duration > 2)

sleepNoise = ”Still Sleeping: XXXXX”;System.out.println(sleepNoise);

}

.Right: overloading and overriding..

.

. ..

.

.

public void sleep(int duration) {...

}public void sleep() {

System.out.println(”Sleeping: XXXXX”);}

Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 45 / 45