🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

Inheritance

Inheritance in Java

Inheritance in Java

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). This promotes code reuse and establishes a relationship between classes. In Java, inheritance is achieved using the extends keyword.

Syntax:

class Superclass {
    // Fields and methods of the superclass
}

class Subclass extends Superclass {
    // Fields and methods specific to the subclass
}
  

Example:

// Superclass
class Animal {
    String species;

    Animal(String species) {
        this.species = species;
    }

    void eat() {
        System.out.println("Animal is eating");
    }

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

// Subclass
class Dog extends Animal {
    String breed;

    Dog(String species, String breed) {
        super(species); // Call the constructor of the superclass
        this.breed = breed;
    }

    void bark() {
        System.out.println("Dog is barking");
    }
  

Access Modifiers in Inheritance:

  1. Public Members: Public members of the superclass are accessible in the subclass.
  2. Protected Members: Protected members of the superclass are accessible in the subclass. Additionally, they are accessible to other classes in the same package.
  3. Default (Package-Private) Members: Default members of the superclass are accessible in the subclass if both classes are in the same package.
  4. Private Members: Private members of the superclass are not directly accessible in the subclass.

Method Overriding:

Inheritance allows a subclass to provide a specific implementation for a method that is already defined in its superclass. This is known as method overriding.

// Superclass
class Animal {
    void makeSound() {
        System.out.println("Some generic sound");
    }
}

// Subclass
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark! Bark!");
    }
  

super Keyword:

The super keyword is used to refer to the superclass, allowing you to access its fields, methods, or constructors.

// Subclass constructor using super keyword
class Dog extends Animal {
    String breed;

    Dog(String species, String breed) {
        super(species); // Call the constructor of the superclass
        this.breed = breed;
    }
  

Types of Inheritance:

  1. Single Inheritance: A subclass extends only one superclass.
  2. Multiple Inheritance (Interface-based): A subclass implements multiple interfaces. Java doesn't support multiple inheritance through classes to avoid the diamond problem, but it supports it through interfaces.
  3. Multilevel Inheritance: A subclass extends another subclass, creating a chain of inheritance.

Constructor Chaining:

In constructor chaining, a subclass constructor can call either its superclass constructor or another constructor within the same class using the super or this keyword.

// Superclass
class Animal {
    Animal() {
        System.out.println("Animal constructor");
    }
}

// Subclass
class Dog extends Animal {
    Dog() {
        super(); // Call the constructor of the superclass
        System.out.println("Dog constructor");
    }
  

Inheritance is a powerful mechanism that enables code reuse and facilitates the creation of a hierarchy of classes, leading to more organized and modular code. However, it's essential to use inheritance judiciously to avoid issues such as tight coupling and to favor composition over inheritance when appropriate.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.