Object Oriented Programming with Java: INHERITANCE

Code N Program 101
4 min readJun 9, 2023

--

Buy me a coffee ☕

If you want to know everything about the Java programming language from beginner to intermediate level, buy my ebook “A Complete Course on Java from Beginner to Intermediate Level

Java is an object-oriented programming language, which means that it is designed around the concept of objects. Here is one of the key features of Java’s object-oriented programming (OOP) system:

Inheritance

Inheritance is one of the key features of Java's object-oriented programming system, and it allows one class to inherit properties and methods from another class. This enables code reusability and simplifies code maintenance. In this post, we will explore the concept of inheritance in Java in detail.

The Basics of Inheritance

In Java, inheritance is implemented using the extends keyword. When a class extends another class, it inherits all the non-private members of the parent class. This includes fields, methods, and nested classes.

Here's an example of a simple class hierarchy:

class Vehicle {
protected int speed;

public void setSpeed(int speed) {
this.speed = speed;
}

public void start() {
System.out.println("Starting vehicle...");
}
}

class Car extends Vehicle {
private String model;

public Car(String model) {
this.model = model;
}

public void drive() {
System.out.println("Driving a " + model + " at " + speed + " mph");
}
}

In this example, the Vehicle class has a field called speed and two methods, setSpeed and start. The Car class extends Vehicle and adds a field called model and a method called drive.

When an instance of Car is created, it has access to the fields and methods of both Vehicle and Car.

Access Modifiers in Inheritance

When a subclass inherits a member from its superclass, the access level of the member in the subclass cannot be more restrictive than the access level in the superclass. In other words, a private member in the superclass cannot be accessed from the subclass.

The following table shows how the access levels of members in a superclass affect their accessibility in a subclass:

Access Levels in Superclass

Overriding Methods

Inheritance also allows subclasses to override methods defined in their superclass. This means that the subclass provides its own implementation of the method instead of using the implementation inherited from the superclass.

Here's an example of method overriding:

class Vehicle {
public void start() {
System.out.println("Starting vehicle...");
}
}

class Car extends Vehicle {
@Override
public void start() {
System.out.println("Starting car...");
}
}

In this example, the Car class overrides the start method of Vehicle with its own implementation.

When a method is overridden, the subclass's implementation is called instead of the superclass's implementation when the method is called on an instance of the subclass. If the subclass wants to call the superclass's implementation of the method, it can do so using the super keyword.

Abstract Classes

In Java, an abstract class is a class that cannot be instantiated and is intended to be subclassed. An abstract class can have abstract methods, which are declared but not implemented. Subclasses of an abstract class must provide an implementation for all of its abstract methods.

Here’s an example of an abstract class:

abstract class Animal {
protected String name;

public Animal(String name) {
this.name = name;
}

public abstract void makeSound();
}

class Dog extends Animal {
public Dog(String name) {
super(name);
}

@Override
public void makeSound() {
System.out.println(name + " barks");
}
}

In this example, the Animal class is an abstract class with an abstract method makeSound. The Dog class extends Animal and provides an implementation for the makeSound method.

Final Classes and Methods

In Java, the final keyword can be used to prevent a class from being subclassed or a method from being overridden. A final class cannot be extended, and a final method cannot be overridden by its subclasses.

Here's an example:

final class Vehicle {
// ...
}

class Car extends Vehicle { // Error: Cannot extend final class
// ...
}

class Vehicle {
public final void start() {
// ...
}
}

class Car extends Vehicle {
@Override
public void start() { // Error: Cannot override final method
// ...
}
}

In this example, the Vehicle class is marked as final, so it cannot be subclassed. The start method in the Vehicle class is marked as final, so it cannot be overridden by subclasses.

Summary

Inheritance is a fundamental concept in Java’s object-oriented programming system. It allows classes to inherit properties and methods from their parent classes, enabling code reuse and creating class hierarchies. Java supports single inheritance, where a class can inherit from only one superclass, but it supports multiple levels of inheritance, where a subclass can have its own subclasses. By understanding and effectively using inheritance, you can design and organize your Java code in a more modular, reusable, and maintainable way.

If you want to know everything about the Java programming language from beginner to intermediate level, buy my ebook “A Complete Course on Java from Beginner to Intermediate Level

--

--