A concrete class is any normal class in a Java program. This class will not have any abstract methods. All the methods in the concrete class java are completely implemented.
A java concrete class can inherit from another class, even an abstract class or implement an interface. We can instantiate the concrete class and create its objects.
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
Example :
public class Main { // Concrete Class example
static int total(int val1, int val2) {
return val1 + val2;
}
public static void main(String args[]) {
int sum = total(10, 22);
System.out.println("Total of two integers: " + sum);
}
}
Output :
Total of two integers: 32
Another Example :
public class ConcreteCalculator {
static int add(int a, int b)
{
return a + b;
}
static int subtract(int a, int b)
{
return a - b;
}
static int multiply(int a, int b)
{
return a * b;
}
static int divide(int a, int b)
{
return a / b;
}
public static void main(String[] args)
{
int sum = add(100, 154);
int diff = subtract(154, 79);
int prod = multiply(7, 8);
int div = divide(345, 5);
System.out.println("Addition result: " + sum);
System.out.println("Subtraction result: " + diff);
System.out.println("Multiplication result: " + prod);
System.out.println("Division result: " + div);
}
}
Output :
Addition result: 254
Subtraction result: 75
Multiplication result: 56
Division result: 69
In the above code, the ConcreteCalculator class has 4 methods that perform arithmetic operations. All four methods add, subtract, multiply, divide are implemented and return the result. As all the methods of the class have been implemented ConcreteCalculator is a concrete class.
Abstract and Concrete Class in Java
Let’s consider a program of concrete class implementation with abstract class. The shape is an abstract class with two unimplemented methods area(), perimeter(). The other three classes extend class Shape and implement both the methods area() and perimeter() of the abstract class. Hence, the classes Circle, Triangle, and Square are concrete classes.
Code :
abstract class Shape {
abstract double area();
abstract double perimeter();
}
class Circle extends Shape {
double r = 5; // radius of circle
public double area() {
return 3.14 * r * r; // Area of Circle = π r^2
}
public double perimeter() {
return 2 * 3.14 * r; // Perimeter of Circle = 2 π r
}
}
class Triangle extends Shape {
// Declaring required variables
double b = 12;
double h = 4;
public double area() {
// Implementing parent class method area
return b * h / 2; // Area of triangle = ½ × Base × Height
}
public double perimeter() {
double a = 5; // Assuming a = 5 (You can use the actual value if available)
return a + b + h; // Perimeter of triangle = a + b + h (Sum of all sides)
}
}
class Square extends Shape {
double a = 8; // side of square
public double area() {
return a * a; // Area of Square = a * a (side square)
}
public double perimeter() {
return 4 * a; // Perimeter of Square = 4 * side (Sum of all sides)
}
}
public class DemoMain {
public static void main(String[] args) {
Circle c = new Circle();
double c_area = c.area();
double c_perimeter = c.perimeter();
System.out.println("Area of Circle: " + c_area);
System.out.println("Perimeter of Circle: " + c_perimeter);
Triangle t = new Triangle();
double t_area = t.area();
double t_perimeter = t.perimeter();
System.out.println("Area of Triangle: " + t_area);
System.out.println("Perimeter of Triangle: " + t_perimeter);
Square s = new Square();
double s_area = s.area();
double s_perimeter = s.perimeter();
System.out.println("Area of Square: " + s_area);
System.out.println("Perimeter of Square: " + s_perimeter);
}
}
Output :
Area of Circle: 78.5
Perimeter of Circle: 31.400000000000002
Area of Triangle: 24.0
Perimeter of Triangle: 26.0
Area of Square: 64.0
Perimeter of Square: 32.0
In the above code, Shape is a abstract class with abstract methods area() and perimeter(). Classes Circle, Triangle, and Square inherit the abstract class shape and implement both abstract methods. As the classes Circle, Triangle and Square have all the methods implemented they are Concrete Classes.So this was a simple implementation of abstract and concrete classes in java.
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.
- Concrete classes in Java are fully implemented classes that implement all the methods.
- Every method that a concrete class contains must be implemented. A concrete class may extend an abstract class or implement an interface.
- To instantiate a concrete class in Java, the new keyword is used.
- A concrete class can also be declared as a final class