Concrete class in Java

Concrete Class in Java

A class in java that can be created using β€˜new’ keyword is called a concrete class in java. It is also known as the complete blueprint of its own self and can be instantiated. This class has the implementation of all the methods in it. Hence, this class can never contain any unimplemented methods. A concrete class can always extend an abstract class.

Difference between Abstract and Concrete class

  • An abstract class can never be directly instantiated whereas a concrete class can be instantiated. We can also instantiate an abstract class using concrete class.
  • A concrete class implements all the abstract methods of an abstract parent class.
  • We declare an abstract class using an abstract modifier. Whereas, we can instantiate a concrete class using new keyword. If we use abstract keyword in the concrete class, it will become an abstract class only.
  • An abstract class is impossible without abstract methods. Whereas, a concrete class can never have abstract methods.
  • We cannot declare an abstract class as a final class. However, we can declare a concrete class as a final class.

Below is how we can directly instantiate a concrete class using new keyword in Java.

abstract class DeveloperHelps { 
abstract void display(); 
} 
class ConcreteClass extends DeveloperHelps { 
void display() { 
System.out.println("My name is Megha"); 
} 
} 
public class test { 
public static void main(String[] args) { 
ConcreteClass x = new ConcreteClass(); 
x.display(); 
System.out.println("Welcome to Developer Helps"); 
} 
}

The output of this java program will be:

My name is Megha
Welcome to Developer Helps

Another example will helps you in better understanding of difference between an abstract and concrete class

public class DeveloperHelps {
public static void main(String args[]) {
fruit mango = new mango();
mango.eat();
}
}
abstract class fruit {
abstract public void eat();
}
class mango extends fruit{
public void eat(){
System.out.println("Mango is a fruit");
}
}

The output of this java program will be:

Mango is a fruit

Summary:

  • A class which is not an abstract class is always a concrete class.
  • A concrete class can extend abstract class.
  • An abstract class can never extend a concrete class.
  • If a concrete class will have abstract methods, it will be one abstract class only.

Leave a comment

Your email address will not be published. Required fields are marked *