Java Inheritance

Java Inheritance

Inheritance in Java is a mechanism where a new class can acquire the properties and behaviors (methods and fields) of an existing class. It's like passing down characteristics from a parent to a child. Inheritance promotes code reuse and is a fundamental concept in object-oriented programming.

Superclasses and Subclasses

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class)

In Java, classes can have an "IS-A" relationship, much like a cat IS-A(n) animal. We'll illustrate this concept with code.

One of the main reasons is code reusability. You don't have to write the same code over and over again. We'll explain how inheritance helps you work smarter, not harder.

Syntax Simplified

Let's start with the syntax of inheritance. We'll use the extends keyword to create a connection between parent and child classes.

Creating Superclasses and Subclasses Time for some hands-on action! We'll create a superclass and a subclass, showing you how to inherit attributes and methods. Here's some code:

Example
// Superclass
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Subclass
class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

Inheritance is a powerful concept in Java that allows you to create new classes by inheriting attributes and methods from existing classes. In this tutorial, we'll dive into the various types of inheritance in Java, making them easy to understand with examples.

Single Inheritance

Single inheritance is the simplest form of inheritance, where a class inherits from only one superclass. It's like inheriting traits from a single parent.

Example
class Animal {
    void eat() {
        System.out.println("This animal eats.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

Multilevel Inheritance

In multilevel inheritance, a class inherits from another class, which in turn inherits from yet another class. It's like inheriting traits from your grandparents, then your parents.

Example
class Animal {
    void eat() {
        System.out.println("This animal eats.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class BabyDog extends Dog {
    void weep() {
        System.out.println("The baby dog weeps.");
    }
}

Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from a single superclass. It's like multiple siblings inheriting traits from the same parent.

Example
class Animal {
    void eat() {
        System.out.println("This animal eats.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("The cat meows.");
    }
}

Interface-based Multiple Inheritance

Java doesn't support multiple inheritance through classes (i.e., a class inheriting from multiple classes), but it allows multiple inheritance through interfaces. Interfaces are like contracts that a class can implement.

Example
interface Swim {
    void swim();
}

interface Fly {
    void fly();
}

class Bird implements Swim, Fly {
    public void swim() {
        System.out.println("The bird swims.");
    }

    public void fly() {
        System.out.println("The bird flies.");
    }
}

Contact Us

Name
Email
Mobile No:
subject:
Message: