Java Abstract Class and Abstract Methods
Java Abstract Class and Abstract Methods
Methods
In this tutorial, we will learn about Java abstract classes and methods with the
help of examples. We will also learn about abstraction in Java.
An abstract class can have both the regular methods and abstract methods.
For example,
// abstract method
abstract void method1();
// regular method
void method2() {
System.out.println("This is regular method");
}
}
To know about the non-abstract methods, visit Java methods. Here, we will
learn about abstract methods.
// error
// class should be abstract
class Language {
// abstract method
abstract void method1();
}
Output
In the above example, we have created an abstract class named Language . The
class contains a regular method display() .
We have created the Main class that inherits the abstract class. Notice the
statement,
obj.display();
Here, obj is the object of the child class Main . We are calling the method of the
abstract class using the object obj .
class Main {
public static void main(String[] args) {
d1.makeSound();
d1.eat();
}
}
Run Code
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() .
Note: If the Dog class doesn't provide the implementation of the abstract
method makeSound() , Dog should also be declared as abstract. This is because
the subclass Dog inherits makeSound() from Animal .
An abstract class can have constructors like the regular class. And, we can
access the constructor of an abstract class from the subclass using
the super keyword. For example,
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.
Java Abstraction
The major use of abstract classes and methods is to achieve abstraction in
Java.
The major advantage of hiding the working of the brake is that now the
manufacturer can implement brake differently for different motorbikes,
however, what brake does will be the same.
Let's take an example that helps us to better understand Java abstraction.
class Main {
public static void main(String[] args) {
MountainBike m1 = new MountainBike();
m1.brake();
SportsBike s1 = new SportsBike();
s1.brake();
}
}
Run Code
Output:
MountainBike Brake
SportsBike Brake
In the above example, we have created an abstract super class MotorBike . The
superclass MotorBike has an abstract method brake() .
The brake() method cannot be implemented inside MotorBike . It is because
every bike has different implementation of brakes. So, all the subclasses
of MotorBike would have different implementation of brake() .
REFERENCES: https://www.programiz.com/java-programming/abstract-classes-methods