Before we start with abstract classes, let us first understand what abstraction actually means in Java.
What is Abstraction?
Let us take an example of our daily life. Whenever we use a mobile phone to make a call to somebody, we simply pickup our phone and dial the number of that person and the phone of the other person starts ringing. Did you ever need to know the details of how the call was actually being made? The answer to this question would be a NO for sure. This process of hiding the details of how the call is being made using protocols is called abstraction. Likewise in object oriented programming, abstraction plays an important role.
Definition of Abstraction
Abstraction here refers to hiding of the details of the implementation from the users and showing only the functionality to the user as the users need to know the functional details only.
Hence we can summarize it as the user will have information on what the object does instead of how it is done.
Ways to achieve Abstraction
In Java abstraction is achieved by using Abstract Classes and interfaces.
Java Abstract Classes
A Java abstract class is a class which is declared using the abstract keyword.
The following rules have to be followed to declare any class as an abstract class:
- An abstract class must be declared with the abstract keyword.
- An abstract class cannot be instantiated i.e. new instances of an abstract class cannot be created by us.
- It can contain constructors and static members.
- It can have abstract and non-abstract methods in it. Abstract methods are the methods without body. For example: public void get();
- If there are abstract methods in a class then that class must be declared abstract as a normal class cannot contain abstract methods.
- In order to use the abstract class, we have to inherit it from another class and provide the implementation of all the abstract methods in it.
Why do we need an abstract class?
In a program when we know that the functionality or implementation of a particular function declared in the parent class will be overridden by every child class in their respective manner, we declare that method as abstract. Once the class has an abstract method, the class is also declared as abstract.
In other words, we can also say that when we want any method to be implemented by each and every child class, we make that method abstract. If the programmer at any point in the program does not implement the abstract method then a compilation error will come, making it necessary to be implemented in the child classes.
Declaring a Java Abstract Class
A Java abstract class is declared using the abstract keyword. This is done as follows:
abstract class class_name{
// Class Body
}
Abstract Methods in Java
A method which is declared abstract and does not have an implementation in the parent class is known as an abstract method.
The following rules have to be followed to declare any method of an abstract class as an abstract method:
- The abstract keyword must be prefixed to the declaration of the method.
- An abstract method contains only the method signature but no body.
In place of curly braces, an abstract method will have a semi colon at the end.
Declaring Java Abstract Methods
This is done as shown in this syntax:
abstract class class_name
{
abstract void method1(); // This is an abstract method
void method2() // A Normal method with a concrete body
{
// body of method
}
}
Why we cannot create objects of an abstract class?
As abstract classes act like a template which needs to be extended and implemented before use, these classes are referred as incomplete. Now, abstract classes contain abstract functions which do not have a body (i.e. a definition) so if we create an object of such a class and the object invokes this function then there would be no implementation which would be invoked. As an object is concrete, this would not be allowed and a compilation error will be shown if we do so. Hence, abstract classes cannot have an object of its own.
An example
abstract class shape // Abstract Class { abstract void draw(); // Abstract Method } class rect extends shape { void draw() // Implementation 1 of the abstract method { System.out.println("Draw Rectangle"); } } class circle extends shape { void draw() // Implementation 2 of the abstract method { System.out.println("Draw Circle"); } } class abstractEg { public static void main(String args[]) { shape s = new circle(); s.draw(); shape r = new rect(); r.draw(); } }