Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Java, classes are the building blocks of object-oriented programming. They define the attributes & behaviors of objects. One important type of class in Java is the concrete class. Concrete classes are the regular classes that we use to create objects & implement the features defined in interfaces or abstract classes.
In this article, we'll discuss what concrete classes are, their syntax, and how they differ from abstract classes. We'll also look at examples of concrete classes that extend abstract classes or implement interfaces.
What is a Concrete Class?
A concrete class in Java is a regular class that has a complete implementation of all its methods. Unlike abstract classes, concrete classes can be instantiated, meaning we can create objects of these classes. They provide the actual functionality of the methods defined in their parent classes or interfaces.
Concrete classes are used to represent real-world entities or concepts in our programs. For example, if we have an abstract class called "Animal" with abstract methods like eat() & sleep(), we can create concrete subclasses like "Dog" or "Cat" that provide the actual implementation for these methods. Concrete classes are the most common type of class in Java programs.
Syntax of Concrete Class in Java
The syntax for declaring a concrete class in Java is straightforward. Here's the basic syntax:
access_modifier class ClassName {
// class body
// fields
// constructors
// methods
}
```
Let's break down the components:
- `access_modifier`: This can be `public`, `private`, `protected`, or default (no modifier). It specifies the visibility of the class.
- `class`: The keyword used to declare a class in Java.
- `ClassName`: The name of the concrete class, which should follow Java naming conventions (e.g., start with a capital letter).
- `class body`: The class body is enclosed in curly braces `{}` and contains the fields, constructors, and methods of the class.
For example :
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
In this example, we have a concrete class named `Student` with private fields `name` and `age`, a constructor to initialize the fields, and a method `displayInfo()` to display the student's information.
Simple Concrete Class Example
In this example, we'll create a class called "Rectangle" that represents a rectangle shape.
public class Rectangle {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double calculateArea() {
return width * height;
}
public double calculatePerimeter() {
return 2 * (width + height);
}
}
In this code:
- We have a concrete class named `Rectangle`.
- The class has two private fields: `width` and `height`, representing the dimensions of the rectangle.
- We have a constructor `Rectangle(double width, double height)` that takes the width and height as parameters and initializes the fields.
- We have getter and setter methods for both `width` and `height` to access and modify their values.
- The class has two methods: `calculateArea()` and `calculatePerimeter()`, which calculate and return the area and perimeter of the rectangle, respectively.
In the `Main` class, we create an object of the `Rectangle` class using the constructor and passing the width and height values. We then use the getter methods to retrieve the width and height, and we call the `calculateArea()` and `calculatePerimeter()` methods to calculate and display the area and perimeter of the rectangle.
Concrete Class Which Extends an Abstract Class
In Java, a concrete class can extend an abstract class, providing implementations for the abstract methods defined in the abstract class. Let's look at an example to illustrate this concept.
First, we'll define an abstract class called "Shape":
public abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double calculateArea();
public String getColor() {
return color;
}
}
In the `Shape` abstract class:
- We have a protected field `color` representing the color of the shape.
- We have a constructor that takes the color as a parameter and initializes the `color` field.
- We have an abstract method `calculateArea()` that will be implemented by the concrete subclasses.
- We have a concrete method `getColor()` that returns the color of the shape.
Now, let's create a concrete class `Circle` that extends the `Shape` abstract class:
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
public double getRadius() {
return radius;
}
}
In the `Circle` concrete class:
- We extend the `Shape` abstract class using the `extends` keyword.
- We have an additional field `radius` representing the radius of the circle.
- We have a constructor that takes the color and radius as parameters. It calls the superclass constructor using `super(color)` to initialize the `color` field, and it initializes the `radius` field.
- We provide an implementation for the abstract method `calculateArea()` specific to the circle formula.
- We have a getter method `getRadius()` to retrieve the radius value.
Now, let's see how we can use the `Circle` concrete class:
In the `Main` class, we create an object of the `Circle` class, passing the color and radius values to the constructor. We can then use the methods inherited from the `Shape` abstract class (`getColor()`) as well as the methods specific to the `Circle` class (`getRadius()` and `calculateArea()`).
Concrete Class Which Implements an Interface
In Java, a concrete class can also implement one or more interfaces. When a class implements an interface, it provides implementations for all the methods declared in the interface. Let's look at an example to understand this concept.
First, we'll define an interface called "Drawable":
public interface Drawable {
void draw();
}
The `Drawable` interface declares a single method `draw()` that must be implemented by any class that implements this interface.
Now, let's create a concrete class `Rectangle` that implements the `Drawable` interface:
public class Rectangle implements Drawable {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("Drawing a rectangle with width: " + width + " and height: " + height);
}
public double calculateArea() {
return width * height;
}
}
In the `Rectangle` concrete class:
- We use the `implements` keyword to indicate that the class implements the `Drawable` interface.
- We have two private fields `width` and `height` representing the dimensions of the rectangle.
- We have a constructor that takes the width and height as parameters and initializes the fields.
- We provide an implementation for the `draw()` method declared in the `Drawable` interface. In this case, it prints a message indicating that it is drawing a rectangle with the given width and height.
- We have an additional method `calculateArea()` specific to the `Rectangle` class that calculates the area of the rectangle.
Now, let's see how we can use the `Rectangle` concrete class that implements the `Drawable` interface:
Drawing a rectangle with width: 5.0 and height: 3.0
Area: 15.0
In the `Main` class, we create an object of the `Rectangle` class, passing the width and height values to the constructor. We can then call the `draw()` method defined in the `Drawable` interface, which is implemented by the `Rectangle` class. Additionally, we can call the `calculateArea()` method specific to the `Rectangle` class.
Java Abstraction vs. Concrete Classes
Java Abstraction
Concrete Classes
Abstract classes and interfaces represent abstract concepts or blueprints.
Concrete classes represent specific implementations of abstract concepts or interfaces.
Abstract classes can have abstract methods (methods without a body) and concrete methods.
Concrete classes provide implementations for all methods, including those inherited from abstract classes or interfaces.
Interfaces can only have abstract methods and constants. They cannot have fields or constructors (Java 8 onwards supports default methods).
Concrete classes can have their own fields, constructors, and methods in addition to implementing abstract methods.
Abstract classes cannot be instantiated directly. They are used as base classes for inheritance.
Concrete classes can be instantiated and used to create objects.
Abstract classes and interfaces define a contract that concrete subclasses must follow.
Concrete classes provide the actual implementation of the contract defined by abstract classes or interfaces.
Abstract classes and interfaces are used to achieve abstraction and define common behavior.
Concrete classes are used to represent real-world entities and provide specific functionality.
Frequently Asked Questions
Can a concrete class have abstract methods?
No, a concrete class must provide implementations for all methods, including any abstract methods inherited from its parent classes or interfaces.
Can a concrete class extend multiple classes?
No, in Java, a concrete class can only extend a single class. However, it can implement multiple interfaces.
Is it mandatory for a concrete class to implement all the methods declared in an interface it implements?
Yes, a concrete class must implement all the methods declared in the interfaces it implements. Otherwise, it must be declared as an abstract class.
Conclusion
In this article, we learned about concrete classes in Java. Concrete classes are regular classes that provide complete implementations for all their methods. They can be instantiated to create objects and represent specific entities or concepts. We explained the syntax of concrete classes, saw examples of simple concrete classes, and learned how concrete classes can extend abstract classes or implement interfaces.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.