Java Abstract Class

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

In Java, an abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can contain both abstract methods (methods without a body) and concrete methods (methods with an implementation). Abstract classes are used to provide a common base class for other classes to extend and share common code.

Key Points:

  • Cannot be Instantiated: Abstract classes cannot be instantiated directly.
  • Abstract Methods: Can contain methods without implementation.
  • Concrete Methods: Can also contain methods with implementation.
  • Constructors: Can have constructors, which can be called by subclasses.
  • Common Base Class: Used to share common code among related classes.

Table of Contents

  1. Defining an Abstract Class
  2. Abstract Methods
  3. Implementing Abstract Methods
  4. Constructors in Abstract Classes
  5. Real-World Example
  6. Conclusion

1. Defining an Abstract Class

An abstract class is defined using the abstract keyword. It can contain both abstract and concrete methods.

Example:

public abstract class Animal {
    // Abstract method
    public abstract void makeSound();

    // Concrete method
    public void eat() {
        System.out.println("Eating...");
    }
}

Explanation:

  • abstract class Animal: Declares an abstract class named Animal.
  • public abstract void makeSound(): An abstract method that must be implemented by subclasses.
  • public void eat(): A concrete method with an implementation.

2. Abstract() Methods

Abstract methods are methods that do not have an implementation. Subclasses must provide an implementation for these methods.

Example:

public abstract class Animal {
    public abstract void makeSound();
}

Explanation:

  • public abstract void makeSound(): Declares an abstract method that subclasses must implement.

3. Implementing Abstract() Methods

Subclasses of an abstract class must implement all abstract methods from the abstract class.

Example:

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

public class AbstractClassExample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();  // Outputs: Bark
        dog.eat();        // Outputs: Eating...
    }
}

Explanation:

  • Dog: A subclass of Animal that provides an implementation for the makeSound() method.
  • AbstractClassExample: A class with a main method to test the Dog class.

4. Constructors in Abstract Classes

Abstract classes can have constructors, which can be called by subclasses to initialize common fields.

Example:

public abstract class Animal {
    private String name;

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

    public String getName() {
        return name;
    }

    public abstract void makeSound();

    public void eat() {
        System.out.println("Eating...");
    }
}

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

    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

public class ConstructorExample {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy");
        System.out.println("Dog's name: " + dog.getName());  // Outputs: Dog's name: Buddy
        dog.makeSound();  // Outputs: Bark
        dog.eat();        // Outputs: Eating...
    }
}

Explanation:

  • Animal(String name): A constructor in the abstract class Animal to initialize the name field.
  • Dog(String name): A constructor in the Dog class that calls the superclass constructor using super(name).

5. Real-World Example

Let's create a real-world example with multiple abstract classes and concrete classes that extend them.

Example:

public abstract class Vehicle {
    private String model;

    public Vehicle(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }

    public abstract void start();

    public void stop() {
        System.out.println("Vehicle stopped.");
    }
}

public class Car extends Vehicle {
    public Car(String model) {
        super(model);
    }

    @Override
    public void start() {
        System.out.println("Car " + getModel() + " started.");
    }
}

public class Motorcycle extends Vehicle {
    public Motorcycle(String model) {
        super(model);
    }

    @Override
    public void start() {
        System.out.println("Motorcycle " + getModel() + " started.");
    }
}

public class RealWorldExample {
    public static void main(String[] args) {
        Car car = new Car("Toyota");
        Motorcycle motorcycle = new Motorcycle("Harley Davidson");

        car.start();  // Outputs: Car Toyota started.
        car.stop();   // Outputs: Vehicle stopped.

        motorcycle.start();  // Outputs: Motorcycle Harley Davidson started.
        motorcycle.stop();   // Outputs: Vehicle stopped.
    }
}

Explanation:

  • Vehicle: An abstract class with an abstract method start() and a concrete method stop().
  • Car: A subclass of Vehicle that provides an implementation for the start() method.
  • Motorcycle: Another subclass of Vehicle that provides an implementation for the start() method.
  • RealWorldExample: A class with a main method to test the Car and Motorcycle classes.

6. Conclusion

Abstract classes in Java provide a way to define common behavior that can be shared among related classes while allowing for customization through abstract methods. By using abstract classes, you can achieve a higher level of code reuse and organization. Understanding how to define and use abstract classes and methods can help you design more flexible and maintainable Java applications.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare