1 / 31

Inheritance

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.

yaron
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Lecture 9 from Chapter 8

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

  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

  4. Example

  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

  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

  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.

  8. Example

  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. int k int i, j Super class Sub class Extend Extend class A class B

  10. Example – class B access class A

  11. Example - more here, Pet is inheritd from Cat

  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. X private int j;

  13. Example – Explanation with private int j

  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.

  15. Example – super(s, i) here, super(s,i) is inherited from DClass

  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

  18. Example

  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) Class A Book Video (VCD, DVD) Class B Map Leaflet Class C

  22. Example – three classes It displays 1 It displays 2

  23. 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.

  24. Example note: the methods, show() is identical

  25. Example – more here, class B will call show() in class B, not Class A

  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.

  27. Example

  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.

  29. Example – dynamic dispatch

  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

More Related