Java Tutorial

Java Abstract Classes

An abstract method has no body — subclasses must implement it. You cannot construct an abstract class; you extend it and fill in the missing pieces.

A partial blueprint

Mark a class abstract when it should never be created on its own. It can hold shared fields and concrete methods, plus abstract methods that subclasses must override.

Example

abstract class Shape {
  abstract double area();

  void describe() {
    System.out.println("area = " + area());
  }
}

class Circle extends Shape {
  double r;
  Circle(double r) { this.r = r; }

  @Override
  double area() {
    return Math.PI * r * r;
  }
}

public class Main {
  public static void main(String[] args) {
    Shape s = new Circle(2);
    s.describe();
  }
}

You cannot new an abstract class

The compiler rejects new Shape(). Abstract types exist to be extended. Variables of the abstract type are fine — they must hold a concrete subclass instance.

Example — this does not compile

abstract class Shape {
  abstract double area();
}

// Shape s = new Shape();   // error: Shape is abstract; cannot be instantiated

Shared code plus required hooks

Abstract classes shine when subclasses share real implementation. Put the common logic in concrete methods; leave only the varying steps abstract.

Example

abstract class Worker {
  abstract String role();

  void introduce() {
    System.out.println("I am a " + role());
  }
}

class Chef extends Worker {
  @Override String role() { return "chef"; }
}

class Pilot extends Worker {
  @Override String role() { return "pilot"; }
}

public class Main {
  public static void main(String[] args) {
    Worker[] crew = { new Chef(), new Pilot() };
    for (Worker w : crew) w.introduce();
  }
}

Prefer an abstract class when subclasses share code or fields. Prefer an interface when you only need a contract and classes may already extend something else.

Try It Yourself

Exercise: Create abstract class Animal with abstract void speak(). Implement Dog and Cat. Store both in an Animal[] and callspeak.

Show solution
abstract class Animal {
  abstract void speak();
}

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) {
    Animal[] zoo = { new Dog(), new Cat() };
    for (Animal a : zoo) a.speak();
  }
}

The abstract type forces every subclass to supply speak; the array still holds Animal references.

Key Takeaways

  • Abstract methods have no body; subclasses must implement them.
  • You cannot construct an abstract class with new.
  • Abstract classes can mix shared concrete code with required hooks.
  • Use them for partial blueprints; use interfaces for pure 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.

Physics

Abstract instrument

An abstract class defines read() without implementing it. Subclasses supply the concrete sensor behaviour.

Example

abstract class Instrument {
  abstract double read();
}
class Thermometer extends Instrument {
  double read() { return 21.5; }
}

public class Main {
  public static void main(String[] args) {
    Instrument i = new Thermometer();
    System.out.println(i.read());
  }
}

FAQ: Java Abstract Classes

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 abstract class 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 abstract class in this Java Java lesson (Java Abstract Classes).

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.