Difference between Abstract Class and Concrete Class in Java

Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. object of such class cannot be created directly using new keyword. An abstract class can be instantiated either by concrete subclass, or by defining all the abstract method along with the new statement. It may or may not contain abstract method. An abstract method is declared by abstract keyword, such methods cannot have a body. If a class contains abstract method, then it also needs to be abstract.

Concrete Class: A concrete class in Java is a type of subclass, which implements all the abstract method of its super abstract class which it extends to. It also has implementations of all methods of interfaces it implements.

Abstract Class vs Concrete Class



  1. Modifier: An abstract class is declared using abstract modifier. Concrete class should not be declared using abstract keyword, on doing so, it will also become abstract class.

  2. Instantiation: An abstract class cannot be instantiated directly, i.e. object of such class cannot be created directly using new keyword. An abstract class can be instantiated either by concrete subclass, or by defining all the abstract method along with the new statement. A concrete class can be instantiated directly, using a new keyword.

    Example: Invalid direct instantiation of an abstract class.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    abstract class DemoAbstractClass {
        abstract void display();
    }
      
    public class JavaApplication {
        public static void main(String[] args)
        {
            DemoAbstractClass AC = new DemoAbstractClass();
            System.out.println("Hello");
        }
    }

    chevron_right

    
    

    Compile Error:

    prog.java:9: error: DemoAbstractClass is abstract; cannot be instantiated
            DemoAbstractClass AC = new DemoAbstractClass();
                                   ^
    

    Example: Valid instantiation by defining all abstract method of an abstract class.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    abstract class DemoAbstractClass {
        abstract void display();
    }
      
    public class JavaApplication {
        public static void main(String[] args)
        {
            DemoAbstractClass AC = new DemoAbstractClass() {
                void display()
                {
                    System.out.println("Hi.");
                }
            };
            AC.display();
            System.out.println("How are you?");
        }
    }

    chevron_right

    
    

    Output:

    Hi.
    How are you?
    

    Example: Direct instantiation of concrete using new keyword.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    abstract class DemoAbstractClass {
        abstract void display();
    }
      
    class ConcreteClass extends DemoAbstractClass {
        void display()
        {
            System.out.println("Hi.");
        }
    }
      
    public class JavaApplication {
        public static void main(String[] args)
        {
            ConcreteClass C = new ConcreteClass();
            C.display();
            System.out.println("How are you?");
        }
    }

    chevron_right

    
    

    Output:

    Hi.
    How are you?
    
  3. Abstract methods: An abstract class may or may not, have an abstract method. A concrete class cannot have an abstract method, because class containing an abstract method must also be abstract.
    Let’s understand this by an example:-
    We have a class called “HttpServlet” in Servlet. Now, this class is special in its own way.
    “HttpServlet is an abstract class, but it doesn’t contain any abstract methods”.
    Let’s understand the meaning of the above statement in-depth:-
    -> HttpServlet is an abstract class because it doesn’t have proper implementation for its methods. Since it doesn’t have proper implementation for its methods, then what is the sense of creating objects for such class. Therefore, this class is made as abstract.

    Now Question arises is that:-
    Q.If methods have not proper implementation, then why they have not made it abstract?
    -> Since abstract methods are those, which doesn’t have a proper body defining and we override such methods in our class to give it a proper definition. So basically abstract methods are used for overriding purpose.
    Now this “HttpServlet” class contain 10 methods and suppose if these methods were made abstract then we need to override all the 10 methods of “HttpServlet” class which is a foolish way since we need only doGet()and doPost() methods.


  4. Below is a sample program to illustrate above point in Java:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to show servlet example
    // Importing required Java libraries
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
      
    // Extend HttpServlet class
    public class AdvanceJavaConcepts extends HttpServlet 
       private String output;
        
       // Initializing servelet
       public void init() throws ServletException 
       {
          output = "Advance Java Concepts";
       }
      
       // Requesting and printing the output
       public void doGet(HttpServletRequest req, 
                        HttpServletResponse resp)
          throws ServletException, IOException 
          {
             resp.setContentType("text/html");
             PrintWriter out = resp.getWriter();
             out.println(output);
          }
      
          public void destroy() 
          {
             System.out.println("Over");
          }
    }

    chevron_right

    
    

    Output:

    Advance Java Concepts
    output
    Over
    

    NOTE: As mentioned earlier,HttpServlet class doesn’t contain any abstract method.So the program run successfully without overriding all of its method.
    But suppose if this class contain all of its method as abstract, then we need to override all of it’s methods although they do nothing in our class.
    Prototypes of methods of HttpServlet class are:

    1. public void service(ServletRequest req,ServletResponse res)
    2. protected void service(HttpSerletRequest req,HttpServletResponse res)
    3. protected void doGet(HttpSerletRequest req,HttpServletResponse res)
    4. protected void doPost(HttpSerletRequest req,HttpServletResponse res)
    5. protected void doHead(HttpSerletRequest req,HttpServletResponse res)
    6. protected void doOptions(HttpSerletRequest req,HttpServletResponse res)
    7. protected void doPut(HttpSerletRequest req,HttpServletResponse res)
    8. protected void doTrace(HttpSerletRequest req,HttpServletResponse res)
    9. protected void doDelete(HttpSerletRequest req,HttpServletResponse res)
    10. protected void getLastModified(HttpSerletRequest req)

    Unnecessary overriding of such methods will be their in our class if it were made abstract.

    NOTE: However in latest IDE like netbeans etc,it will automatically override all abstract methods by giving them blank body reducing the headache of programmer to override.

  5. Final: An abstract class cannot be final, because all its abstract methods must defined in the subclass. A concrete class can be declared as final.
  6. Interface: Interface implementation is not possible with abstract class, however, it is possible with concrete class.

Abstract Class Concrete Class
An abstract class is declared using abstract modifier. A concrete class is note declared using abstract modifier.
An abstract class cannot be directly instantiated using the new keyword. A concrete class can be directly instantiated using the new keyword.
An abstract class may or may not contain abstract methods. A concrete class cannot contain an abstract method.
An abstract class cannot be declared as final. A concrete class can be declared as final.
Interface implementation is not possible Interface implementation is possible.

Some important points:

  • A concrete class is a subclass of an abstract class, which implements all its abstract method.
  • Abstract methods cannot have body.
  • Abstract class can have static fields and static method, like other classes.
  • An abstract class cannot be declared as final.
  • Only abstract class can have abstract methods.
  • A private, final, static method cannot be abstract, as it cannot be overridden in a subclass.
  • Abstract class cannot have abstract constructors.
  • Abstract class cannot have abstract static methods.
  • If a class extends an abstract class, then it should define all the abstract methods (override) of the base abstract class. If not, the subclass(the class extending abstract class) must also be defined as abstract class.


My Personal Notes arrow_drop_up

Student, BCA

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnshulVaidya