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!

What is the difference between interface and concrete class in Java?

Last Updated on August 7, 2023 by Mayank Dham

In Java, being an object-oriented language, you have the advantage of structuring your code using reusable classes. The term "reusable" holds significance as it emphasizes that code reusability begins not just when you create objects from classes, but even during the process of designing the classes themselves.

What is Interface in Java?

In Java, an interface is a reference type that defines a contract for classes that implement it. It specifies a set of abstract methods that the implementing classes must provide concrete implementations for. In simpler terms, an interface defines a list of method signatures that must be implemented by any class that claims to implement that interface.

An interface is declared using the interface keyword and can include method declarations (without method bodies) and constant declarations. All methods in an interface are implicitly public and abstract, meaning they do not have method bodies and must be implemented in the implementing classes.

Here’s the basic syntax for declaring an interface in Java:
public interface MyInterface {
// Method declarations (implicitly public and abstract)
void method1();
int method2(String arg);
// …

    // Constant declarations (implicitly public, static, and final)
    int MY_CONSTANT = 42;
    // ...
}

To use an interface, a class must implement it using the implements keyword:
public class MyClass implements MyInterface {
// Implementing the abstract methods from the interface
@Override
public void method1() {
// Provide implementation for method1
}

    @Override
    public int method2(String arg) {
        // Provide implementation for method2
        return 0;
    }
    // ...
}

An important feature of Java interfaces is that a class can implement multiple interfaces, allowing it to inherit the behavior and contracts from multiple sources.

public class MyMultipleInterfacesClass implements Interface1, Interface2 {
    // Implementing methods from both Interface1 and Interface2
    // ...
}

Interfaces are useful for achieving abstraction, defining common behavior that multiple unrelated classes can share, and enabling loose coupling between classes in Java applications. They play a significant role in supporting the concept of "interface-based programming" and enhancing code flexibility and maintainability.

What is Concrete Class in Java?

In Java, a concrete class is a class that provides concrete, complete implementations of all the methods declared in its class hierarchy, including methods inherited from its parent classes and interfaces. Unlike abstract classes, concrete classes can be instantiated, meaning objects can be created from them using the new keyword.

Key characteristics of concrete classes in Java:

  • Complete Implementations: A concrete class must provide concrete (non-abstract) implementations for all the methods declared in its class hierarchy, including those inherited from abstract classes and interfaces.
  • Instantiation: Objects of concrete classes can be created using the new keyword, making them directly usable.
  • May or May Not Inherit from Other Classes: A concrete class can either extend another concrete class or an abstract class. It can only extend one class, as Java supports single inheritance. However, a concrete class can implement multiple interfaces.
  • Can Have Constructors: Concrete classes can have constructors that are used to initialize object instances when they are created.
  • Can Have Instance Variables: Concrete classes can have instance variables (fields) to hold the state of objects.
  • Can Be Final: A concrete class can be declared as final, which means it cannot be subclassed further.

Example of a concrete class in Java:

// Concrete class

public class Dog {
    // Instance variables
    private String name;
    private int age;

    // Constructor
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Concrete method
    public void bark() {
        System.out.println(name + " is barking!");
    }

    // Concrete method
    public void fetch() {
        System.out.println(name + " is fetching the ball.");
    }
}

In the above example, Dog is a concrete class that provides complete implementations for the bark() and fetch() methods. You can create objects of this class and call its methods directly.

public class Main {
    public static void main(String[] args) {
        Dog dog1 = new Dog("Buddy", 3);
        dog1.bark(); // Output: Buddy is barking!
        dog1.fetch(); // Output: Buddy is fetching the ball.
    }
}

It’s important to note that concrete classes can also have methods inherited from parent classes and interfaces that they need to implement, making them fully functional and providing concrete behavior to their objects.

Conclusion
In Java, a concrete class is a class that provides complete implementations for all the methods declared in its class hierarchy, including methods inherited from its parent classes and interfaces. Unlike abstract classes, concrete classes can be instantiated, allowing objects to be created from them using the new keyword. Concrete classes form the foundation of object-oriented programming in Java, as they define the actual behavior and state of objects.

FAQs related to the difference between interface and concrete class in Java:

1. What is the difference between a concrete class and an abstract class in Java?
The main difference between a concrete class and an abstract class is that a concrete class provides complete implementations for all its methods, while an abstract class may have abstract methods (methods without implementations) that must be implemented by its subclasses. Concrete classes can be instantiated, whereas abstract classes cannot be directly instantiated.

2. Can a concrete class be subclassed in Java?
Yes, a concrete class can be subclassed in Java. Subclassing allows a new class (subclass) to inherit properties and behaviors from an existing class (superclass). The subclass can extend the concrete class and add its own specific functionalities or override the methods inherited from the superclass.

3. Is it possible to have abstract methods in a concrete class?
No, a concrete class cannot have abstract methods. Concrete classes are required to provide complete implementations for all their methods. If a class has at least one abstract method, it must be declared as an abstract class.

4. Can a concrete class implement multiple interfaces in Java?
Yes, a concrete class can implement multiple interfaces in Java. This allows the class to inherit and implement the behavior defined in each interface. Java supports multiple interface implementation, but it does not support multiple inheritance for classes.

5. When should I use a concrete class in my Java application?
Use a concrete class when you want to create objects directly and provide complete, ready-to-use implementations for all the methods in the class hierarchy. Concrete classes are suitable for defining actual objects with specific behavior and state. If you want to define a template or a partially implemented class, you might consider using an abstract class.

6. Can a concrete class be marked as final in Java?
Yes, a concrete class can be marked as final in Java. When a class is declared as final, it cannot be subclassed further, meaning you cannot create subclasses of a final class. This is useful when you want to prevent any modification or extension of a particular class.

Leave a Reply

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