Why do we use abstract class in Java?
Abstract classes are one of the fundamental concepts in Java programming language and play a vital role in object-oriented programming (OOP). They allow for code reuse and provide a blueprint for creating new classes. In this article, we will explore the concept of abstract classes in Java, why they are used, and when to use them.
What is an Abstract Class?
An abstract class is a class that cannot be instantiated on its own, but must be inherited by a subclass. It acts as a blueprint for other classes and can contain abstract methods (methods without a body) and concrete methods (methods with a body). An abstract class can also contain instance variables, constructors, and final methods (methods that cannot be overridden by subclasses).
Why Use Abstract Classes?
Code Reusability: Abstract classes provide a way to share common code between subclasses. By using an abstract class, you can define methods and fields that will be inherited by all subclasses, reducing the need to duplicate code in each subclass.
Improved Organization: Abstract classes allow you to group related methods and fields together, making the code more organized and easier to understand.
Method Overriding: An abstract class can contain abstract methods, which must be overridden by subclasses. This allows for greater flexibility in how the methods are implemented and provides a way to enforce a specific implementation in subclasses.
Improved Type Safety: By using abstract classes, you can ensure that only objects of the correct type are passed to a method. For example, if you have an abstract class called Shape, you can declare a method that takes a Shape object as an argument, and only objects that are subclasses of Shape will be accepted as arguments.
When to Use Abstract Classes?
When you want to share code among several related classes.
When you want to enforce a specific implementation in subclasses.
When you want to declare non-static or non-final fields.
When you want to provide a method that must be overridden in subclasses.
Example of Abstract Class in Java
Consider a scenario where you have a class hierarchy for different types of animals. You could define an abstract class called Animal, which contains common properties such as age, weight, and height.
File name : Animal.java
abstract class Animal {
private int age;
private int weight;
private int height;
public Animal(int age, int weight, int height) {
this.age = age;
this.weight = weight;
this.height = height;
}
public abstract void makeSound();
public int getAge() {
return age;
}
public int getWeight() {
return weight;
}
public int getHeight() {
return height;
}
}
Next, you could create concrete subclasses for specific types of animals, such as Dog, Cat, and Horse. Each of these subclasses would inherit the properties and methods defined in the Animal class, but would also provide their own implementation of the makeSound method.
class Dog extends Animal {
public Dog(int age, int weight, int height) {
super(age, weight, height);
}
@Override
public void makeSound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
public Cat(int age, int weight, int height) {
super(age, weight, , height);
}
@Override
public void makeSound() {
System.out.println("Meow");
}
}
class Horse extends Animal {
public Horse(int age, int weight, int height) {
super(age, weight, height);
}
@Override
public void makeSound() {
System.out.println("Neigh");
}
}
The output of the code would depend on how the classes and objects are used in the program.
Output:
Animal dog = new Dog(5, 50, 60);
dog.makeSound(); // Output: Bark
Animal cat = new Cat(3, 20, 30);
cat.makeSound(); // Output: Meow
Animal horse = new Horse(7, 100, 120);
horse.makeSound(); // Output: Neigh
By using an abstract class in this example, we can share common properties and methods among all animal subclasses, while still allowing for specific implementations for each type of animal.
Conclusion
Abstract classes are an important concept in Java programming language and provide a way to share code and enforce implementation among related classes. They offer improved organization, type safety, and method overriding capabilities, making them an essential tool for object-oriented programming. Understanding when and how to use abstract classes can help you write cleaner, more organized, and more maintainable code.