Java Abstract Class and Abstract Method
In java, we have a concept called abstraction
is the technique of hiding implementation specifics from the user and only displaying functionality.
In java we use, Abstract
class to acheive 0 to 100% abstraction and we use interface
to acheive 100% abstraction.
Abstract Class
We can create an abstract
class using the abstract
keyword.
The abstract
class can contain both the normal methods and abstract methods.
Java does not allow the instantiation of the abstract class. Means, we cannot create object for an abstract class.
An abstract
class has to be extended by other normal class.
Example for abstract class
abstract class ProgrammingLanguage {
// method of abstract class
public void DisplayLanguage() {
System.out.println("I'm Java Programming Language");
}
}
class Main extends ProgrammingLanguage {
public static void main(String[] args) {
// create an object Main class
Main obj = new Main();
// access method of abstract class method
// using object of Main class
obj.DisplayLanguage();
}
}
Output
I'm Java Programming Language
In the above program, we have an abstract class ProgrammingLanguage
with a method DisplayLanguage()
.
The Main
class will inherit the ProgrammingLanguage
and with the obj
object of the Main
class we call the DisplayLanguage()
method of the abstract class ProgrammingLanguage
.
An abstract class can have constructor of its own which can be
invoked by the sub-class using thesuper
keyword.
Abstract Method
An abstract
method does not contain any implementation or body for the method. An abstract
method can be allowed only inside the abstract
class.
Any class that inherits the abstract
class has to implement the abstract
method(s). To make a method abstract, we use the abstract
keyword.
Syntax for abstract method
abstract return_type method_name();
Example to declare abstract method
abstract void DisplayInfo();
Example program for Abstract Method
abstract class Animal {
abstract void Sound();
public void Category() {
System.out.println("I am an animal.");
}
}
class Cat extends Animal {
public void Sound() {
System.out.println("My sound is meow");
}
}
class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.Category();
cat.Sound();
}
}
Output
I am an animal
My sound is meow
Here, we have an abstract class Animal
, and this class contains an abstract method Sound()
and a normal method Category()
.
The Cat
class inherits the Animal
class and implements the Sound()
method.
Now it the Main
class we create an object for the Cat
class and call the Category()
and the Sound()
method.
Real-world example for Abstract Class
// Abstract Class
abstract class Bike {
Bike() {
System.out.println("I am a Bike");
}
// Abstract method
abstract void totalGears();
void runningStatus() {
System.out.println("Safe to ride now");
}
}
// Inherit the abstract class Bike
class Honda extends Bike {
// Implement abstract method
void totalGears() {
System.out.println("Total of 5 gears");
}
}
class Main {
public static void main(String args[]) {
// Create instance of Honda class
Bike bike = new Honda();
bike.totalGears();
bike.runningStatus();
}
}
Output
I am a Bike
Total of 5 gears
Safe to ride now