In this post, we will learn the difference between Concrete Class and Abstract Class in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Concrete Class | Abstract Class |
---|---|
A concrete class is a class that can be directly instantiated using the new keyword. | An abstract class is a class that cannot be directly instantiated using the new keyword. |
All methods in a concrete class have a body (i.e., implementation). | Abstract classes may contain abstract methods, which are declared without an implementation. Subclasses are expected to provide an implementation for these methods. |
A concrete class is a complete class. | An abstract class is an incomplete class because it depends on its subclasses to provide a complete implementation. |
You can create an object directly from a concrete class. | You cannot create an object directly from an abstract class. You have to create an object from its non-abstract child classes. |
Concrete classes provide no restrictions on how they can be extended or what methods a subclass must implement. | Abstract classes can define abstract methods that subclasses must implement, creating a template for the way that a group of subclasses should behave. |
In a concrete class, every method behaves in a defined way. | In an abstract class, some methods can be left undefined (abstract methods). |
Example
Let's create an example to illustrate the difference between a concrete class and an abstract class in Java, along with the output.// Abstract class
abstract class Shape {
protected String name;
public Shape(String name) {
this.name = name;
}
// Abstract method
public abstract void draw();
// Concrete method
public void printName() {
System.out.println("Shape name: " + name);
}
}
// Concrete class that extends the abstract class
class Circle extends Shape {
private int radius;
public Circle(String name, int radius) {
super(name);
this.radius = radius;
}
// Implementing the abstract method
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
}
public class AbstractClassVsConcreteClassExample {
public static void main(String[] args) {
// Create a Circle object (Concrete class)
Circle circle = new Circle("Circle1", 5);
// Call the abstract method (polymorphism)
circle.draw();
// Call the concrete method
circle.printName();
}
}
Output:
Drawing a circle with radius 5
Shape name: Circle1
Explanation:
We have an abstract class Shape, which has a constructor, an abstract method draw(), and a concrete method printName().
The Shape class is declared abstract using the abstract keyword, and it contains an abstract method draw(). Abstract methods do not have a body and must be implemented by the subclasses (concrete classes) that extend the abstract class.
The Circle class is a concrete class that extends the Shape abstract class. It provides an implementation of the draw() method.
In the main method, we create an object of the Circle class and call the draw() method. Since Circle is a subclass of Shape, we can use polymorphism to call the draw() method.
We also call the printName() method, which is a concrete method defined in the Shape class. Concrete methods have a complete implementation and are inherited by the subclasses without any requirement for implementation.
The output shows that the draw() method of the Circle class is called, and it prints the message "Drawing a circle with radius 5". Additionally, the printName() method of the Shape class is called, and it prints the message "Shape name: Circle1".
The key difference between a concrete class and an abstract class is that an abstract class can have abstract methods without implementation, while a concrete class must implement all methods defined in its superclass or interface. Concrete classes can be instantiated, while abstract classes cannot be directly instantiated but can be subclassed to create concrete subclasses.
Comments
Post a Comment