Implementing Inheritance – Object-Oriented Programming
5.1 Implementing Inheritance Inheritance is one of the fundamental mechanisms for code reuse in OOP. It allows new classes to be derived from existing ones. The new class (also called a subclass, subtype, derived class, or child class) can inherit members from the old class (also called a superclass, supertype, base class, or parent class). The subclass can add new behavior and properties and, under certain circumstances, modify its inherited behavior. A subclass specifies the name of its superclass in the subclass header using the extends clause. Click here to view code image class TubeLight extends Light { … } // TubeLight is a subclass of Light. The subclass specifies only the additional new and modified members in its class body. The rest of its declaration is made up of its inherited members. If no extends clause is specified in the header of a class declaration, the class implicitly inherits…
The Subtype–Supertype Relationship – Object-Oriented Programming
The Subtype–Supertype Relationship A class defines a reference type, a data type whose objects can be accessed only by references. Therefore, the inheritance hierarchy can be regarded as a type hierarchy, embodying the subtype–supertype relationship between reference types. The subclass– superclass relationship is a special case of the subtype–supertype relationship that is between classes. The subclass–superclass relationship allows single inheritance of type, meaning that the subclass inherits the type of its direct superclass. This is in contrast to a class that implements several interfaces, resulting in multiple inheritance of type—that is, a class inherits the type of all interfaces it implements. In the context of Java, the subtype–supertype relationship implies that the reference value of a subtype object can be assigned to a supertype reference because a subtype object can be substituted for a supertype object. This assignment involves a widening reference conversion, as references are assigned up the inheritance…