OOP - Inheritance
In this section, we study Inheritance.
Introduction to Inheritance
Examples
Inheritance and Data Members
Inheritance and Constructors
Method Overriding
Object Class
Inheritance
It is a form of software reuse.New classes are created by extending an existing class’s features (data members and methods) and adding new features.
Basically, new class inherits the members from an existing class.
This existing class is called the superclass (base class in C++).
The new class is called subclass (derived class in C++).
The superclass is more general and defines the common features.
The subclass is more specific by adding its own specific features.
The subclass exhibits the behaviors of its superclass with additional behaviors.
The subclass can become the superclass for future subclass.
The inheritance hierarchy can have several levels:
Direct superclass (one level) and indirect superclass (more levels).
In Java, the class hierarchy begins with class Object (java.lang.Object).
Java does not support multiple inheritance.
“is-a” represents the inheritance relationship between the superclass and subclass.
Every subclass object “is an” object of its superclass, one superclass can have many subclasses.
Superclass: Vehicle
Subclass: Car, Truck, and Bus
A car is a vehicle.
Subclass: Car, Truck, and Bus
A car is a vehicle.
Examples
Quadrilateral – Rectangle
Class Rectangle inherits from class Quadrilateral.
A rectangle is a quadrilateral. A rectangle is a specific type of quadrilateral.
It is incorrect to claim that every quadrilateral is a rectangle.
A rectangle is a quadrilateral. A rectangle is a specific type of quadrilateral.
It is incorrect to claim that every quadrilateral is a rectangle.
public class Rectangle extends Quadrilateral {}
University Member: Employee, Student, Alumni
Employee: Faculty, Staff
A university community members includes employees, students, and alumni.
Employees includes faculty and staff.
An employee is a member.
A student is a member.
A faculty is an employee.
Employees includes faculty and staff.
An employee is a member.
A student is a member.
A faculty is an employee.
public class Employee extends Member {}
public class Faculty extends Employee {}
public class Faculty extends Employee {}
Inheritance and Data Members
Superclass' private data members are indeed inherited by subclasses, but not directly accessible by subclasses.Superclass' public data members are inherited and directly accessible by subclasses.
However, it is considered as bad programming practice to use public data members.
Superclass' protected data members are inherited and directly accessible by subclasses and classes in the same package.
It is always better to make all data members private and then provide public methods for accessing them.
Inheritance and Constructors
Constructors are not inherited.When a subclass is instantiated, the superclass default constructor is executed first.
The super keyword refers to an object’s superclass.
The superclass constructor can be explicitly called from the subclass by using the super keyword.
Calling a superclass constructor must be the first java statement in the subclass constructors.
Method Overriding
Method Overloading vs. Method OverridingOverloading: Several methods have the same name, but different parameter lists.
Overriding: Subclass overrides superclass’s method, they both have the same signature.
A subclass may have a method with the same signature as the superclass method.
The subclass method can override the superclass method. This is known as method overriding.
A subclass method that overrides a superclass method must have the same signature as the superclass method.
The subclass object invokes the subclass’s version of the method, not the superclass’ version.
The @Override comment tag should be used just before the subclass method declaration.
This causes the compiler to display an error message if the method fails to correctly override a method in the superclass.
A subclass method can call the overridden superclass method via the super keyword.
Preventing a Method from Being Overridden
The final modifier will prevent the overriding of the superclass method in a subclass.
If a subclass attempts to override a final method, the compiler generates an error.
This ensures that a particular superclass method is used by subclasses rather than a modified version of it.
Object Class
All Java classes are directly or indirectly derived from a class named Object.Object is in the java.lang package.
Any class that does not specify the extends keyword is automatically derived from the Object class.
Basically, every class is derived from the Object class.
Every class inherits the Object class’ members.
Example: toString() and equals() methods.
In the Object class, the toString() method returns a string containing the object’s class name and the hash code of its memory address.
The equals() method accepts the address of an object as its argument and returns true if it is the same as the calling object’s address.
Example: toString() and equals() methods.
In the Object class, the toString() method returns a string containing the object’s class name and the hash code of its memory address.
The equals() method accepts the address of an object as its argument and returns true if it is the same as the calling object’s address.
***Inheritance Does Not Work in Reverse
It is impossible for a superclass to call a subclass’ method.