1 lecture 3 inheritance. 2 a class that is inherited is called superclass the class that inherits is...

27
1 Lecture 3 Inheritance

Post on 19-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

1

Lecture 3

Inheritance

Page 2: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

2

Inheritance

A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version of a superclass extends keyword is used to inherit superclass Java does not support multiple superclasses into

single subclass (This differs from C++) A subclass can be a superclass of another

subclass So class can be a superclass of itself

Page 3: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

3

Page 4: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

4

Member access and Inheritance A subclass cannot access those members of the

superclass that have been declared as private

Page 5: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

5

Example A Box class has three instance variables width,

height and depth and one member method volume. Also it has different formats of constructor methods to initialize the instance variables, such as Through object parameter Through parameters w, h and d No parameter Single parameter length of each side

A BoxWeight class that has all the properties of Box except another instance variable weight. To initialize this variable, its needs constructor

Page 6: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

6

Example (cont.)

Another class ColorBox hass all the properties of Box except another property color

Write down the structures of these three classes

Page 7: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

7

A Superclass Variable Can Reference a Subclass Object

Page 8: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

8

Using super If a superclass keeps its data members private,

then there would be no way for subclass to directly access or initialize its parents instance variables

The solution is the use of keyword super Whenever a subclass needs to refer to its

immediate superclass, it can do so by use of the keyword super

Super has two general forms, For accessing constructors For accessing hidden member of superclass

Page 9: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

9

Using super to call superclass constructors

super (parameter-list) super( ) must always be the first statement executed

inside a subclass’ constructor

// BoxWeight now uses super to initialize its Box attributes.class BoxWeight extends Box { double weight; // weight of box

// initialize width, height, and depth using super() BoxWeight(double w, double h, double d, double m) { super(w, h, d); // call superclass constructor weight = m; }

}

Page 10: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

10

Complete Example

Page 11: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

11

Complete Example(cont.)

Page 12: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

12

Complete Example(cont.)

Page 13: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

13

A second use for super Here super acts as somewhat like this

super.member Mostly applicable when member names of a subclass hide

members by the same name in the superclass

// Using super to overcome name hiding.class A { int i;}// Create a subclass by extending class A.class B extends A { int i; // this i hides the i in A

B(int a, int b) { super.i = a; // i in A i = b; // i in B }

Page 14: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

14

Creating a Multilevel Hierarchy

• super always refers to the constructor in the closest superclass

•These 3 classes are normally places in three files and compiled separately

Page 15: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

15

When constructor are called?

The output of the program is:

Inside’s A’s constructors

Inside’s B’s constructors

Inside’s C’s constructors

Page 16: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

16

Method Overriding

When a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass

When a overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass

Like virtual functions in C++

Page 17: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

17

Example

Page 18: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

18

Accessing superclass version of overridden function

class B extends A { int k;

B(int a, int b, int c) { super(a, b); k = c; }

void show() { super.show( ); // this calls A's show() System.out.println("k: " + k); }}

Page 19: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

19

Difference between overriding and overloading

Method overriding occurs only when the names and the type signatures of the two methods are identical

If they are not, then two methods are simply overloaded

Page 20: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

20

Example

Page 21: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

21

Dynamic Method Dispatch

Mechanism by which a call to an overridden function is resolved at run time, rather than compile time (run-time polymorphism)

Uses the principle: a superclass reference variable can refer to a subclass object

It is the type of the object being referred to (not the type of of the reference variable) that determines which version of an overridden method will be executed

Page 22: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

22

Example

Page 23: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

23

Using Abstract Classes Sometimes a superclass is unable to create a

meaningful implementation for a method(i.e. Figure )

In that case, a superclass declares the structure (generalized form) of a given abstraction without providing a complete implementation of every method (left for subclass to fill it)

No body in the methodabstract type-name (parameter-list);

Any class that contains one or more abstract methods must also be declared abstract

There can be no objects of an abstract class

Page 24: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

24

Example

Page 25: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

25

Using final to Prevent Overriding Methods declared as final cannot be overridden

class A { final void meth() { System.out.println("This is a final method."); }}class B extends A { void meth() { // ERROR! Can't override. System.out.println("Illegal!"); }}

Normally Java resolves calls to methods dynamically, at run time (late binding)

Since, final methods cannot be overridden, a call to one can be resolved at compile time (early binding)

Page 26: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

26

Using final to Prevent Inheritance final keyword before a class declaration prevents it

from being inherited Declaring a class as final implicitly declares all of its

methods as finalfinal class A { // ...}

// The following class is illegal.class B extends A { // ERROR! Can't subclass A // ...}

Page 27: 1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version

27

The Object Class Object class is a special type of class defined by Java All other classes are subclasses of Object A reference variable of type object can refer to an

object of any other class Objects defines the following methods:

Object clone( ) boolean equals (Object object) void finalize( ) Class getClass( ) String to String( ) Void wait(long milliseconds)