Abstract Methods and Classes in Java – In this Java Tutorial, we shall see one of the ways to implement Abstraction in Java using abstract methods and classes.
Abstract Methods and Classes in Java
Abstract method
An abstract method has only declaration part but no implementation or definition is provided.
An example of declaring an abstract method is as following.
public abstract void printMessage();
Abstract class
An abstract class is a class that is declared abstract. And abstract class can have none or one or more abstract methods. But if there is at least one abstract method in a class, the class has to be declared abstract. And an imported point to be noted is that an abstract class can never be instantiated, i.e., an object of abstract class type can never be created.
An abstract class can only be a super class for other normal classes or other abstract classes.
An example of an abstract class, and a sub-class that extends the abstract class, and an abstract class extending the abstract class will be shown below:
Example 1 – Abstract Class
Vehicle is an abstract class with abstract methods accelerate() and breaking().
Vehicle.java
public abstract class Vehicle {
public abstract void accelerate();
public abstract void breaking();
}
Example 2 – Extend Abstract Class
We can extend an abstract class with another abstract class. Sub-class extends super class.
In the following program, Car is an abstract class, extending another abstract class, Vehicle, and has another abstract method mode().
Car.java
public abstract class Car extends Vehicle {
@Override
public abstract void accelerate();
@Override
public abstract void breaking();
public abstract void mode();
}
Example 3 – Class Extending Abstract Class
We can extend a class with an abstract class. In such cases, we have to implement all the abstract methods and properties of the abstract class.
In this following program, Jaguar is a class extending abstract class Car and implementing all the abstract methods in Car.
Jaguar.java
public class Jaguar extends Car {
String name;
public Jaguar(String name){
this.name = name;
}
@Override
public void accelerate() {
System.out.println(name + " is accelerating.");
}
@Override
public void breaking() {
System.out.println(name + " is accelerating.");
}
@Override
public void mode() {
System.out.println("The "+name+" is in sports mode.");
}
}
AbstractClassDemo.java
/**
* This class provides a demo for Abstract class and methods.
* @author tutorialkart.com
*
*/
public class AbstractClassDemo {
public static void main(String[] args) {
System.out.println("---Demo on Interfaces in Java---");
Jaguar myNewJaguar = new Jaguar("XJ");
myNewJaguar.accelerate();
myNewJaguar.breaking();
myNewJaguar.mode();
}
}
Output
---Demo on Interfaces in Java---
XJ is accelerating.
XJ is accelerating.
The XJ is in sports mode.
Conclusion
In this Java Tutorial, we have completed the ways of implementing abstraction in Java. In our next Java tutorial, we shall learn Encapsulation in Java.