31
1 Inheritance Lecture 9 from Chapter 8

Inheritance

  • Upload
    yaron

  • View
    26

  • Download
    1

Embed Size (px)

DESCRIPTION

Inheritance. Lecture 9 from Chapter 8. Review. Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level hierarchy Calling the constructors Method overriding. Command line arguments. We can pass the values to a program. - PowerPoint PPT Presentation

Citation preview

Page 1: Inheritance

1

Inheritance

Lecture 9 from Chapter 8

Page 2: Inheritance

2

Review

Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level hierarchy Calling the constructors Method overriding

Page 3: Inheritance

3

Command line arguments

We can pass the values to a program. This is accomplished by passing command-line

arguments to main(). For example,

args[0] is 1, args[1] is 2 and args[2] is 3

Page 4: Inheritance

4

Example

Page 5: Inheritance

5

Explanation

The program will display the arguments (command-line arguments) that it is called with.

The output is: args[0] 1 args[1] 2 args[2] 3

Page 6: Inheritance

6

Inheritance – an introduction

Inheritance enables us to create a class that is similar to a previously defined class, but one that still has some of its own properties. Here, sub class is inherited from super class or super class extends to sub-class

Super class Sub class

Page 7: Inheritance

7

Inheritance -characteristics

Inheritance is one of the cornerstones of OOP. The other two are: encapsulation and polymorphism.

It allows the creation of hierarchical classification.

Using inheritance, we can create a general class that defines traits common to a set of related items.

This class can then be inherited by other.

Page 8: Inheritance

8

Example

Page 9: Inheritance

9

Explanation

The sub class B includes all of the members of its superclass A.

As a result, class B can access showa(). The sum() can be directly referred.

Super class Sub classExtend Extend

Page 10: Inheritance

10

Example – class B access class A

Page 11: Inheritance

11

Example - more

Page 12: Inheritance

12

Member Access and Inheritance

Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that was declared as private.

Page 13: Inheritance

13

Example – Explanation with private int j

Page 14: Inheritance

14

Using super

A subclass can call a constructor method defined by its superclass.

Super(parameter-list) Here, parameter-list specifies any

parameters needed by the constructor in the superclass.

Super() must always be the first statement executed inside a subclass’constructor.

Page 15: Inheritance

15

Example – super(s, i)

Page 16: Inheritance

16

Explanation

In SClass, there are three parameters, s, i and sexcode. Two parameters, namely, s and i are inherited from DClass

SClassuses Super(s, i). Here, the parameters are: string s and

integer i

Page 17: Inheritance

17

Super.member, page 202

The second from of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form:

super.memebr

Page 18: Inheritance

18

Example

Page 19: Inheritance

19

Explanation

Here, super.i = a will set the value in Class A

this.i will set the value in Class B We could use i instead of this.i

Page 20: Inheritance

20

Multi-level hierarchy

So far, we used two classes, superclass and subclass.

We can build hierarchies that contain more than two layers.

For example, we could define three classes, A, B and C.

C is a subclass of B which is a subclass of A.

Page 21: Inheritance

21

Example

Library item(CityU)

Book Video(VCD, DVD)

Map Leaflet

Page 22: Inheritance

22

Example – three classes

It displays 1

It displays 2

Page 23: Inheritance

23

Method Overriding

In a class hierarchy, 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 an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.

Subclass cannot see the method in the superclass

Page 24: Inheritance

24

Example

Page 25: Inheritance

25

Example – more

Page 26: Inheritance

26

Overload

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

When the two methods are not identical (the same names and types), it is called overload.

Page 27: Inheritance

27

Example

Page 28: Inheritance

28

Dynamic Method Dispatch

Method overriding forms the basis of Java’s most powerful concepts.

It is called dynamic method dispatch. Dynamic method dispatch is the

mechanism by which a call to an overridden method.

Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

Page 29: Inheritance

29

Example – dynamic dispatch

Page 30: Inheritance

30

Explanation

The above program creates one superclass, AClass and two sub-classes, BClass and CClass. A reference of type AClass called r is declared.

This program then assigns different reference to different class.

Page 31: Inheritance

31

Summary

Command line arguments – enter values from DOS prompt

Basic Inheritance – inherited from superclass Member access and inheritance Using super – similar to this, except for

superclass Creating a multi-level hierarchy – supports more

than one subclass Method overriding – override the method of same

names and type signature