Abstract Classes

Abstract classes in Java are classes that cannot be instantiated on their own and are meant to be subclassed. They serve as a blueprint for other classes. An abstract class can contain abstract methods (methods without a body) and concrete methods (methods with a body).

Key Features of Abstract Classes:

  1. Abstract Methods: Methods declared without an implementation. Subclasses are required to provide the implementation for these methods.
  2. Concrete Methods: Methods with an implementation that can be inherited by subclasses.
  3. Cannot be Instantiated: Abstract classes cannot be used to create objects directly.
  4. Constructors: Abstract classes can have constructors, which can be called when a subclass object is created.
  5. Inheritance: Abstract classes are intended to be extended by other classes. A subclass must implement all abstract methods of the abstract class unless the subclass is also abstract.

Example:

abstract class Animal {
    // Abstract method (does not have a body)
    public abstract void makeSound();

    // Regular method
    public void eat() {
        System.out.println("This animal eats food.");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();  // Outputs: Woof
        myDog.eat();        // Outputs: This animal eats food.
    }
}

Abstract classes are useful when you want to define a common interface for a group of related classes but also want to provide some common functionality that the subclasses can inherit. They are commonly used in frameworks and libraries to provide a base class with default behavior while allowing customization through subclassing.

Program to try

 abstract class Shape {
    double area;

    // Abstract method
    public abstract void calculateArea();

    // Concrete method
    public void displayArea() {
        System.out.println("The area is: " + area);
    }
}


class Circle extends Shape {
    double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    // Implementation of abstract method
    @Override
    public void calculateArea() {
        area = Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    double length, width;

    Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    // Implementation of abstract method
    @Override
    public void calculateArea() {
        area = length * width;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle(5.0);
        circle.calculateArea();
        circle.displayArea();  // Outputs: The area is: 78.53981633974483

        Shape rectangle = new Rectangle(4.0, 6.0);
        rectangle.calculateArea();
        rectangle.displayArea();  // Outputs: The area is: 24.0
    }
}

Explanation

  1. Abstract Class Shape: Defines an abstract method calculateArea() and a concrete method displayArea().
  2. Subclass Circle: Implements the calculateArea() method to calculate the area of a circle using the formula πr².
  3. Subclass Rectangle: Implements the calculateArea() method to calculate the area of a rectangle using the formula length × width.
  4. Main Class: Creates instances of Circle and Rectangle, calculates their areas, and displays the results.

This example demonstrates the use of abstract classes to define a common interface for different shapes and provides specific implementations for calculating the area of each shape.


Here's a Java program that creates an abstract class named Shape with an abstract method numberOfSides(). Then, we provide three classes: Rectangle, Triangle, and Hexagon, each extending Shape and implementing the numberOfSides() method to display the number of sides of the respective shapes.

 abstract class Shape {
    // Abstract method
    public abstract void numberOfSides();
}


class Rectangle extends Shape {
    // Implementation of abstract method
    @Override
    public void numberOfSides() {
        System.out.println("A rectangle has 4 sides.");
    }
}

class Rectangle extends Shape {
    // Implementation of abstract method
    @Override
    public void numberOfSides() {
        System.out.println("A rectangle has 4 sides.");
    }
}

class Triangle extends Shape {
    // Implementation of abstract method
    @Override
    public void numberOfSides() {
        System.out.println("A triangle has 3 sides.");
    }
}
class Hexagon extends Shape {
    // Implementation of abstract method
    @Override
    public void numberOfSides() {
        System.out.println("A hexagon has 6 sides.");
    }
}
public class Main {
    public static void main(String[] args) {
        Shape rectangle = new Rectangle();
        rectangle.numberOfSides();  // Outputs: A rectangle has 4 sides.

        Shape triangle = new Triangle();
        triangle.numberOfSides();  // Outputs: A triangle has 3 sides.

        Shape hexagon = new Hexagon();
        hexagon.numberOfSides();  // Outputs: A hexagon has 6 sides.
    }
}

Explanation

  1. Abstract Class Shape: Contains an abstract method numberOfSides() with no implementation.
  2. Subclass Rectangle: Implements the numberOfSides() method to display that a rectangle has 4 sides.
  3. Subclass Triangle: Implements the numberOfSides() method to display that a triangle has 3 sides.
  4. Subclass Hexagon: Implements the numberOfSides() method to display that a hexagon has 6 sides.
  5. Main Class: Creates instances of Rectangle, Triangle, and Hexagon, and calls their numberOfSides() methods to display the number of sides.

Comments

Popular posts from this blog

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

String Problems