DEV Community

Sundar Joseph
Sundar Joseph

Posted on

Inheritance in Java ;

Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.

Syntax ;

class ChildClass extends ParentClass {

// Additional fields and methods  
Enter fullscreen mode Exit fullscreen mode

}

Why Use Inheritance in Java? ;

Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code.
Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism.
Abstraction: The concept of abstraction where we do not have to provide all details, is achieved through inheritance. Abstraction only shows the functionality to the user.
Enter fullscreen mode Exit fullscreen mode

Key Terminologies Used in Java Inheritance ;

Class: Class is a set of objects that share common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).
Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Extends Keyword: This keyword is used to inherit properties from a superclass.
Enter fullscreen mode Exit fullscreen mode

How Inheritance Works in Java? ;

The extends keyword is used for inheritance in Java. It enables the subclass to inherit the fields and methods of the superclass. When a class extends another class, it means it inherits all the non-primitive members (fields and methods) of the parent class and the subclass can also override or add new functionality to them.

Note: The extends keyword establishes an "is-a" relationship between the child class and the parent class. This allows a child class to have all the behavior of the parent class.
Enter fullscreen mode Exit fullscreen mode

example ;

In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.
inheritance-660x454

Implementation:

// Parent class

class Animal {

void sound() {

    System.out.println("Animal makes a sound");

}
Enter fullscreen mode Exit fullscreen mode

}

// Child class

class Dog extends Animal {

void sound() {

    System.out.println("Dog barks");

}
Enter fullscreen mode Exit fullscreen mode

}

// Child class

class Cat extends Animal {

void sound() {

    System.out.println("Cat meows");

}
Enter fullscreen mode Exit fullscreen mode

}

// Child class

class Cow extends Animal {

void sound() {

    System.out.println("Cow moos");

}
Enter fullscreen mode Exit fullscreen mode

}

// Main class

public class Geeks {

public static void main(String[] args) {

    Animal a;

    a = new Dog();

    a.sound();  // Output: Dog barks
Enter fullscreen mode Exit fullscreen mode

    a = new Cat();

    a.sound();  // Output: Cat meows
Enter fullscreen mode Exit fullscreen mode

    a = new Cow();

    a.sound();  // Output: Cow moos

}
Enter fullscreen mode Exit fullscreen mode

}

Output

Dog barks
Cat meows
Cow moos

Top comments (0)