In Java, you can use inherited methods simply by creating an instance of a subclass and then calling the method using that instance — just as you would with methods defined in the subclass itself.
Inheritance is the process of building a new class based on the features of another existing class. It is used heavily in Java, Python, and other object-oriented languages to increase code reusability and simplify program logic into categorical and hierarchical relationships.
However, each language has its own unique way of implementing inheritance that can make switching difficult.
Today, we’ll give you a crash course Java inheritance and show you how to implement the core inheritance tools like typecasting, method overriding, and final
entities.
Start mastering Java with our hands-on courses today.
Java is one of the most sought after and widely used programming languages in the tech industry, and will continue to be for the foreseeable future. It runs on everything from phones to game consoles to data centers. This path is perfect for you if you already have experience programming, but are new to Java. It’ll help you become an employable Java developer with incredibly valuable skills, ready to jump in and contribute to real-world projects.
Inheritance is a mechanism that allows one class to inherit properties or behaviors from another class. Multiple classes can inherit from the same parent class, forming a tree-like hierarchy structure. Inheriting classes can add features beyond those inherited from the parent class to allow for unique behavior.
Inheritance is essential to advanced Object Oriented Programming (OOP) as it allows you to reuse one class’s features across your program without replicating code.
Inheritance is often used to represent categories (parent classes) and sub-categories (subclasses). The parent class sets the features present in all objects regardless of subcategory, while each subclass represents a smaller, more specific category.
For example, you could create the class Car
that specifies wheels = 4
and a subclass Sedan
that includes the attribute doors = 4
. The flow of inheritance relationships often reflects the logical relationship similar to squares and rectangles; in this case, all sedans are cars, but not all cars are sedans.
Inheritance has three main advantages:
Each programming language has slightly different terminology for inheritance. In Java, the parent class is called the superclass, and the inheritor class is called the subclass. Developers may also call superclasses base or parent classes and subclasses derived or child classes.
Subclasses are linked to superclasses using the extends
keyword during their definition. Subclasses can define new local methods or fields to use or can use the super
keyword to call inherited methods or the super constructor.
class b {
// implementation of inheritedMethod()
}
class a extends b
{
inheritedMethod();
}
super
is essentially a “previous value” button called from within a child class that allows you to read and access features from the parent class regardless of their value in the current child class.
The super
keyword is used to:
super.var
reads the value of var
set in the parent class, while var
alone reads the modified value from the child.super.method()
allows the child to access the parent class implementation of method()
. This is only required if the child class also has a method with the same name.As a refresher, constructors in Java are special methods used to initialize objects. Calling the super constructor creates a new object that requires all the fields defined in the parent class constructor.
You can then add additional fields in other statements to make the child instance more specific than the parent. Essentially, it allows you to use the parent class constructor as a template for your child class constructor.
public Car(String make, String color, int year, String model, String bodyStyle) {
super(make, color, year, model); //parent class constructor
this.bodyStyle = bodyStyle;
}
There are several types of inheritance available in Java:
Single inheritance is when a single subclass inherits from a superclass, forming one layer of inheritance.
Multilevel Inheritance is when a superclass is inherited by an intermediate class, which is then inherited by a derived class, forming 3 or more levels of inheritance.
Hierarchical inheritance is when one superclass serves as a baseline for multiple specific subclasses. This is the most common form of inheritance.
There are also two other types of inheritance that are only available in Java through a combination of class and interface inheritance.
Multiple inheritance, when a single subclass inherits from multiple parent classes.
Hybrid inheritance, a mix of two or more of the above kinds of inheritance.
Java does not support multiple inheritance with classes, meaning both of these types of inheritance are impossible with Java classes alone. However, a subclass can inherit more than one interface (an abstract class). You can therefore simulate multiple inheritance if you combine the use of interfaces and classes.
To help you understand inheritance more, let’s look at Java inheritance examples in pseudocode. Pay attention to the syntax components of inheritance we’ve seen so far, like super
and shared methods.
To declare inheritance in Java, we simply add extends [superclass]
after the subclass’s identifier.
Here’s an example of a class Car
that inherits from base class Vehicle
using private strings and getter/setter methods to achieve encapsulation.
// Base Class Vehicleclass Vehicle {// Private Fieldsprivate String make;private String color;private int year;private String model;// Parameterized Constructorpublic Vehicle(String make, String color, int year, String model) {this.make = make;this.color = color;this.year = year;this.model = model;}// public method to print detailspublic void printDetails() {System.out.println("Manufacturer: " + make);System.out.println("Color: " + color);System.out.println("Year: " + year);System.out.println("Model: " + model);}}// Derived Class Carclass Car extends Vehicle {// Private fieldprivate String bodyStyle;// Parameterized Constructorpublic Car(String make, String color, int year, String model, String bodyStyle) {super(make, color, year, model); //calling parent class constructorthis.bodyStyle = bodyStyle;}public void carDetails() { //details of carprintDetails(); //calling method from parent classSystem.out.println("Body Style: " + bodyStyle);}}class Main {public static void main(String[] args) {Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car ObjectelantraSedan.carDetails(); //calling method to print details}}
This is an example of single inheritance, as only one object inherits from the parent class. On line 37, you can see that we use super
to call the superclass constructor that simplifies our Car
constructor. You can also see how Car
has access to the Vehicle
class printDetails()
method on line 42.
printDetails()
can be called withoutsuper
becauseCar
does not have its own implementation ofprintDetails()
. Thesuper
keyword is only needed when the program must decide which version of the method is being used.
Start mastering Java with our hands-on courses today.
Java is one of the most sought after and widely used programming languages in the tech industry, and will continue to be for the foreseeable future. It runs on everything from phones to game consoles to data centers. This path is perfect for you if you already have experience programming, but are new to Java. It’ll help you become an employable Java developer with incredibly valuable skills, ready to jump in and contribute to real-world projects.
Java also allows you to reference a subclass as an instance of its superclass, essentially treating the subclass as if it were of the superclass type. This process is known as typecasting. It is a great way to create modular code as you can write code that will work for any subclass of the same parent. For example, you can reference a Car
type variable as a Vehicle
type object.
Car car = new Car();
Vehicle vehicle = car;
We first create a Car
instance then assign that instance to a Vehicle
type variable. Now the Vehicle
variable reference points to the Car
instance. This allows you to treat any subclass of Vehicle
as the same Vehicle
type, even if you don’t know which subclass of Vehicle
it is.
The two types of typecasting are upcasting and downcasting.
Upcasting is when you treat a child class as if it were an instance of the parent class, like our previous example. Any fields unique to the child class will be hidden to let them fit the mold of the parent class.
Downcasting is when you treat an instance of the parent class as if it were one of its child classes. While any subclass can be upcast, only objects that were originally subclass typed can be downcast.
In other words, an object can be downcast if the object was originally of the subclass type but was later upcast to the parent class.
//valid code
Car car = new Car();
// upcast to Vehicle
Vehicle vehicle = car;
// downcast to car again
Car car2 = (Car) vehicle;
The upcast object still retains the fields it had and therefore can be added back to make it a valid object of the child class type again.
However, objects that were originally of the parent class do not have values for any essential fields unique to the child class. As a result, it will compile but will throw an error at runtime.
Sometimes we’ll need one of our subclasses to edit the behavior of an inherited method. Java lets us do this by overriding existing methods by creating new methods of the same name. It also allows us to provide class implementations of abstract methods from interfaces.
class Parent {
void myMethod() {
//original implementation
}
}
class Child extends Parent {
@override
void myMethod() {
//new implementation
}
}
Method overriding is a fundamental tool when implementing polymorphism, a design principle that allows for different classes to have unique implementations for the same method. If we break down the word, “poly” means many, and “morph” means form.
In simplest terms, polymorphism means having many class-specific forms of a process to accomplish the same task.
Here are the features a program must have to allow method overriding:
Final
class.
To override a method in Java, define a new method with the same name as the method you wish to override and add the @Override
tag above it.Here, you can see an example of how we can create class-specific behavior for the same method call. Our method call is always getArea()
however the implementation of the method depends on the class of shape being evaluated.
// A sample class Shape which provides a method to get the Shape's areaclass Shape {public double getArea() {return 0;}}// A Rectangle is a Shape with a specific width and heightclass Rectangle extends Shape { // extended form the Shape classprivate double width;private double height;public Rectangle(double width, double height) {this.width = width;this.height = height;}public double getArea() {return width * height;}}// A Circle is a Shape with a specific radiusclass Circle extends Shape {private double radius;public Circle(double radius) {this.radius = radius;}public double getArea() {return 3.14 * radius * radius;}}class driver {public static void main(String args[]) {Shape[] shape = new Shape[2]; // Creating shape array of size 2shape[0] = new Circle(2); // creating circle object at index 0shape[1] = new Rectangle(2, 2); // creating rectangle object at index 1// Shape object is calling children classes methodSystem.out.println("Area of the Circle: " + shape[0].getArea());System.out.println("Area of the Rectangle: " + shape[1].getArea());}}
The advantages of method overriding are:
In Java, the final
keyword can be used while declaring a variable, class, or method to make the value unchangeable. The value of the entity is decided at initialization and will remain immutable throughout the program. Attempting to change the value of anything declared as final
will throw a compiler error.
// declaring a final variable
class FinalVariable {
final int var = 50;
var = 60 //This line would give an error
}
The exact behavior of final
depend on the type of entity:
final
Parameter cannot be changed anywhere in the functionfinal
Method cannot be overridden or hidden by any subclassfinal
Class cannot be a parent class for any subclassfinal boolean immutable = true;
boolean mutable = immutable;
While the value of final
entities cannot be changed, they can be used to set the value of non-final
variables. This property makes it helpful for solving data mutability problems where multiple sections of code need to reference the same entity to function.
You can set the sections to reference the final
version of the entity, use it to create a non-final
entity copy, then manipulate that for any operations. Using final
ensures that the original shared reference remains the same so that each piece can behave consistently.
Inheritance is a powerful tool in Java and is essential to understand advanced OOP designs. Some next concepts to explore on your Java development journey are:
To help you understand these and other advanced concepts, we’ve created the Java for Developers Path. Within, you’ll find a collection of our finest Java content on topics like OOP, multithreading, recursion, and new Java 8 features. These lessons are chosen from across our course library, allowing you to learn to code in Java from our best material for each concept.
By the end, you’ll have the skills and hands-on experience needed to ace your next Java interview.
Happy learning!
Free Resources