In this article, you will learn-
Java Abstract Class and Abstract Methods
In this tutorial, we will find out about Abstraction in Java. We will find out about Java abstract classes and methods and how to use them in our program.
Java Abstract Class
An abstract class is a class that can’t be started up (we can’t make objects of an abstract class). In Java, we use the abstract catchphrase to pronounce an abstracy class.
abstract class Animal {
//attributes and methods
}
If we try to create objects of an abstract class, we will get a compilation error. For example,
Animal a1 = new Animal()
It will generate a compilation error:
Animal is abstract; cannot be instantiated
In spite of the fact that abstract classes can’t be instantiated, we can make subclasses from it. We can make objects of subclasses to get to members from the abstract class.
Before we find out about it in detail, we need to understand abstract methods.
Java Abstract Method
We use a similar watchword abstract to make abstract methods. An abstract method is announced without an execution. For instance,
abstract void makeSound();
Here, makeSound() is an abstract method. The body of makeSound() is replaced by ;.
It’s important to take note that, only an abstract class can contain abstract methods. If we incorporate abstract methods inside a class that isn’t abstract, we will get a mistake.
An abstract class can contain both abstract and non-abstract methods. Here’s an example.
abstract class Animal {
public void displayInfo() {
System.out.println(“I am an animal.”);
}
abstract void makeSound();
}
In the above example, we have created an abstract class Animal. It contains an abstract method makeSound() and a non-abstract method displayInfo().
Inheritance of Abstract Class
An abstract class can’t be started up. To get to the members of an abstract class, we should acquire it. For instance
Example 1: Inheritance of Abstract Class
abstract class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output
I am an animal.
In the above example, we have created an abstract class Animal. We cannot create objects of Animal. To access the displayInfo() of Animal, we have inherited a subclass Dog of Animal.
We then used the d1 object of Dog to access the method displayInfo().
Overriding of Abstract Methods
In Java, it is required to supersede abstract methods of the superclass in the subclass. It is on the grounds that the subclass acquires abstract methods of the superclass.
Since our subclass incorporates abstract methods, we need to override them.
Note: If the subclass is additionally pronounced abstract, it’s not compulsory to override abstract methods.
Example 2: Overriding Abstract Method
abstract class Animal {
abstract void makeSound();
public void eat() {
System.out.println("I can eat.");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Bark bark");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.makeSound();
d1.eat();
}
}
Output
Bark bark.
I can eat.
In the above example, we have created an abstract class Animal. The class contains an abstract method makeSound() and a non-abstract method eat().
We have inherited a subclass Dog from the superclass Animal. Here, the subclass Dog provides the implementation for the abstract method makeSound().
We then created an object d1 of Dog. Using the object, we then called d1.makeSound() and d1.eat() methods.
Accesses Constructor of Abstract Classes
Like non-abstract classes, we access the constructor of an abstract class from the subclass using the super catchphrase. For instance,
abstract class Animal {
Animal() {
….
}
}
class Dog extends Animal {
Dog() {
super();
...
}
}
Here, we have used the super() inside the constructor of Dog to access the constructor of the Animal.
Note that the super should always be the first statement of the subclass constructor. Visit Java super keyword to learn more.
Why Java Abstraction?
Abstraction is a significant concept of object-oriented programming. Abstraction just shows the required data and all the superfluous subtleties are kept covered up. This allows us to oversee unpredictability by discarding or concealing subtleties with a less complex, more elevated level of thought.
A practical example of abstraction can be motorbike brakes. We recognize what brake does. At the point when we apply the brake, the motorbike will stop. Notwithstanding, the working of the brake is kept escaped us.
The major advantage of hiding the working of the brake is that now the producer can execute brake diversely for various motorbikes, notwithstanding, what brake will be the equivalent.
Let’s take an example that helps us to better understand Java abstraction.
Example 3: Java Abstraction
abstract class Animal {
abstract void makeSound();
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Bark bark.");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meows ");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.makeSound();
Cat c1 = new Cat();
c1.makeSound();
}
}
Output
Bark bark
Meows
In the above example, we have created a superclass Animal. The superclass Animal has an abstract method makeSound().
The makeSound() method cannot be implemented inside Animal. It is because every animal makes different sounds. So, all the subclasses of Animal would have different implementation of makeSound().
We cannot implement makeSound() in Animal in such a way that it can be correct for all subclasses of Animal. So, the implementation of makeSound() in Animal is kept hidden.
In the above example, Dog makes its own implementation of makeSound() and Cat makes its own implementation of makeSound().
Key Points to Remember
- We use abstract watchword to make abstract classes and methods.
- An abstract method doesn’t have any usage (method body).
- A class containing abstract methods ought to likewise be abstract.
- We can’t make objects of an abstract class.
- To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.
- A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it’s not mandatory to override abstract methods.
- We can access the static attributes and methods of an abstract class using the reference of the abstract class. For example,
Animal.staticMethod();
Java Interface
In Java, an interface is like an abstract class. However, interfaces don’t have any non-abstract methods. We will learn more about interfaces in the next tutorial.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.