Start Coding Now

Inheritance in Java - Extends Keyword

Learn about inheritance in Java. Single, Multilevel, and Hierarchical inheritance explained with "extends" keyword.

Inheritance (Subclass and Superclass)

Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass).

Keywords:

  • extends: inherits from a class
  • implements: inherits from an interface
class Vehicle {
  protected String brand = "Ford";
  public void honk() {
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle { private String modelName = "Mustang"; public static void main(String[] args) { Car myCar = new Car(); myCar.honk(); System.out.println(myCar.brand + " " + myCar.modelName); } }

Frequently Asked Questions