Abstract keyword in Java

Abstract in java is used to represent that there is no concrete implementation.

The abstract keyword in Java is used to declare a class or a method as abstract.

An " abstract class " can have abstract methods which have to be implemented by its concrete sub classes.

An " abstract method " does not have concrete implementation or body, and must ends with a semicolon.


The following example declares a class “Employee” as abstract:

1
2
3
4
public abstract class Employee {
 
    public abstract void job();
}
public abstract class Employee {

    public abstract void job();
}


In the above example, we can see that class “Employee” is declared as abstract and also method “job()” is declared as abstract.

Rules for Abstract class and Abstract Methods in Java


An abstract method:


Will not have body (method definition) and should end with a semicolon.

Keyword abstract should be used for declaring it.

Should be implemented by concrete sub classes.

Mandatorily we must declare the enclosing class as abstract class


An abstract class:

We can’t instantiate or create an object for the abstract class.

The purpose of an abstract class is to keep common generic methods for a set of related sub-classes and make those sub-classes to provide the 

specific method definition.

In abstract classes we Can have both abstract and concrete methods.

Example:
Now any class extending “Employee” abstract class has to provide the method definition to qualify as concrete class.

Create Developer.java

1
2
3
4
5
6
7
8
public class Developer extends Employee {
 
    public void job(){
                            
                              //Do coding based on requirement
 
                     }
}
public class Developer extends Employee {

    public void job(){
                            
                              //Do coding based on requirement

                     }
}


Create Tester.java

1
2
3
4
5
6
7
public class Tester extends Employee {
 
    public void job(){
                               
                             //Do testing of application
                     }
}
public class Tester extends Employee {

    public void job(){
                               
                             //Do testing of application
                     }
}


Abstract class can have both abstract and concrete methods

Example:

1
2
3
4
5
6
7
8
9
public abstract class Employee {
 
    public abstract void job();
 
    public void resign(){
 
                                            //Follow same resignation process for all employees
                         }
}
public abstract class Employee {

    public abstract void job();

    public void resign(){

                                            //Follow same resignation process for all employees
                         }
}


Abstract methods cannot be declared as static and final.

Both the below examples are not valid in Java

static abstract example

1
2
3
4
public abstract class Employee {
 
    public static abstract void job();
}
public abstract class Employee {

    public static abstract void job();
}


final abstract example

1
2
3
4
public abstract class Employee {
 
    public final abstract void job();
}
public abstract class Employee {

    public final abstract void job();
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on