Concrete Class in Java


The concrete class is a base regular implementation which is declared initially for the all other methods related to it. This class do not have any unimplemented method, which is also can be extended to an abstract class on an interface class. We can create an object class by using a new key word class. The process can inherit an another abstract class. This process is also known as the 100% implemented class present in Java.

There are so many advantages and disadvantages of a concrete class in Java −

  • Advantages −

    • The process serve to decompose the complex designs.

    • Reduce object dependendies.

    • It is a real time injection fo several implementations.

    • Can be extended to the interface and abstruct class.

  • Disadvantages −

    • Less flexible but faster than others.

    • Unlisted in nature.

Algorithm to Define a Concrete Class in Java

In this possible algorithm, we are going to show you how to declare and perform a concrete class in a Java environment. By using this algoritm, we will build some possible syntax to explain the problem statement in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Declare and import Java packages.

  • Step 3 − Declare a public class.

  • Step 4 − Declare a string name.

  • Step 5 − Return the value.

  • Step 6 − Mention the abstract method.

  • Step 7 − Inherit the previous class.

  • Step 8 − Declare the main method.

  • Step 9 − Declare string argument.

  • Step 10 − Print the desired value.

  • Step 11 − Get the return value.

  • Step 12 − Terminate the process.

Syntax to Define a Concrete Class in Java

class ConcreteCalculator {
	static int add(int a, int b){
		return a + b;
	}
	static int subtract(int a, int b){
		Concrete Class in Java
		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){
		//Declaration of main method
		int sum = add(Value, Value);
		int diff = subtract(Value, Value);
		int prod = multiply(Value, Value);
		int div = divide(Value, Value);
		//Print the values of the process
		System.out.println("" + sum);
		System.out.println("" + diff);
		System.out.println("" + prod);
		System.out.println("" + div);
	}
}
abstract class Shape{
	abstract double area();
	abstract double perimeter();
}
class Circle extends Shape{
	double r = 5;
	public double area(){
		return 3.14 * r * r;
	}
	public double perimeter(){
		return 2 * 3.14 * r;
	}
}
class Triangle extends Shape{
	double a = 5;
	double b = 12;
	double c = 9;
	double h = 4;
	public double area(){
		return b * h / 2;
	}
	public double perimeter(){
		return a + b + c;
	}
}
class Square extends Shape{
	double a = 8;
	public double area(){
		return a * a;
	}
	public double perimeter(){
		return 4 * a;
	}
}

In this possible syntax above, we have tried to show you how to declare and create a concrete class. By using this possible syntax we are heading towards the possible Java illustrations to understand the problem statement in a broad view.

Approaches to Follow

  • Approach 1 − Java program to illustrate concrete class by using the sum class also define an product class by using this

  • Approach 2 − Java program to define an abstract class by using a concrete method and also use a ShapeUtil Class that has utility methods

Approach 1: to Illustrate Concrete and Product Classes

Use of the sum Class Method

In this possible approach, we are going to apply the sum() class method to get the addition value by using a concrete class in Java.

public class XYZ extends Main class implements ABC {
	public String honk() {
		return "beep";
	}
}

Example

//Java program to illustrate concrete class by using the product and sum classes
public class ARBRDD {
   static int product(int a, int b){
      return a * b;
   }
   static int sum(int a, int b){
      return a + b;
   }
   public static void main(String args[]){
      int p = product(16, 7);
      int s = sum(7, 16);
      System.out.println("Product: " + p);
      System.out.println("Sum: " + s);
   }
}

Output

Product: 112
Sum: 23

Use of Product Class Method

In this possible approach, we are going to apply the product() class method to get the product value by using a concrete class in Java.

Example

//Java program to illustrate concrete class by implementing a product() method in an interface
interface X {
   int product(int a, int b);
   int sum(int a, int b);
}
abstract class Product implements X {
   public int product(int a, int b){
      return a * b;
   }
}
public class Main extends Product {
   public int sum(int a, int b){
      return a + b;
   }
   public static void main(String args[]){
      Main ob = new Main();
      int p = ob.product(16, 7);
      int s = ob.sum(7, 16);
      System.out.println("Product of the two numbers is: " + p);
      System.out.println("Sum of the two numbers is: " + s);
   }
}

Output

Product of the two numbers is: 112
Sum of the two numbers is: 23

Approach 2: Define an Abstract Class by Using a Concrete Method With ShapeUtil Class

Use of the Abstract Class Method

In this possible approach,we are going to apply the abstract class method to get the desired value by using a concrete class in Java.

public void draw() {
   System.out.println("A Statement Value...");
}
public double getArea() {
   return width * height;
}

Example

//Java program to define an abstract class by using a concrete method
abstract class Shape{
   abstract double area();
   abstract double perimeter();
}
class Circle extends Shape{
   double r = 5;
   public double area(){
      return 3.14 * r * r;
   }
   public double perimeter(){
      return 2 * 3.14 * r;
   }
}
class Triangle extends Shape{
   double a = 5;
   double b = 12;
   double c = 9;
   double h = 4;
   public double area(){
      return b * h / 2;
   }
   public double perimeter(){
      return a + b + c;
   }
}
class Square extends Shape{
   double a = 8;
   public double area(){
      return a * a;
   }
   public double perimeter(){
      return 4 * a;
   }
}
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

Use of the ShapeUtil Class Method

In this possible approach,we are going to apply the ShapeUtil Class that has utility methods to draw any shapes and print with a concrete class.

Example

//Java program to use a ShapeUtil Class that has utility methods to draw any shapes and print with a concrete class
abstract class Shape {
   private String name;
   public Shape() {
      this.name = "Unknown shape is here";
   }
   public Shape(String name) {
      this.name = name;
   }
   public String getName() {
      return this.name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public abstract void draw();
   public abstract double getArea();
   public abstract double getPerimeter();
}
class Rectangle extends Shape {
   private double width;
   private double height;
   public Rectangle(double width, double height){
      super("Rectangle Is The Aimed Drawing");
      this.width = width;
      this.height = height;
   } 
   public void draw() {
      System.out.println("Drawing a rectangle...");
   }
   public double getArea() {
      return width * height;
   }
   public double getPerimeter() {
      return 2.0 * (width + height);
   }
}
class Circle extends Shape {
   private double radius;
   public Circle(double radius) {
      super("Circle is the aimed drawing");
      this.radius = radius;
   }
   public void draw() {
      System.out.println("Drawing a circle...");
   }
   public double getArea() {
      return Math.PI * radius * radius;
   }
   public double getPerimeter() {
      return 2.0 * Math.PI * radius;
   }
}
class ShapeUtil {
   public static void drawShapes(Shape[] list){
      for (int i = 0; i < list.length; i++){
         list[i].draw();
      }
   }
   public static void printShapeDetails(Shape[] list){
      for (int i = 0; i < list.length; i++){
         String name = list[i].getName();
         double area = list[i].getArea();
         double perimeter = list[i].getPerimeter();
         System.out.println("Name of the drawing: " + name);
         System.out.println("Area of the drawing: " + area);
         System.out.println("Perimeter value is: " + perimeter);
      }
   }
}
public class Main{
   public static void main(String[] args){
      Shape[] shapeList = new Shape[2];
      shapeList[0] = new Rectangle(16.0, 7.0);
      shapeList[1] = new Circle(23.0);
      ShapeUtil.drawShapes(shapeList);
      ShapeUtil.printShapeDetails(shapeList);
   }
}

Output

Drawing a rectangle...
Drawing a circle...
Name of the drawing: Rectangle Is The Aimed Drawing
Area of the drawing: 112.0
Perimeter value is: 46.0
Name of the drawing: Circle is the aimed drawing
Area of the drawing: 1661.9025137490005
Perimeter value is: 144.51326206513048

Conclusion

The concrete class is a normal class of a Java program, which will not have any abastract methods present into it. Today in this article, we have learned about the concrete class in Java and the methods related with it. With the above mentioned algorithm and syntax; we have built some Java codes to explain the problem statement in an efficient manner.

Updated on: 29-Dec-2023

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements