How to create abstract class in Java?
Abstract Classes in Java
Abstract classes are an essential feature of Java, which provides a way to define common behavior that can be shared by multiple classes. They are used when you want to enforce a certain level of consistency across related classes and ensure that certain methods are implemented in subclasses.
Here is a step-by-step guide on how to create an abstract class in Java:
Keyword: To define an abstract class, you need to use the 'abstract' keyword before the class declaration.
For example:
abstract class Shape {
}
Abstract Methods: An abstract class can contain abstract methods. Abstract methods are methods without a body, only a signature. The signature of an abstract method includes the method name, return type, and parameters.
For example:
abstract class Shape {
abstract void draw();
}
Subclasses: To use an abstract class, you need to create a subclass that extends the abstract class. The subclass must provide an implementation for all abstract methods defined in the abstract class.
For example:
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
Instance Creation: You cannot create an instance of an abstract class directly. Instead, you must create an instance of a subclass.
For example:
Shape s = new Circle();
s.draw();
Overriding: The subclass can override the methods inherited from the abstract class. Overriding a method means to provide a new implementation for a method inherited from a parent class.
For example:
class Square extends Shape {
void draw() {
System.out.println("Drawing a Square");
}
}
Abstract classes provide a way to define a common interface for related classes and enforce a level of consistency in the implementation of subclasses. They also provide a way to define abstract methods, which must be implemented by subclasses. By following the steps outlined in this guide, you should be able to create an abstract class in Java and use it to create a subclass.
Example of an Abstract Class in Java
Here is a simple example that demonstrates how to create an abstract class and use it to create a subclass:
Main.java
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing a Square");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
s = new Square();
s.draw();
}
}
Explanation:
- The Shape class is declared as an abstract class using the abstract keyword.
- The Shape class contains an abstract method draw() that does not have a body.
- The Circle class extends the Shape class and provides an implementation for the draw() method.
- The Square class also extends the Shape class and provides its own implementation for the draw() method.
- In the Main class, we create an instance of the Circle class and call its draw() method. Then, we create an instance of the Square class and call its draw() method.
Output:
Drawing a Circle
Drawing a Square
In this example, we see how an abstract class can be used to enforce a common interface for related classes. The Shape class defines the draw() method that must be implemented by its subclasses. This ensures that the subclasses provide an implementation for the draw() method and have a consistent behaviour.
Example of an Abstract Class with Concrete Methods
In addition to abstract methods, an abstract class can also contain concrete methods, which have a body and can be called directly. Here is a simple example that demonstrates this concept:
Main.java
abstract class Animal {
abstract void makeSound();
void breathe() {
System.out.println("Breathing...");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Barking...");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meowing...");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
a.breathe();
a = new Cat();
a.makeSound();
a.breathe();
}
}
Output:
Barking...
Breathing...
Meowing...
Breathing...
In this example, we see how an abstract class can have both abstract and concrete methods. The subclasses inherit the concrete method breathe() and can use it directly. This demonstrates the power of abstract classes in providing a common interface and shared behavior for related classes.