Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaDifference between Abstract Class and Concrete Class in Java

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. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along with the new statement. It may or may not contain an abstract method. An abstract method is declared by abstract keyword, such methods cannot have a body. If a class contains an 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 an 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 a 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.
     

Java




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


  1. Compile Error: 
     
prog.java:9: error: DemoAbstractClass is abstract; cannot be instantiated
        DemoAbstractClass AC = new DemoAbstractClass();
                               ^
  1. Example: Valid instantiation by defining all abstract method of an abstract class. 
     

Java




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?");
    }
}


  1.  
Output: 

Hi.
How are you?

 

  1. Example: Direct instantiation of concrete using new keyword.
     

Java




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?");
    }
}


  1.  
Output: 

Hi.
How are you?

 

  1.  
  2. 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. 
    1. public void service(ServletRequest req, ServletResponse res)
    2. protected void service(HttpServletRequest req, HttpServletResponse res)
    3. protected void doGet(HttpServletRequest req, HttpServletResponse res)
    4. protected void doPost(HttpServletRequest req, HttpServletResponse res)
    5. protected void doHead(HttpServletRequest req, HttpServletResponse res)
    6. protected void doOptions(HttpServletRequest req, HttpServletResponse res)
    7. protected void doPut(HttpServletRequest req, HttpServletResponse res)
    8. protected void doTrace(HttpServletRequest req, HttpServletResponse res)
    9. protected void doDelete(HttpServletRequest req, HttpServletResponse res)
    10. protected void getLastModified(HttpServletRequest req)
  3. 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.
  4. Interface: An abstract class cannot implement an interface alone but it can implement an interface, by the use of a child class and not providing implementations of all of the interface’s methods. It is the responsibility of the first concrete class that has that abstract class as an ancestor to implement all of the methods in the interface. 
     

 

Abstract Class Concrete Class
An abstract class is declared using abstract modifier. A concrete class is not 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.
Implement an interface is possible by not providing implementations of all of the interface’s methods. For this a child class is needed.. Easy implementation of all of the methods in the interface.

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. 
     

 

RELATED ARTICLES

Most Popular

Recent Comments