In Object-Oriented Programming paradigm, classes can be derived from other classes, hence inheriting fields and methods from those classes.
Java, C++, Python, Scala, etc. are some of the programming languages that exhibit the property of inheritance.
The idea of inheritance is very simple yet powerful. There can be times when we want to create a new class and there is already an existing class that includes some of the code we might be in need, so in that case we can derive our new class from the existing class. In doing this, we can reuse the fields and methods of the existing class without having to do the whole stuff(writing and debugging the code) again by ourselves.
A subclass inherits all the members(i.e. fields, methods and nested classes) from its superclass or parent class.
A class derived from another class is called a subclass (also known as a derived class, extended class or child class). The class from which the subclass is derived is called a superclass (also known as a base class or a parent class).
Note: Constructors are not the members, hence they are not inherited by the subclasses, but the constructor of the superclass can be invoked from the subclass.
Except Object, which has no superclass, every class has one and only one direct superclass, which is also known as Single Inheritance. When there is no explicit declaration of a superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes that are derived from classes and so on, unless and until derived from the topmost class, Object. Such type of a class is said to be descended from all the classes in the inheritance chain stretching back to the superclass Object.
The Java Platform Class Hierarchy
The Object class, defined in the java.lang package, defines and implements the behavior common to all the classes, including the classes that we write. In the Java platform, many classes are derived directly from the Object class, and other classes are derived from those which are derived from some other classes and so on, forming a hierarchy of classes as a result.
Power of a Subclass
A subclass has the capability of inheriting all of the public and protected members of its parent, irrespective of what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. The inherited members can be used as is, can be replaced, hidden or supplemented with new members:
- The inherited fields can be used directly, just like any other fields.
- A field can be declared in the subclass with the same name as the one in the superclass, thus hiding it (which is not recommended).
- New fields in the subclass can be can be defined which are not in the superclass.
- The inherited methods can be used directly as they are.
- A new instance method can be written in a subclass that has the same signature as the one in the superclass, thus overriding it.
- A new static method can be written in a subclass that has the same signature as the one in the superclass, thus hiding it.
- New methods can be declared in the subclass that are not in the superclass.
- A subclass constructor can be written that invokes the constructor of the superclass, either implicitly or by using the keyword super.
Private Members in a Superclass
A subclass does not inherit private members of its parent class. However, if the superclass has its public or protected methods for accessing these its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class — both fields and methods. Hence, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
In my next article, I will try to elaborate on some of the examples (code snippets) that show inheritance and the common problems that we might run into while implementing inheritance.