Inheritance in Java

Inheritance is a fundamental concept of object-oriented programming in Java that allows a new class (called the "subclass" or "derived class") to be based on an existing class (called the "superclass" or "base class"). The new subclass inherits all the properties and behaviors of the superclass, and can also add new features or override the existing ones.

Here's an example:

// Superclass
class Animal {
  private String name;

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

  public void eat() {
    System.out.println(name + " is eating.");
  }
}

// Subclass
class Cat extends Animal {
  public Cat(String name) {
    super(name);
  }

  public void meow() {
    System.out.println("Meow!");
  }

  @Override
  public void eat() {
    System.out.println("The cat " + super.getName() + " is eating.");
  }
}

// Main class
class Main {
  public static void main(String[] args) {
    Cat cat = new Cat("Whiskers");
    cat.eat();  // Output: The cat Whiskers is eating.
    cat.meow(); // Output: Meow!
  }
}

In this example, we have a superclass Animal that has a private field name and a method eat(). We also have a subclass Cat that extends Animal and has an additional method meow(). When we create an instance of Cat and call the eat() and meow() methods, we can see that the eat() method of the Cat class overrides the eat() method of the Animal class, and the meow() method is unique to the Cat class.

By using inheritance in Java, we can reduce code duplication, improve code organization, and make it easier to add new features or modify existing ones without affecting the existing code.

Types of Inheritance
There are various types of inheritance.

  • Single Inheritance: In single inheritance, a class extends only one superclass. For example:
// Superclass
class Animal {
  public void eat() {
    System.out.println("Eating...");
  }
}

// Subclass
class Cat extends Animal {
  public void meow() {
    System.out.println("Meow!");
  }
}

// Main class
class Main {
  public static void main(String[] args) {
    Cat cat = new Cat();
    cat.eat();  // Output: Eating...
    cat.meow(); // Output: Meow!
  }
}

In this example, the Cat class extends the Animal class, and inherits the eat() method from the Animal class.

  • Multilevel Inheritance: In multilevel inheritance, a class extends a superclass, and the subclass of that class extends the first subclass, and so on. For example:

// Superclass
class Animal {
  public void eat() {
    System.out.println("Eating...");
  }
}

// Subclass
class Cat extends Animal {
  public void meow() {
    System.out.println("Meow!");
  }
}

// Subclass of Subclass
class PersianCat extends Cat {
  public void purr() {
    System.out.println("Purr...");
  }
}

// Main class
class Main {
  public static void main(String[] args) {
    PersianCat persianCat = new PersianCat();
    persianCat.eat();  // Output: Eating...
    persianCat.meow(); // Output: Meow!
    persianCat.purr(); // Output: Purr...
  }
}

In this example, the PersianCat class extends the Cat class, which in turn extends the Animal class.

  • Hierarchical Inheritance: In hierarchical inheritance, multiple classes extend the same superclass. For example:

// Superclass
class Animal {
  public void eat() {
    System.out.println("Eating...");
  }
}

// Subclass 1
class Cat extends Animal {
  public void meow() {
    System.out.println("Meow!");
  }
}

// Subclass 2
class Dog extends Animal {
  public void bark() {
    System.out.println("Bark!");
  }
}

// Main class
class Main {
  public static void main(String[] args) {
    Cat cat = new Cat();
    Dog dog = new Dog();
    cat.eat();  // Output: Eating...
    cat.meow(); // Output: Meow!
    dog.eat();  // Output: Eating...
    dog.bark(); // Output: Bark!
  }
}

In this example, the Cat and Dog classes both extend the Animal class.

  • Multiple Inheritance (not supported in Java): In multiple inheritance, a class extends more than one superclass. However, Java does not support multiple inheritance with classes.
Next Post Previous Post
No Comment
Add Comment
comment url