Concrete class in Java

A concrete class is a class that has an implementation for all of its methods. It is a complete class, who implements all methods from abstract classes or interfaces.

It is a complete class and can be instantiated. Any class which is not abstract is a concrete class. e.g

package com.javacodestuffs.concrete; public class Calculator { public int multiply(int a, int b) { return a * b; } static int add(int a, int b) { return a + b; } public static void main(String args[]) { Calculator calc = new Calculator(); int product = calc.multiply(51, 123); int sum = calc.add(123, 51); System.out.println("Product of numbers is : " + product); System.out.println("Sum of numbers is : " + sum); } } $javac com/javacodestuffs/concrete/Calculator.java $java -Xmx128M -Xms16M com/javacodestuffs/concrete/Calculator Product of numbers is : 6273 Sum of numbers is : 174

Abstract Classes

An abstract class is a class that contains both unimplemented methods and implemented methods, but it is incomplete.

There are many examples of Abstract Classes in JDK Like AbstractMap, AbstractSet, etc.

abstract class Product implements MathOperations { public int add(int x, int y); //unimplemented method public int multiply(int x, int y) { return x * y; } }

Interfaces

In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.

Method bodies exist only for default methods and static methods.

Interfaces cannot be instantiated, they can only be implemented by classes or extended by other interfaces.

interface MathOperations { public int multiply(int x, int y); public int add(int x, int y); public int square(int x); public default void displayResult(int x) { System.out.println("The result is : " + x); } }

The concrete class extends an abstract class

package com.javacodestuffs.concrete; interface MathOperations { public int multiply(int x, int y); public int add(int x, int y); public int square(int x); } abstract class Product implements MathOperations { // product of two numbers public int multiply(int x, int y) { return x * y; } } // concrete class public class Calculator extends Product { public int add(int x, int y) { return x + y; } public int square(int x) { return x * x; } public static void main(String args[]) { Calculator calc = new Calculator(); int product = calc.multiply(51, 123); int sum = calc.add(123, 51); int square = calc.square(11); System.out.println("product of numbers are : " + product); System.out.println("Sum of numbers is : " + sum); System.out.println("Square of number is : " + square); } } $javac com/javacodestuffs/concrete/Calculator.java $java -Xmx128M -Xms16M com/javacodestuffs/concrete/Calculator product of numbers are : 6273 Sum of numbers is : 174 Square of number is : 121

We have seen we have interface MathOperations with 3 methods. The product is abstract class unable to implement all methods as it is abstract, implements only single method public int multiply(int x, int y).

But Calculator is a concrete class implements all methods which are not implemented by his parent i.e Product class.

Differences between interfaces and concrete and abstract classes.

In this article, we have seen the Concrete class in Java with examples.