Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Object Oriented Programming - Lesson 6: Inheritance presents Inheritance (Principles, Inheritance hierarchy, Sub class definition, Order of initialization), Reusing through class, To provide new functionality to a subclass. | LESSON VI. Inheritance Trinh Thanh TRUNG (MSc) trungtt@soict.hust.edu.vn 094.666.8608 Content • Inheritance – Principles – Inheritance hierarchy – Sub class definition • extends – Order of initialization • super 1. Principles • Reusing through class: create a new class by extending the functionality of an existing class – The existing class is called the parent class, or super class, or base class – The new class is called the child class, or subclass, or derived class • Relation: New class is a kind of existing class – A subclass inherits the operations, attributes and hierarchical relationships of its super class • If a method is defined in a super class, all of its sub class inherit automatically this method • If a set of member variables are defined in a super class, all of its sub classes inherit the same set of member variables • To provide new functionality to a subclass, we can – Define new methods and variables for this subclass only – Override (execute instead of) methods of the super class in this subclass 4 Example Super class Media Defining basic operations on media products All the behaviors of super class (public attributes, operations, relationships) are inherited by the sub-class Sub class Book Basic operations on media products + Super class Media Specific behaviors on books 5 2. Class hierarchy • The class hierarchy defines the inheritance relationship between classes. – The root of the class hierarchy is the class Object. – Every class in Java directly or indirectly extends (inherits from) this class. • Direct super class: the super class from which a subclass explicitly inherits. – Java: a class can only have one direct super class (single inheritance) • Indirect super class: any class above the direct super class in the class hierarchy • The constructors and destructors are .