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:
- Abstract Methods: Methods declared without an implementation. Subclasses are required to provide the implementation for these methods.
- Concrete Methods: Methods with an implementation that can be inherited by subclasses.
- Cannot be Instantiated: Abstract classes cannot be used to create objects directly.
- Constructors: Abstract classes can have constructors, which can be called when a subclass object is created.
- 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
- Abstract Class
Shape
: Defines an abstract methodcalculateArea()
and a concrete methoddisplayArea()
. - Subclass
Circle
: Implements thecalculateArea()
method to calculate the area of a circle using the formula πr². - Subclass
Rectangle
: Implements thecalculateArea()
method to calculate the area of a rectangle using the formula length × width. - Main Class: Creates instances of
Circle
andRectangle
, 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
- Abstract Class
Shape
: Contains an abstract methodnumberOfSides()
with no implementation. - Subclass
Rectangle
: Implements thenumberOfSides()
method to display that a rectangle has 4 sides. - Subclass
Triangle
: Implements thenumberOfSides()
method to display that a triangle has 3 sides. - Subclass
Hexagon
: Implements thenumberOfSides()
method to display that a hexagon has 6 sides. - Main Class: Creates instances of
Rectangle
,Triangle
, andHexagon
, and calls theirnumberOfSides()
methods to display the number of sides.
Comments
Post a Comment