Abstraction means hiding the details and visualizing them more simply. In Java, abstraction is a process of hiding the implementation details and providing the functionality by showing only the relevant details like a box with a control panel. We need not worry about what happens inside, all we need to do is provide the input and receive the expected output.
The abstraction can be achieved using an Abstract class and Interface. We will be limiting our discussion to the Abstract class.
A class containing an abstract
keyword in its declaration is known as an abstract
class.
abstract class MyClass{
//may contain abstract and normal methods
//may contain static methods
//may contain final methods
//may contain constructors
}
There are certain properties that such class attains once abstract
is used with it.
abstract
method. However if a class contains even one abstract method, then it has to be mandatorily declared as abstract.The method containing an abstract
keyword in its declaration and has no implementation is known as an abstract
method.
Syntax:
abstract return_type methodName(parameter-list);
Let us look into a series of examples to see all the properties of abstract class in action.
An abstract class cannot be instantiated
abstract class MyAbstractClass{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
public class ClassMain{
public static void main(String args[]){
MyAbstractClass obj = new MyAbstractClass();
}
}
ClassMain.java:13: error: MyAbstractClass is abstract; cannot be instantiated
MyAbstractClass obj = new MyAbstractClass();
^
1 error
An abstract class may or may not contain an abstract method. However if a class contains even one abstract method, then it has to be mandatorily declared as abstract.
Valid Declarations :
abstract class MyAbstractClass{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
}
abstract class MyAbstractClass{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
Invalid Declaration:
Since class contains an abstract method, MyAbstractClass is bound to be abstract
class MyAbstractClass{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
ClassMain.java:1: error: MyAbstractClass is not abstract and does not override abstract method methodAbstract() in MyAbstractClass
class MyAbstractClass{
^
1 error
The class inheriting an abstract class needs to implement the abstract methods inherited. If child class cannot provide the implementation, then child class has to be mandatorily declared as abstract.
1. When implementation is performed inside child class
abstract class MyParentAbstract{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
class MyChildAbstract extends MyParentAbstract{
@Override
void methodAbstract(){
System.out.println("I am abstract method implementation inside MyChildAbstract");
}
}
public class ClassMain{
public static void main(String args[]){
MyChildAbstract obj = new MyChildAbstract();
obj.methodAbstract();
}
}
I am abstract method implementation inside MyChildAbstract
2. When implementation is not performed inside the child class
abstract class MyParentAbstract{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
class MyChildAbstract extends MyParentAbstract{
}
public class ClassMain{
public static void main(String args[]){
MyChildAbstract obj = new MyChildAbstract();
obj.methodAbstract();
}
}
ClassMain.java:10: error: MyChildAbstract is not abstract and does not override abstract method methodAbstract() in MyParentAbstract class MyChildAbstract extends MyParentAbstract{ ^ 1 error
The correction that needs to be done:
The child class needs to be declared abstract as well.
abstract class MyParentAbstract{
//normal method
void methodNormal(){
System.out.println("I am normal method");
}
//abstract method
abstract void methodAbstract();
}
abstract class MyChildAbstract extends MyParentAbstract{
}
This was all about Java abstract classes. We will discuss another aspect of Java Abstraction i.e interface in the next lecture.