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. abstract class Phone{
  2. abstract void receiveCall();
  3. abstract void sendMessage();
  4. }
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. class Samsung extends Phone{
  2. public void receiveCall(){
  3. System.out.println("Call received in Samsung");
  4. }
  5.  
  6. public void sendMessage(){
  7. System.out.println("Message sent in Samsung");
  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");
}

}


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

  1. public class AbstractTest{
  2. public static void main(String[] args) {
  3. Phone samsung = new Samsung();
  4. samsung.receiveCall();
  5. samsung.sendMessage();
  6. }
  7. }
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. abstract class Vehicle{
  2. String regNo;
  3. vehicle(){
  4. System.out.println("Creating Vehicle");      
  5. }
  6. public abstract void start();
  7. public abstract void stop();
  8. public void display(){
  9. System.out.println("Vehicle reg no : "+regNo);
  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);
}
}


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

TwoWheeler.java

  1. class TwoWheeler extends Vehicle{  
  2.     @Override  
  3.     public void start() {  
  4.         System.out.println("Two wheeler start logic");      
  5.   }    
  6.  
  7.  @Override  
  8. public void stop() {  
  9.         System.out.println("Two Wheeler stop logic");      
  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");       
  }      
} 


VehicleManager.java

  1. public class VehicleManager{  
  2.     public static void main(String[] args) {  
  3.         Vehicle twoWheeler = new TwoWheeler();
  4. twoWheeler.regNo="HR01 Q2125";
  5.        
  6.         twoWheeler.start();  
  7.         twoWheeler.stop();  
  8.         twoWheeler.display();
  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();
      
    }  
}  



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. class Vehicle{
  2. abstract void start();
  3. void display(){
  4. System.out.println("Vehicle");
  5. }
  6. }
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. abstract class Vehicle{
  2. abstract void start();
  3. void display(){
  4. System.out.println("Vehicle");
  5. }
  6. }
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. abstract class Vehicle{
  2. abstract void start();
  3. void display(){
  4. System.out.println("Vehicle");
  5. }
  6. }
abstract class Vehicle{
abstract void start();
void display(){
System.out.println("Vehicle"); 
}
}


class extending above abstract class and providing implementation of abstract method

  1. class TwoWheeler extends Vehicle{  
  2.     @Override  
  3.     public void start() {  
  4.         System.out.println("Two wheeler start logic");      
  5.   }    
  6.  
  7. }  
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. class TwoWheeler extends Vehicle{  
  2.    public void stop() {  
  3.         System.out.println("Two Wheeler stop logic");      
  4.   }      
  5. }
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. abstract class TwoWheeler extends Vehicle{  
  2.    public void stop() {  
  3.         System.out.println("Two Wheeler stop logic");      
  4.   }      
  5. }
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. abstract class Vehicle{
  2. abstract void start();
  3. void display(){
  4. System.out.println("Vehicle");
  5. }
  6. }
  7.  
  8. public class VehicleManager{  
  9.     public static void main(String[] args) {  
  10.         Vehicle vehicle= new Vehicle();  
  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();  

}

}


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

About the Author

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