Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Concrete class in Java

Last Updated on August 7, 2023 by Mayank Dham

A concrete class is one that provides implementations for all its methods, leaving no room for unimplemented methods. Such a class can either extend an abstract class or implement an interface, as long as it fulfills all the required method implementations. Being a complete class, it is capable of being instantiated.

Put simply, any class that is not abstract is considered a concrete class.

Necessary condition for a concrete class in Java

In Java, a concrete class is a class that can be instantiated, and it must meet the following necessary conditions:

  • It must not be declared as abstract: A concrete class cannot have the abstract keyword in its declaration. Abstract classes cannot be instantiated, and they may contain abstract methods (methods without implementation).
  • It must provide implementations for all its methods: A concrete class should implement all the methods declared in its class definition. Unlike abstract classes, concrete classes cannot leave any methods without implementation.
  • It must not extend an abstract class without implementing its abstract methods: If a concrete class extends an abstract class, it must provide concrete (non-abstract) implementations for all the abstract methods declared in the parent abstract class.
  • It must not fail to implement methods from implemented interfaces: If a concrete class implements one or more interfaces, it must provide concrete implementations for all the methods declared in those interfaces.

Example of Concrete Class in Java

Let’s consider an example of a concrete class in Java called Rectangle.

public class Rectangle {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public void printDetails() {
        System.out.println("Rectangle - Width: " + width + ", Height: " + height);
    }
}

Explanation
In the example above, we have a class named Rectangle, which represents a simple rectangle shape. This class is a concrete class because it satisfies the necessary conditions for being concrete:

1. It is not declared as abstract: There is no abstract keyword in the class declaration, making it a concrete class.
2. It provides implementations for all its methods: The class has three methods: getArea(), getPerimeter(), and printDetails(). Each of these methods has a complete implementation, leaving no methods without an implementation.
3. It does not extend an abstract class without implementing its abstract methods: In this case, the Rectangle class does not extend any other class, so there’s no abstract class to implement.
4. It does not fail to implement methods from implemented interfaces: The Rectangle class does not implement any interfaces, so this condition does not apply.

The Rectangle class has two private instance variables width and height, representing the dimensions of the rectangle. The constructor initializes these variables with the provided values. The getArea() method calculates the area of the rectangle, the getPerimeter() method calculates its perimeter, and the printDetails() method prints the width and height of the rectangle to the console.

Since the Rectangle class meets all the necessary conditions for a concrete class, you can create instances of it and use its methods without any issues. For example:

public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(5.0, 3.0);
        System.out.println("Area: " + rectangle.getArea());
        System.out.println("Perimeter: " + rectangle.getPerimeter());
        rectangle.printDetails();
    }
}

Output:

Area: 15.0
Perimeter: 16.0
Rectangle - Width: 5.0, Height: 3.0
public class Rectangle {
    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public void printDetails() {
        System.out.println("Rectangle - Width: " + width + ", Height: " + height);
    }
}
public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(5.0, 3.0);
        System.out.println("Area: " + rectangle.getArea());
        System.out.println("Perimeter: " + rectangle.getPerimeter());
        rectangle.printDetails();
    }
}

The output confirms that the Rectangle class is a concrete class and can be instantiated and used successfully.

Conclusion
In Java, a concrete class is a class that can be instantiated and is complete in the sense that it provides implementations for all its methods. It cannot have any unimplemented methods, and it must not be declared as abstract. Concrete classes can extend other classes and implement interfaces, as long as they fulfill the requirements of those extended classes and implemented interfaces. These classes are essential for creating objects and representing real-world entities in a program.

FAQs related to concrete classes in Java:

1. What is the purpose of a concrete class in Java?
Concrete classes provide the actual implementation of a class and are used to create objects in Java. They encapsulate data and behavior within their methods, allowing programmers to work with real-world entities and define specific functionalities.

2. Can a concrete class be inherited in Java?
Yes, a concrete class can be inherited in Java. Other classes can extend a concrete class using the extends keyword, allowing them to inherit its properties and methods. Concrete classes can also be extended by other concrete classes, creating a class hierarchy.

3. Can a concrete class have abstract methods in Java?
No, a concrete class cannot have abstract methods. Abstract methods lack implementation, and a concrete class must provide concrete implementations for all its methods.

4. Can a concrete class implement multiple interfaces in Java?
Yes, a concrete class can implement multiple interfaces in Java using the implements keyword. By doing so, the class must provide concrete implementations for all the methods declared in each interface.

5. Can we create an instance of an abstract class or an interface in Java?
No, we cannot directly create an instance of an abstract class or an interface in Java. They are incomplete classes that may have abstract methods without implementation. However, we can create objects of concrete classes that extend an abstract class or implement an interface.

6. When should I use a concrete class in Java?
You should use a concrete class when you want to create objects that represent specific entities with well-defined behavior and attributes. Concrete classes are suitable for most situations where you need complete implementations and the ability to create instances. If you have common functionality shared across multiple classes, you may consider using abstract classes or interfaces to define shared methods.

Leave a Reply

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