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; its just a prototype declaration like access modifier, return type, parameter etc.

What Situation when abstract class have Importance

Suppose you want to making a super class which define only some general functionality like as you make a class which define the some general(common ) properties and some properties are left blank and you want that this properties is defined by in derived class
for example there are a bird class it contain the the 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 attribute are change according to every bird so this a situation where we can give the complete implementation of general properties and and left the abstract all the not common properties  according the rule if any class inherit the bird class then it must be implement all the abstract method   


Syntax of declaration abstract class

"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 parameter);
 /* we can not give the body of

An example of an abstract class:

abstract class Abhishek
{
    abstract void abstractmethod();
    // concrete methods are still allowed in abstract classes
    void nonabstract()
    {
         System.out.println("This is a concrete method.");
    }
}
class Child extends Abhishek
{
     void abstractmethod()
     {
         System.out.println("B must be implementation of abstractmethod.");
     }
}
class AbstractDemo
{
      public static void main(String args[])
      {
            Child b = new Child();
            b.abstractmethod();
            b.nonabstract();
      }
}

Ouput

Clipboard(1).jpg

Summary

You can create yourown 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 overriden by a child class (inherited class).

Up Next
    Ebook Download
    View all
    Learn
    View all