Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Abstract Classes and Methods: A Comprehensive Guide

Java Abstract Classes and Methods

This tutorial provides a clear, example‑driven exploration of Java abstract classes and methods, and explains how abstraction is implemented in Java.

Java Abstract Class

In Java, an abstract class cannot be instantiated. The abstract keyword marks the class as abstract. For example,

// create an abstract class
abstract class Language {
  // fields and methods
}
...
// try to create an object Language
// throws an error
Language obj = new Language();

An abstract class may contain both concrete (regular) methods and abstract methods. For example,

abstract class Language {
  // abstract method
  abstract void method1();
  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}

For more on regular methods, see Java methods.


Java Abstract Method

An abstract method has no body; it declares a contract that subclasses must fulfill. It is defined with the abstract keyword, e.g.,

abstract void display();

Because it lacks a body, the statement ends with a semicolon. If a class declares an abstract method, the class itself must be declared abstract, otherwise the compiler throws an error:

// error
// class should be abstract
class Language {
  // abstract method
  abstract void method1();
}

Example: Java Abstract Class and Method

Although abstract classes cannot be instantiated directly, concrete subclasses can extend them and use their members. For instance,

abstract class Language {
  public void display() {
    System.out.println("This is Java Programming");
  }
}
class Main extends Language {
  public static void main(String[] args) {
    Main obj = new Main();
    obj.display();
  }
}

Output

This is Java programming

Here, the abstract class Language defines a concrete method display(). The subclass Main inherits that method and invokes it through its instance.


Implementing Abstract Methods

When an abstract class declares an abstract method, every concrete subclass must provide an implementation. For example,

abstract class Animal {
  abstract void makeSound();
  public void eat() {
    System.out.println("I can eat.");
  }
}
class Dog extends Animal {
  public void makeSound() {
    System.out.println("Bark bark");
  }
}
class Main {
  public static void main(String[] args) {
    Dog d1 = new Dog();
    d1.makeSound();
    d1.eat();
  }
}

Output

Bark bark
I can eat.

The subclass Dog implements the abstract makeSound() method, while inheriting the concrete eat() method.

Note: If Dog does not override makeSound(), it must itself be declared abstract.


Accessing Constructor of Abstract Classes

Abstract classes can define constructors just like normal classes. Subclasses invoke the superclass constructor using super(). For example,

abstract class Animal {
   Animal() {
      // initialization logic
   }
}
class Dog extends Animal {
   Dog() {
      super();
      // additional initialization
   }
}

Remember that super() must be the first statement in the subclass constructor.


Java Abstraction

Abstract classes and methods are the primary tools for achieving abstraction in Java. Abstraction hides unnecessary implementation details, presenting only the essential interface to the user. This simplifies complex systems and promotes flexibility.

For instance, a motorbike brake performs the same high‑level function—slowing the bike—while its internal mechanism may differ across models.

Example 3: Java Abstraction

abstract class MotorBike {
  abstract void brake();
}
class SportsBike extends MotorBike {
  public void brake() {
    System.out.println("SportsBike Brake");
  }
}
class MountainBike extends MotorBike {
  public void brake() {
    System.out.println("MountainBike Brake");
  }
}
class Main {
  public static void main(String[] args) {
    MountainBike m1 = new MountainBike();
    m1.brake();
    SportsBike s1 = new SportsBike();
    s1.brake();
  }
}

Output:

MountainBike Brake
SportsBike Brake

Each concrete subclass supplies its own brake() implementation, illustrating how abstraction decouples interface from implementation.

Note: Interfaces can also be used to achieve abstraction in Java.


Key Points to Remember

Java

  1. Mastering C# Abstract Classes & Methods: A Practical Guide
  2. Java Classes and Objects: A Practical Guide
  3. Java Methods: How to Define, Call, and Use Them Effectively
  4. Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
  5. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  6. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  7. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  8. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  9. Master Java: Understanding Objects, Classes, and Core OOP Concepts
  10. Java Abstraction Explained: Simplify Code & Boost Readability