Java Tutorial

Java Inheritance

A subclass reuses a superclass with extends. Write the shared parts once, then specialize — and call super when you need the parent version.

extends reuses a class

class Dog extends Animal means Dog has every non-private member of Animal, plus whatever you add. Inheritance is for an "is-a" relationship: a dog is an animal.

Example

class Animal {
  void speak() {
    System.out.println("...");
  }
}

class Dog extends Animal {
  @Override
  void speak() {
    System.out.println("woof");
  }
}

public class Main {
  public static void main(String[] args) {
    new Dog().speak();   // woof
  }
}

super calls the parent

super.speak() runs the superclass method. A subclass constructor can call super(...)as its first statement to initialize the parent part of the object.

Example

class Animal {
  String name;

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

  void speak() {
    System.out.println(name + " makes a sound");
  }
}

class Dog extends Animal {
  Dog(String name) {
    super(name);
  }

  @Override
  void speak() {
    super.speak();
    System.out.println(name + " barks");
  }
}

public class Main {
  public static void main(String[] args) {
    new Dog("Mochi").speak();
  }
}

One superclass only

Java classes use single inheritance: one extends clause. For extra capabilities that are not a strict parent type, use interfaces. You can still build deep trees — Dog extends Animalextends Object — but each class has one direct parent.

Example — inherited fields

class Vehicle {
  int speed;
}

class Bike extends Vehicle {
  void accelerate() {
    speed += 5;
  }
}

public class Main {
  public static void main(String[] args) {
    Bike b = new Bike();
    b.accelerate();
    System.out.println(b.speed);   // 5
  }
}

Inheritance couples types tightly. Prefer it when the subclass truly is a specialized version of the parent. For "has-a" (a car has an engine), use a field — composition — instead of extending.

Try It Yourself

Exercise: Create Cat extends Animal that overrides speak to print"meow". In main, call speak on both a Dog and aCat.

Show solution
class Animal {
  void speak() { System.out.println("..."); }
}

class Dog extends Animal {
  @Override void speak() { System.out.println("woof"); }
}

class Cat extends Animal {
  @Override void speak() { System.out.println("meow"); }
}

public class Main {
  public static void main(String[] args) {
    new Dog().speak();
    new Cat().speak();
  }
}

Both subclasses reuse the Animal type idea but supply their own speak behavior.

Key Takeaways

  • extends creates an is-a relationship and reuses the superclass.
  • Override methods to specialize; mark with @Override.
  • super reaches the parent method or constructor.
  • Java allows one superclass; use interfaces for additional contracts.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Biology

A specialised animal

A subclass reuses a superclass. Dog is still an Animal, but speak is the specialised version.

Example

class Animal {
  void speak() { System.out.println("..."); }
}
class Dog extends Animal {
  @Override void speak() { System.out.println("woof"); }
}

public class Main {
  public static void main(String[] args) {
    new Dog().speak();
  }
}

FAQ: Java Inheritance

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java inheritance examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java inheritance in this Java Java lesson (Java Inheritance).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.