Inheritance
Java allows a Class to extend the content of another Class to reduce code redundancies. This coding practice is called Inheritance. It is one of the fundamental Object-oriented techniques that aim to reduce the code we write in our project.
In this code note, we will cover following topics:
- DRY Principal
- Inheritance Basics
- Method Overriding
- Controlling Inheritance
- Inheritance & Constructor
- Summary
› DRY Principal¶
One of the frowned-upon practices in software development is writing repeating code. So much so that there is a programming principle aimed at preventing code redundancies called Don't Repeat Yourself.

The Don't Repeat Yourself principle mandates avoiding duplicate writing code as much as possible. It is one of the fundamental principles of software development aimed at reducing code repetition. Inheritance is one of the most effective and straightforward ways to reduce redundant code in your project.
Let us understand reducing code redundancies by looking at these two separate Class models:
As you can see, both class model contains duplicate fields and methods, as shown in red. This code redundancy is common as we often have to represent similar data types in our project. However, creating a Class that contains duplicate fields and methods violates the DRY principle.
Using inheritance, we can prevent DRY principle violations. In Java, the inheritance gives us the ability seamlessly copy & paste the fields and methods of one Class to another Class, which results in unique codebase.
As you can see, inheritance extracted these repeating Fields and Methods into another class called BankAccount. Because of this, there are no more repeating codes, and an is-a relationship has formed between super and subclasses.
For example:
CheckingAccountis-aBankAccountSavingAccountis-aBankAccount
If we make any changes to the BankAccount class, the changes will be automatically reflected in the CheckingAccount and SavingAccount classes.
› Inheritance Basics¶
Let's go over some inheritances key points:
- Java class can be inherited by another class by using the extends keyword.
- The class we are trying to inherit is called Parent class or Super class.
- The class that inherited fields and methods from the parent class is the Child class or Sub class.
- Methods that are unique to the Child class are known as specialization.
- In the Child class, we can override the inherited methods from the Parent class.
Let's understand inheritance key points with code examples. Consider the following class models.
The Peter class is a parent class where all of its public fields and public methods will be inherited.
The Stewie class is a child class where it inherits all the public fields and public methods form another class.
Usage:
Type these out in the main() method.
› Method Overriding¶
From the above examples, we know that objects of a subclass can also access methods of its superclass. What happens if the same method is defined in both the superclass and subclass? For example, the same fight() method exists in both classes. The only difference is their method code content. This is known as Method Overriding.
Method overriding allows the Subclass to have its version of the method implementation for the method it shares with its parent class.
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by the parent class—allowing it has its own unique implementation.
Even though it is optional, You should label your overriding method with @Override annotations to clarify that it is performing method overridden. It is explicit programming, and everybody would appreciate you.
Object Class
The Object class is the root of the Java class hierarchy. Every class you make in Java inherits an Object class directly or indirectly behind the scene. Thanks to the code inside this class, we can create an object from our own class after designing them.
Peter's class is directly inheriting the Object class behind the scene. Stewie's class, extending Peter's class, is indirectly extending the Object class.
Object class has some practical methods you should remember. Every single class in Java has these methods.
| Method Name | Description |
|---|---|
clone() |
Create a new object instance that duplicates the current instance. |
hashCode() |
Get a hash code ( ID ) for the current instance. |
getClass() |
Return type information for the current instance. |
finalize() |
Handle special resource cleanup scenarios. |
toString() |
Return text representing the current instance. |
equals() |
Compare another object to the current instance for equality. |
› Controlling Inheritance¶
Java offers two ways of controlling inheritance. We can prevent our class from being inherited, and also, we can force our class to be used only for inheritance.
Prohibiting Inheritance
Use final keyword. If you mark your class as final, that class cannot be used for inheritance.
If you mark your method as final, that method cannot be overridden in inheritance.
Forcing Inheritance:
Use abstract keyword. If you mark your class as abstract, that class can only be used in inheritance. You cannot use it standalone such as creating an object from it. Note that abstract classes can contain normal methods and abstract methods.
If the abstract class contains abstract methods, then the child class must override and provide the method body for the abstract methods. This is one way of forcing method override in the child class.
› Inheritance & Constructor¶
Please note that only the Fields and Methods are inherited. The Constructor section of the class is not inherited.
If the superclass contains a unique constructor, you must call these constructors in subclass constructors. You can do this by using the super keyword. Calling the superclass constructor inside the subclass constructor must be the first line of code in the Child class's constructor.
The super keyword in Java is used in subclasses to access superclass members (attributes, constructors, and methods).
Use of super keyword
- To call methods of the super class that is overridden in the subclass.
- To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name.
- To explicitly call the superclass' constructor from the subclass constructor.
› Summary¶
Here is some key points I want you guys to remember about inheritance.
- Inheritance uses the
extendskeyword in the form ofChildClass extends ParentClass - Child class overrides parent class's public methods.
- All classes extend the Object class either directly or indirectly.
- The
superkeyword allows access to the parent class fields and method in the child class. - The
finalkeyword is used to prevent inheritance. - The
abstractkeyword is used to force inheritance. - Constructor is not inherited.