Presentation on theme: "1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level."— Presentation transcript:
5 Explanation The program will display the arguments (command-line arguments) that it is called with. The output is: args1 args2 args3
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 classSub class
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.
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.
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
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
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
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.
21 Example Library item (CityU) Book Video (VCD, DVD) Map Leaflet
22 Example – three classes It displays 1 It displays 2
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
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.
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.
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.
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