Abstract class


Abstract class


Abstract class in Java can be created using “abstract” keyword.

If we make any class as abstract then it can’t be instantiated which means we are not able to create the object of abstract class.

Inside Abstract class, we can declare abstract methods as well as concrete methods

So using abstract class, we can achieve 0 to 100 % abstraction.

Example:

1
2
3
4
Abstract class Phone{
Abstract void receiveCall();
Abstract void sendMessage();
}
Abstract class Phone{
Abstract void receiveCall();
Abstract void sendMessage();
}


Abstract method

A method that is declared as abstract and does not have implementation is called abstract method.

Now any concrete class which extends the above abstract class will provide the definition of these abstract methods.

This definition is the actual internal details of how this functionality has been implemented.

1
2
3
4
5
6
7
8
9
10
class Samsung extends Phone{
public void receiveCall(){
System.out.println("Call received in Samsung");
}
 
public void sendMessage(){
System.out.println("Message sent in Samsung");
}
 
}
class Samsung extends Phone{
public void receiveCall(){
System.out.println("Call received in Samsung");
}

public void sendMessage(){
System.out.println("Message sent in Samsung");
}

}


Anyone who needs to access this functionality has to call the method using the Phone object pointing to its subclass.

1
2
3
4
5
6
7
public class AbstractTest{
public static void main(String[] args) {
Phone samsung = new Samsung();
samsung.receiveCall();
samsung.sendMessage();
}
}
public class AbstractTest{
public static void main(String[] args) {
Phone samsung = new Samsung();
samsung.receiveCall();
samsung.sendMessage();
}
}



Samsung class has provided the concrete definition for abstract methods declared inside Phone abstract class.

Constructor and data members in Abstract class


An abstract class can have fields, abstract methods, concrete methods, constructors and even main() method.

Example:

Vehicle.java

1
2
3
4
5
6
7
8
9
10
11
abstract class Vehicle{
String regNo;
vehicle(){
System.out.println("Creating Vehicle");      
}
public abstract void start();
public abstract void stop();
public void display(){
System.out.println("Vehicle reg no : "+regNo);
}
}
abstract class Vehicle{
String regNo;
vehicle(){
System.out.println("Creating Vehicle");      
}
public abstract void start();
public abstract void stop();
public void display(){
System.out.println("Vehicle reg no : "+regNo);
}
}


Now below class is a concrete class which extends the Vehicle class and provide the implementation of all abstract methods.

TwoWheeler.java

1
2
3
4
5
6
7
8
9
10
11
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    
 
 @Override  
public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
} 
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    

 @Override  
public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
} 


VehicleManager.java

1
2
3
4
5
6
7
8
9
10
11
public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle twoWheeler = new TwoWheeler();
twoWheeler.regNo="HR01 Q2125";
        
        twoWheeler.start();  
        twoWheeler.stop();  
        twoWheeler.display();
      
    }  
}  
public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle twoWheeler = new TwoWheeler();
twoWheeler.regNo="HR01 Q2125";
        
        twoWheeler.start();  
        twoWheeler.stop();  
        twoWheeler.display();
      
    }  
}  



Rules for abstract class

1) If a class has any one method declared as abstract, then that class must be defined as abstract class

Example
1
2
3
4
5
6
class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}
class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}


Above program will fail during compile time as its not declared as abstract.

So make it as below

1
2
3
4
5
6
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}

2) Whenever we are extending any abstract class that has abstract method, we should either provide the implementation of the method or make this subclass also as abstract

Example
1
2
3
4
5
6
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}


class extending above abstract class and providing implementation of abstract method

1
2
3
4
5
6
7
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    
 
}  
class TwoWheeler extends Vehicle{  
    @Override  
    public void start() {  
        System.out.println("Two wheeler start logic");       
  }    

}  


class extending above abstract class and not providing implementation of abstract method

1
2
3
4
5
class TwoWheeler extends Vehicle{  
   public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
} 
class TwoWheeler extends Vehicle{  
   public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
} 


Above program will fail during compile time as this class does not provide implementation for abstract method and not defined as abstract class.

Hence we must define this class as abstract as below

1
2
3
4
5
abstract class TwoWheeler extends Vehicle{  
   public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
}
abstract class TwoWheeler extends Vehicle{  
   public void stop() {  
        System.out.println("Two Wheeler stop logic");       
  }      
}

3) We can never ever instantiate the abstract class which means we can not create the object for abstract class.If you try to create an object for abstract class then it will give compile time error.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}
 
public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle vehicle= new Vehicle();  
 
}
 
}
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}

public class VehicleManager{  
    public static void main(String[] args) {  
        Vehicle vehicle= new Vehicle();  

}

}


Above program will fail in compile time as we are trying to instantiate the Abstract class.

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