Abstract Class in JAVA

What is abstract Class

 
An abstract class is nothing but a collection of concrete or non-concrete methods; a concrete method contains a body (implementation) and non-concrete methods do not contain a body; it's just a prototype declaration like access modifier, return type, parameter, etc.
 

What Situation when the abstract class has Importance?

 
Suppose you want to make a super class which defines only some general functionality like as you make a class which defines some general(common ) properties and some properties are left blank and you want that these properties are defined by in derived class.
 
For example there is a bird class it contains some general functionality of birds like as weight, color, flying speed, etc and some attribute can not be same like as size of bings and shape of body these attributes are changed according to every bird so this a situation where we can give the complete implementation of general properties and left the abstract all the not common properties  according to the rule if any class inherit the bird class then it must implement all the abstract method.
 

Declaration of abstract class in Java

 
"abstract" is a keyword in Java; with the help of the abstract keyword, we can make abstract classes and methods.
 
For making an abstract class
 
abstract class Abhishek
{
    /* body of class*/
     note in this class one abstract method
}
 
For making an abstract method
 
abstract int abstractmethod (list of parameters);
/* we can not give the body of 
 
An example of an abstract class
  1. abstract class Abhishek   
  2. {  
  3.     abstract void abstractmethod();  
  4.     // concrete methods are still allowed in abstract classes  
  5.     void nonabstract()   
  6.     {  
  7.          System.out.println("This is a concrete method.");  
  8.     }  
  9. }  
  10. class Child extends Abhishek   
  11. {  
  12.      void abstractmethod()   
  13.      {  
  14.            System.out.println("B must be implementation of abstractmethod.");  
  15.      }  
  16. }  
  17. class AbstractDemo   
  18. {  
  19.       public static void main(String args[])  
  20.       {  
  21.             Child b = new Child();  
  22.             b.abstractmethod();  
  23.             b.nonabstract();  
  24.       }  
  25. }  
Output
 
Clipboard(1).jpg
 

Summary

 
You can create your own abstract class and method with the help of this example. There are not any limitations for making an abstract method and non-abstract method in your program but all the abstract methods are overridden by a child class (inherited class).


Recommended Ebook

Solutions Manual to Objects First with Java – A Practical Introduction using BlueJ

Download Now!
Similar Articles