Home » Java programming language

Differences between Abstract class and Concrete class

Abstract class vs Concrete class: Here, we are going to learn about the differences / comparisons between Abstract class and Concrete class.
Submitted by Preeti Jain, on July 14, 2019

1) Abstract Class

  • The "abstract" keyword is mandatory to declare an abstract class.
  • We cannot create an object of the abstract class directly by using new keyword then, in that case, we can define all the abstract methods along with the new keyword.

Example of abstract class

// Declare an abstract class with abstract keyword
abstract class AbstractClass {
    // Declare an abstract method with no implementation
    abstract void printMethod();
}

public class ImplementedClass {
    public static void main(String[] args) {
        // Creating an object of abstract class using new keyword 
        // along with method implementation.
        AbstractClass ac = new AbstractClass() {
            void printMethod() {
                System.out.println("Hi, We are in Java World");
            }
        };

        // Calling an abstract class methods
        ac.printMethod();
    }
}

Output

D:\Programs>javac ImplementedClass.java

D:\Programs>java ImplementedClass 
Hi, We are in Java World

Note:

  • An abstract class may or may not contain abstract methods.
  • We cannot declare an abstract method as final because we need to implement all the methods if we declare a class as final then it is not allowed to implement the methods.

2) Concrete Class

  • we don't need to prefix "abstract" in the declaration of concrete class if we include "abstract" keyword before class name then it will also become abstract.
  • In the case of the concrete class, we can create an object of this class directly by using the new keyword.

Example of concrete class

// Declare an abstract class with abstract keyword
abstract class AbstractClass {

    // Declare an abstract method with no implementation
    abstract void printMethod();
}

// Defining concrete class with extending abstract class
class ConcreteClass extends AbstractClass {


    // Implementing abstract method
    void printMethod() {
        System.out.println("Hi,We are in Java World");
    }
}

public class Main {
    public static void main(String[] args) {

        // Creating an object of concrete class using new keyword directly.
        ConcreteClass cc = new ConcreteClass();
        
        // Calling concrete class method.
        cc.printMethod();
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
Hi,We are in Java World

Note:

  • In the case of a concrete class, it will not contain any abstract method because if we contain a single abstract method then the class will become abstract.
  • In case of a concrete class, we can declare this class as final because it does not contain abstract method so need to care about the implementation.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.