What is meant by Abstract class? What is Abstract method?


Q.) What is meant by Abstract class? What is Abstract method?

Subject: Object Oriented Programming and Methodology

Abstract Class:

An abstract class in Java is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

Key Points:

  • An abstract class is a class that is declared abstract—it may or may not include abstract methods.
  • Abstract classes cannot be instantiated, but they can be subclassed.
  • Abstract methods are methods that are declared without an implementation (without braces, and followed by a semicolon), and they must be overridden by subclasses.
  • Abstract classes are useful for defining a common interface for a group of related classes.
  • Abstract methods are useful for defining methods that must be implemented by subclasses, but the specific implementation can vary.

Example:

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

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

class Dog extends Animal {
    // Override abstract method
    @Override
    void makeSound() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    // Override abstract method
    @Override
    void makeSound() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();  // Create a Dog object
        animal.makeSound();        // Call the makeSound() method
        animal.eat();             // Call the eat() method

        animal = new Cat();         // Create a Cat object
        animal.makeSound();        // Call the makeSound() method
        animal.eat();             // Call the eat() method
    }
}

Output:

Woof!
Eating...
Meow!
Eating...

Abstract Method:

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). Abstract methods are only allowed in abstract classes, and they must be overridden by subclasses.

Key Points:

  • An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).
  • Abstract methods are only allowed in abstract classes.
  • Abstract methods must be overridden by subclasses.
  • Abstract methods are useful for defining methods that must be implemented by subclasses, but the specific implementation can vary.

Syntax:

abstract void methodName();

Example:

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

class Dog extends Animal {
    // Override abstract method
    @Override
    void makeSound() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    // Override abstract method
    @Override
    void makeSound() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();  // Create a Dog object
        animal.makeSound();        // Call the makeSound() method

        animal = new Cat();         // Create a Cat object
        animal.makeSound();        // Call the makeSound() method
    }
}

Output:

Woof!
Meow!