Concrete Method in Java
In Java, a concrete method is any method that has a complete implementation, meaning it includes a method body with executable code.
This is in contrast to an abstract method, which is declared without an implementation and must be defined in a subclass.
Get Articles Media’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s how to understand and use concrete methods:
1. Definition and Characteristics:
- A concrete method has a
{}block containing its code. - It can be declared in both concrete classes and abstract classes.
- Concrete methods in abstract classes can be inherited and used directly by subclasses, or they can be overridden to provide specific behavior.
- Concrete methods in interfaces are possible through
defaultandstaticmethods (introduced in Java 8) andprivatemethods (introduced in Java 9).
2. Example in a Class:
class MyClass {
// This is a concrete method
public void concreteMethod() {
System.out.println("This is the implementation of a concrete method.");
}
// This is another concrete method with parameters and a return type
public int add(int a, int b) {
return a + b;
}
}3. Example in an Abstract Class:
abstract class Shape {
// An abstract method (no implementation)
public abstract double getArea();
// A concrete method in an abstract class
public void displayMessage() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}4. Example in an Interface (Java 8+):
interface MyInterface {
// An abstract method (must be implemented by concrete classes)
void doSomething();
// A default concrete method (provides a default implementation)
default void defaultMethod() {
System.out.println("Default implementation in the interface.");
}
// A static concrete method (can be called directly on the interface)
static void staticMethod() {
System.out.println("Static method in the interface.");
}
}
class MyClassImpl implements MyInterface {
@Override
public void doSomething() {
System.out.println("Implementation of doSomething.");
}
}







