Abstract class in java with example

What is abstract class?
An abstract class is a just like a normal class with a restriction that it cannot be instantiated which means that you cannot create its object. An abstract class is not complete and its implementation is completed by sub-classes. Note that an abstract class may or may not contain any abstract methods(explained below) but if a class contains even one abstract method, it should be declared abstract.
Why use abstract class?
Many developers question when to use abstract class. Abstract class should be used in following conditions.

    • While creating classes we do not know the complete implementation of a class, it should be marked as abstract and let child classes provide the functionality.
    • Functionality depends on sub-classes. Example, consider a class Vehicle having a drive method. This class is extended by Car, Truck, Train classes. Each of these would have different implementation of drive method. Hence, it is better to create this method as abstract. Therefore, class Vehicle will also be abstract.
    • If we want that our class should always be extended or sub-classed before use, it should be marked as abstract.

What is abstract method?
A method which does not have an implementation or a body is an abstract method. An abstract method can be declared in an abstract class or an interface.
If the abstract method is declared in an interface, then its implementation is provided in the class which implements the interface while if abstract method is declared in an abstract class, then its implementation is provided in a sub-class of the abstract class.
A method declared as abstract should have the keyword abstract in its signature and it does not have a body. That is, it does not contain curly braces({ and }) but rather it ends with parenthesis and semi-colon to indicate that its definition is incomplete.
An abstract method is declared using syntax

<access modifier>  abstract <return type> <method name> ();

Thus to create an abstract method with name sayHello which returns a String and has a public access, the syntax will be,

public String abstract sayHello();

An abstract method should end with a semi-colon(;)

How to create an abstract class?
An abstract class is created using abstract keyword. Syntax to create abstract class is

<access modifier>  abstract  <class name> {
   …. class methods and variables
}

Abstract class example
Below is an example of an abstract class named Student which has two fields, one abstract method and one non-abstract method

public abstract class Student {
 
     private String studentName;
     private int class;
 
   /**
      * Method which is non-abstract
      */
     public void display(){
         System.out.println("Student name : " + studentName + ", Class = " + class);
     }
 
   /**
      * Abstract method
      */
     public abstract void grade();
}

Below is an example of a sub-class of the above abstract class which provides an implementation of the abstract method defined in its super class.

public class StudentGrade extends Student {
 
   /**
      * Implementation of Abstract method
      */
     public void grade(){
         if(class &gt; 10){
              System.out.println("Senior Grade");
         } else {
              System.out.println("Junior Grade");
         }
     }
}

Let’s tweak in

  1. It is not necessary for an abstract class to have abstract method. A class with no abstract method can also be declared abstract.
  2. If a class has even one abstract method then it is mandatory to declare the class as abstract.
  3. An abstract class can have a constructor which is called when any of its sub-classes is instantiated.
  4. An abstract class can have a main method and can be executed just like another class.
  5. A sub-class of an abstract class inherits all the non-private fields and methods of its super-class.
  6. If a sub-class does not provide implementation of abstract methods of its super class then it should also be declared abstract.
  7. An abstract class can implement interfaces without providing implementation for interface methods. This makes sense since the class is already abstract.
  8. Abstract classes can also contain final methods.
0

Leave a Reply

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.