Inheritance
description
Transcript of Inheritance
![Page 1: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/1.jpg)
1
Inheritance
Lecture 9 from Chapter 8
![Page 2: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/4.jpg)
4
Example
![Page 5: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/8.jpg)
8
Example
![Page 9: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/10.jpg)
10
Example – class B access class A
![Page 11: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/11.jpg)
11
Example - more
![Page 12: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/13.jpg)
13
Example – Explanation with private int j
![Page 14: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/15.jpg)
15
Example – super(s, i)
![Page 16: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/18.jpg)
18
Example
![Page 19: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/21.jpg)
21
Example
Library item(CityU)
Book Video(VCD, DVD)
Map Leaflet
![Page 22: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/22.jpg)
22
Example – three classes
It displays 1
It displays 2
![Page 23: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/24.jpg)
24
Example
![Page 25: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/25.jpg)
25
Example – more
![Page 26: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/27.jpg)
27
Example
![Page 28: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/29.jpg)
29
Example – dynamic dispatch
![Page 30: Inheritance](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062408/5681409d550346895dac4680/html5/thumbnails/31.jpg)
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