abstract keyword (Diff abstract class and Interface)

The abstract keyword is a non-access modifier, used for classes and methods.

Class: An abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Method: An abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Rules of abstract keyword

Don’ts

  • An abstract keyword cannot be used with variables and constructors.
  • If a class is abstract, it cannot be instantiated.
  • If a method is abstract, it doesn’t contain the body.
  • We cannot use the abstract keyword with the final.
  • We cannot declare abstract methods as private.
  • We cannot declare abstract methods as static.
  • An abstract method can’t be synchronized.

Do’s

  • An abstract keyword can only be used with class and method.
  • An abstract class can contain constructors and static methods.
  • If a class extends the abstract class, it must also implement at least one of the abstract method.
  • An abstract class can contain the main method and the final method.
  • An abstract class can contain overloaded abstract methods.
  • We can declare the local inner class as abstract.
  • We can declare the abstract method with a throw clause.

InterfaceAbstract Class
No ConstructorConstructor Allowed
Non Static method not allowedNon Static methods Allowed
Non Final method not allowedNon Final methods llowed
By default global variables as public static finalBy default they are package-private, non static and no fianl
Default methods are allowed in InterfaceDefault keyword only applicable for Interface

Examples of abstract Keyword

Example 1: Abstract class containing the abstract method

  1. abstract class Vehicle  
  2. {  
  3.     abstract void bike();  
  4.       
  5. }  
  6. class Honda extends Vehicle  
  7. {  
  8.   
  9.     @Override  
  10.     void bike() {  
  11.         System.out.println(“Bike is running”);  
  12.       
  13.     }  
  14.       
  15. }  
  16.   
  17. public class AbstractExample1 {  
  18.   
  19.     public static void main(String[] args) {  
  20.   
  21.     Honda obj=new Honda();  
  22.     obj.bike();  
  23.     }         
  24. }  

Test it Now

Output:

Bike is running

Example 2: Abstract class containing the abstract and non-abstract method

  1. abstract class Vehicle  
  2. {  
  3.     abstract void bike();  
  4.       
  5.     void car()  
  6.     {  
  7.         System.out.println(“Car is running”);  
  8.     }  
  9.       
  10. }  
  11. class Honda extends Vehicle  
  12. {  
  13.   
  14.     @Override  
  15.     void bike() {  
  16.         System.out.println(“Bike is running”);  
  17.       
  18.     }  
  19.       
  20. }  
  21.   
  22. public class AbstractExample2 {  
  23.   
  24.     public static void main(String[] args) {  
  25.   
  26.     Honda obj=new Honda();  
  27.     obj.bike();  
  28.     obj.car();  
  29.       
  30.     }     
  31.       
  32. }  

Test it Now

Output:

Bike is running
Car is running

Example 3: Abstract class containing the constructor

  1. abstract class Vehicle  
  2. {  
  3.     String msg;  
  4.       
  5.     Vehicle(String msg)  
  6.     {  
  7.     this.msg=msg;  
  8.     }  
  9.       
  10.     void display()  
  11.     {  
  12.         System.out.println(msg);  
  13.     }  
  14.       
  15. }  
  16. class Honda extends Vehicle  
  17. {  
  18.   
  19.     Honda(String msg) {  
  20.         super(msg);  
  21.           
  22.     }  
  23.   
  24. }  
  25.   
  26. public class AbstractExample3 {  
  27.   
  28.     public static void main(String[] args) {  
  29.   
  30.     Honda obj=new Honda(“Constructor is invoked”);  
  31.     obj.display();  
  32.       
  33.     }         
  34. }  

Test it Now

Output:

Constructor is invoked

Example 4: Abstract class containing overloaded abstract methods

  1. abstract class Vehicle  
  2. {  
  3.       
  4.         abstract void display();  
  5.         abstract void display(String msg);  
  6.       
  7. }  
  8. class Honda extends Vehicle  
  9. {  
  10.   
  11.     @Override  
  12.     void display() {  
  13.           
  14.         System.out.println(“abstract method is invoked”);  
  15.     }  
  16.   
  17.     @Override  
  18.     void display(String msg) {  
  19.           
  20.         System.out.println(msg);  
  21.     }  
  22.       
  23. }  
  24. public class AbstractExample4 {  
  25.   
  26.     public static void main(String[] args) {  
  27.      Honda obj=new Honda();  
  28.      obj.display();  
  29.      obj.display(“overloaded abstract method is invoked”);  
  30.     }         
  31. }  

Test it Now

Output:

abstract method is invoked
overloaded abstract method is invoked

Example 5: Inner abstract class

  1. class Vehicle  
  2. {  
  3.     abstract class Car   
  4.     {  
  5.         abstract void display();  
  6.     }  
  7.   
  8. class Honda extends Car  
  9. {  
  10.   
  11.     @Override  
  12.     void display() {  
  13.           
  14.         System.out.println(“inner abstract class is invoked”);  
  15.     }     
  16. }  
  17. }  
  18. public class AbstractExample5 {  
  19.   
  20.     public static void main(String[] args) {  
  21.     Vehicle obj=new Vehicle();  
  22.      Vehicle.Car c=obj.new Honda();  
  23.      c.display();  
  24.     }         
  25. }  

Test it Now

Output:

inner abstract class is invoked

Example 6: Nested abstract class

  1. abstract class Vehicle  
  2. {  
  3.     abstract class Car   
  4.     {  
  5.         abstract void display();  
  6.     }  
  7. }  
  8. class Honda extends Vehicle  
  9. {  
  10.     class FourWheller extends Car  
  11.     {  
  12.     @Override  
  13.     void display() {  
  14.           
  15.         System.out.println(“nested abstract class is invoked”);  
  16.     }     
  17. }  
  18. }  
  19. public class AbstractExample6 {  
  20.   
  21.     public static void main(String[] args) {  
  22.      Vehicle obj=new Honda();  
  23.      Honda h=(Honda)obj;  
  24.      Honda.FourWheller fw=h.new FourWheller();  
  25.       fw.display();  
  26.     }         
  27. }  

Test it Now

Output:

nested abstract class is invoked

Leave a Comment