310 likes | 476 Views
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.
E N D
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. • 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
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
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
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.
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. int k int i, j Super class Sub class Extend Extend class A class B
Example - more here, Pet is inheritd from Cat
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. X private int j;
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.
Example – super(s, i) here, super(s,i) is inherited from DClass
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
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
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
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.
Example Library item (CityU) Class A Book Video (VCD, DVD) Class B Map Leaflet Class C
Example – three classes It displays 1 It displays 2
Method Overriding • Subclass cannot see the method in the superclass • 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.
Example note: the methods, show() is identical
Example – more here, class B will call show() in class B, not Class A
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.
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.
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.
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