inheritance in java

what is the inheritance in java

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (subclass or child class) to inherit properties and behaviors from another class (superclass or parent class). The subclass can reuse and extend the functionality of the superclass, promoting code reuse and creating a hierarchical relationship between classes.

In Java, inheritance is implemented using the extends keyword. Here’s a basic example to illustrate inheritance:

inheritance in java

Example# 1

Example
// Parent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }

    void move() {
        System.out.println("Animal is moving.");
    }

    void makeNoise() {
        System.out.println("Animal makes a noise.");
    }
}

// Child class (subclass) inheriting from Animal
class Cow extends Animal {
    // Overriding the eat() method
    @Override
    void eat() {
        System.out.println("Cow is grazing.");
    }

    // Overriding the makeNoise() method
    @Override
    void makeNoise() {
        System.out.println("Cow says moo.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Cow class
        Cow myCow = new Cow();

        // Accessing methods from both Animal and Cow classes
        myCow.eat();        // Overridden method in Cow
        myCow.sleep();      // Inherited from Animal
        myCow.move();       // Inherited from Animal
        myCow.makeNoise();  // Overridden method in Cow
    }
}
  • The Animal class is the superclass with methods eat(), sleep(), move(), and makeNoise().
  • The Cow class is the subclass that extends Animal. It inherits the methods eat(), sleep(), move(), and makeNoise() from the Animal class and overrides the eat() and makeNoise() methods with its own implementations.
  • The main method demonstrates creating an instance of the Cow class and calling methods from both the Animal and Cow classes.

Key points about inheritance in Java:

  1. Code Reusability: Inheritance promotes code reuse by allowing subclasses to reuse the code of the superclass.
  2. Method Overriding: Subclasses can provide their own implementation for methods inherited from the superclass. This is known as method overriding.
  3. Access to Superclass Members: Subclasses have access to public and protected members of the superclass, allowing them to use or override those members.
  4. Single Inheritance: In Java, a class can extend only one superclass (single inheritance). However, a class can implement multiple interfaces, allowing it to inherit from multiple sources.

Example# 2

Example
// Parent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Child class (subclass) inheriting from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Dog class
        Dog myDog = new Dog();

        // Accessing methods from both Animal and Dog classes
        myDog.eat();    // Inherited from Animal
        myDog.sleep();  // Inherited from Animal
        myDog.bark();   // Specific to Dog
    }
}

In this example:

  • The Animal class is the superclass with methods eat() and sleep().
  • The Dog class is the subclass that extends Animal. It inherits the methods eat() and sleep() from the Animal class and adds its own method bark().
  • The main method demonstrates creating an instance of the Dog class and calling methods from both the Animal and Dog classes.

Example of method overriding in the subclass:

Example
class Cat extends Animal {
    // Method overriding
    @Override
    void eat() {
        System.out.println("Cat is eating.");
    }
}

public class Main {
    public static void main(String[] args) {
        Cat myCat = new Cat();
        myCat.eat();  // Calls the overridden method in Cat class
        myCat.sleep();  // Inherited from Animal class
    }
}

In this example, the Cat class overrides the eat() method inherited from the Animal class with its own implementation.

Leave a Reply

Your email address will not be published. Required fields are marked *