abstract Modifier in Java
Introduction
The abstract modifier in Java is applicable for classes and methods but not for variables. It is used to achieve Abstraction, one of the key concepts in Object-Oriented Programming.
Abstract Methods
Even if we don't know the implementation, we can still declare a method with the abstract modifier, which means that only declaration, not implementation, is available for abstract methods. As a result, abstract methods should be terminated by a semi-colon.
public abstract void m1();
public abstract void m2() {}
In the above code snippet, the declaration of method m1()
is correct whereas that of m2()
is incorrect.
Child class(the class that extends the parent class) is responsible for providing the implementation of parent class abstract methods.
Consider a Vehicle as a real-world application of abstract methods. Varied vehicles have different numbers of wheels, such as a car with four wheels, an auto with three wheels, and so on. Let us create a Vehicle
class and define a getNumberOfWheels()
method inside it.
public abstract class Vehicle {
public abstract int getNumberOfWheels();
}
class Car extends Vehicle {
@Override
public int getNumberOfWheels() {
return 4;
}
}
class Auto extends Vehicle {
@Override
public int getNumberOfWheels() {
return 3;
}
}
We provide instructions to the child class by declaring method abstract in the parent class that these methods must be implemented by the child class.
Note:
Abstract methods never talk about implementation. If any modifier talks about implementation, then it forms an illegal combination with the abstract modifier.
The following are various illegal combinations of modifiers for methods with respect to the abstract modifier.
For example:
public abstract class Test {
abstract final void m1();
}
Output:
Abstract Class
If we are not allowed to construct an object for any Java class (due to partial implementation), we must declare such classes with the abstract modifier, i.e., object instantiation is not possible for abstract classes.
public abstract class Test {
public static void main(String[] args) {
Test t = new Test();
}
}
Output:
Conclusions about Abstract Class and Abstract Method
- If a class has at least one abstract method, it must be declared as an abstract class; otherwise, a compile-time error will occur.
- If a class contains at least one abstract method, then its implementation is incomplete and hence we cannot create objects of that class. To restrict object creation, the class must be declared as abstract class.
- Even if a class doesn't have any abstract methods, we can designate it as an abstract class if we want to restrict object creation. An abstract class can have zero abstract methods.
e.g. HttpServlet
class is abstract but it doesn't contain any abstract method.
Examples
1.
public class P {
public void m1();
}
On compilation:
2.
public class P {
public abstract void m1();
}
On compilation:
3.
public class P {
public abstract void m1(){
}
}
On compilation:
4.
public abstract class P {
public abstract void m1();
public abstract void m2();
}
class C extends P {
public void m1() {
System.out.println("Implementation of m1");
}
}
On compilation:
jIn the above example, we can see that if we're extending an abstract class, we must implement each and every abstract method of the parent class in the child class.
final and abstract
Abstract methods must be overridden in the child class to provide implementation whereas we cannot override final methods. Hence abstract final method
forms illegal combination.
For final
classes, we cannot create child classes whereas, for abstract classes, we have to create child classes to provide the implementation of the abstract methods. Hence, final abstract
combination is illegal at the class level too.
The final class cannot contain abstract methods whereas the abstract class can contain final methods.
final class P {
public abstract void m1();
}
On compilation, the above code snippet throws an error:
abstract class P {
public final void m1() {
}
}
The above code snippet doesn't throw any error.
Conclusion
In this blog, we learned about abstract modifier. We saw how we can use abstract modifier with methods and classes. Apart from that we also discussed the illegal combination of final and abstract modifiers.
Thanks for reading!