Abstract Classes And Methods In Java
In this tutorial, you will learn about abstraction in Java and learn about abstract Java classes and methods and how to use them in the program.
Java abstract class
An abstract class is a class that cannot be introduced (we cannot create an object from an abstract class). In Java, we use the abstract keyword to define an abstract class.
- abstract class Animal {
- // attributes and methods
- }
If we try to create an object from the abstract class, the compiler gets an error. For example :
Animal a1 = new Animal ()
Which causes a compiler error:
Animal is abstract; cannot be instantiated
Although abstract classes cannot be defined, we can create objects from subclasses and subclasses to access abstract class members.
Before we talk about them in detail, we need to understand abstract methods.
Abstract Java method
We use the same abstract keyword to create abstract methods. An abstract method is defined without execution. For example:
- abstract void makeSound ();
Here, makeSound () is an abstract method. Body of makeSound () with ; is already replaced.
It is important to note that only one abstract class can contain abstract methods. If we put abstract methods inside a class that is not abstract, we get an error.
An abstract class can include both abstract and non-abstract methods. Consider the following example:
- abstract class Animal {
- public void displayInfo () {
- System.out.println (“I am an animal.”);
- }
- abstract void makeSound ();
- }
In the example above, we created an abstract Animal class that includes an abstract makeSound () method and a non-abstract displayInfo () method.
Inheritance from the abstract class
The abstract class cannot be introduced. To access members of an abstract class, we must inherit from it.
Example 1: Inheritance from the 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 example above, we created an abstract Animal class. But we can not create an object from Animal. To access displayInfo (), we created a Dog subclass of Animal (inherited from the Animal class)
Then we used the Dog d1 object to access the displayInfo () method.
Overriding (abstracting methods of inherited methods) Abstract methods
In Java, overriding superclass abstract methods is mandatory under the class. Because the subclass inherits the superclass abstract methods.
Since the subclass contains abstract methods, we must re-implement them.
Note: Overriding is not mandatory if abstract subclasses are also defined.
Example 2: Overriding an 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 example above, we have created an abstract Animal class. This class includes an abstract makeSound () method and a non-abstract eat () method.
The Dog subclass inherits from the Animal superclass. Here, the Dog subclass re-implements the displayInfo () abstract method.
Then we created a d1 object from Dog. Using the object, we called the methods d1.displayInfo () and d1.eat ().
Access the abstract class builder
Similar to non-abstract classes, we use super key to access the constructor of an abstract class below the class. For example,
- abstract class Animal {
- Animal () {
- ….
- }
- }
- class Dog extends Animal {
- Dog () {
- super ();
- …
- }
- }
Here, we used super () in the Dog constructor to access the Animal constructor.
Note that super should always be the first constructive sentence of the subclass.
Why Java Abstract?
Abstract is an important concept of object-oriented programming. The abstraction shows only the required information and all unnecessary details are hidden. This allows us to manage complexity by removing or hiding details with a simpler, higher-level idea.
A practical example of abstraction is motorcycle brakes. We know what the brake does. When we brake, the engine stops. However, the work of the brakes is kept secret from us.
The most important advantage of hiding brake work is that the manufacturer can now apply the brake differently for different engines, however, what the brake does is the same.
Let’s take an example to help us 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 example above, we have created an Animal superclass. Animal has the abstract makeSound () method.
The makeSound () method is not executable in Animal. Because each animal makes different sounds. Therefore, all Animal subclasses have a different execution of makeSound ().
We cannot execute makeSound () in Animal in such a way that it can be true for all its subclasses. Therefore, the makeSound () execution in Animal is kept secret.
In the example above, Dog executes from makeSound () and Cat performs its own execution from makeSound ().
Key points to remember
- We use the abstract keyword to create abstract classes and methods.
- An abstract method has no implementation (method body).
- The class containing abstract methods must also be abstract.
- We can not create objects from the abstract class.
- To implement the properties of an abstract class, subclasses inherit from it and create an object from the subclass.
- A subclass must ignore (re-implement) all abstract methods of an abstract class. But if an abstract subclass is defined, ignoring abstract methods is not mandatory.
- We can access the static properties and methods of an abstract class using the abstract class reference. For example :
- Animal.staticMethod ();