1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is...

Post on 19-Dec-2015

218 views 0 download

Tags:

transcript

1

Lecture 3

Inheritance

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

3

4

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

superclass that have been declared as private

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

6

Example (cont.)

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

Write down the structures of these three classes

7

A Superclass Variable Can Reference a Subclass Object

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

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; }

}

10

Complete Example

11

Complete Example(cont.)

12

Complete Example(cont.)

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 }

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

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

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++

17

Example

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); }}

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

20

Example

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

22

Example

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

24

Example

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)

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 // ...}

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)