Inheritance

Pic Courtesy: Liane Metzler (Unsphash)

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.

Pictorial Representation: All the classes in the Java Platform are descendants of Object. The top of the hierarchy, the Object, being the most general of all the classes. The classes near the bottom of the hierarchy provide more specialized behavior.

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:

  1. The inherited fields can be used directly, just like any other fields.
  2. 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).
  3. New fields in the subclass can be can be defined which are not in the superclass.
  4. The inherited methods can be used directly as they are.
  5. A new instance method can be written in a subclass that has the same signature as the one in the superclass, thus overriding it.
  6. A new static method can be written in a subclass that has the same signature as the one in the superclass, thus hiding it.
  7. New methods can be declared in the subclass that are not in the superclass.
  8. 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.

--

--

--

Big Data Engineer / Data Engineer / Data Modeler | Talks about data

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Data Warehouse Training — Episode 3 — Data Warehouse VS Database

What happens when you type ‘ls -l *.c’​ in the shell

Leveraging Artificial Intelligence (AI) to build a scalable cloud-based solution to efficiently…

Check the workflow run status in GitHub actions in any CI Engine.

Starting with Caffe AI Framework

MassVulScan.sh, quickly identify open network ports and any associated vulnerabilities

Accident Lawyer Sandy Utah

accident lawyer sandy utah

Why Laravel is a good choice for your business

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Piu Mallick

Piu Mallick

Big Data Engineer / Data Engineer / Data Modeler | Talks about data

More from Medium

Singleton — The beauty of Single Instance of the Class

Which implementation of Singleton Pattern do I need?

Bloom Filter 101

Introduction To Binary Trees In Java