Write a Java program about the topic extending interface to an interface in object-oriented program.
In Java, an interface is a fundamental concept that allows you to define a contract or blueprint for a group of related classes. It provides a way to achieve multiple inheritance and establish a common structure for classes that might not necessarily share a common ancestor. An interface defines a set of method signatures (without implementations) that classes implementing the interface must provide. This promotes consistency and helps in achieving loose coupling between classes in your codebase.
An interface is declared using the interface keyword followed by its name and a set of method signatures. These methods act as placeholders for functionality that concrete classes implementing the interface must define. An interface can also include constant fields, which are implicitly public, static, and final. Since Java doesn't support multiple inheritance through classes, interfaces provide a way to achieve this by allowing a class to implement multiple interfaces. This promotes code reusability and ensures that classes adhere to specific contracts.
When a class implements an interface, it must provide implementations for all the methods declared in the interface. This enforces the contract defined by the interface. Any class that implements an interface can be used interchangeably wherever the interface type is expected. This concept is at the heart of polymorphism and allows for the creation of more modular and flexible code.
In conclusion, interfaces in Java play a crucial role in achieving abstraction, defining contracts, and facilitating polymorphism. They promote the separation of concerns and help in building modular, reusable, and maintainable code. By providing a way to specify a set of method signatures without implementation details, interfaces enable different classes to work together harmoniously, even when they're not directly related in the class hierarchy. They are an essential tool for designing well-structured and extensible Java applications. Extending an interface to another interface, also known as interface inheritance or interface extension, is a concept in object-oriented programming that allows you to create a new interface that inherits the members (methods and constants) from an existing interface. By extending an interface, you can add additional functionality to the new interface while still maintaining a relationship with the original interface. Here's a deeper explanation of this concept:
In object-oriented programming languages, an interface is a contract that defines a set of method signatures that a class must implement. Interfaces serve as blueprints for classes, specifying what methods they should have without providing their implementations.
When one interface extends another, it means the child interface inherits the method declarations (and constants) from the parent interface. The child interface effectively becomes a specialized version of the parent interface, with additional methods (if any) added.
The syntax for extending an interface to another interface varies depending on the programming language. Generally, it follows a syntax similar to extending classes but using the extends or : keyword to denote interface extension.
Specialization and Refinement:
By extending an interface, you can specialize or refine the behavior of the child interface. The child interface can introduce new methods that are relevant to a specific context while inheriting the core functionality from the parent interface.
Interface extension promotes code reusability since classes that implement the child interface must provide implementations for all the methods declared in both the parent and child interfaces. This allows you to use the same class in different contexts by providing different implementations for the additional methods.
Some programming languages allow interfaces to extend multiple interfaces. This enables even more flexibility and modularity in defining complex interfaces by combining features from multiple parent interfaces.
In summary, extending an interface to another interface allows you to create specialized versions of existing interfaces, enabling code reusability and maintaining a clear and structured design in object-oriented programming.
Sample implementation given below.
public class Point{
private int x;
private int y;
private void setX(int xCoord) {
this.x = xCoord;
}
private void setY(int yCoord) {
this.y = yCoord;
}
private int getX() {
return this.x;
}
private int getY() {
return this.y;
}
}
public interface shape{
public static final double pi = 3.14;
public double area();
public double perimeter();
};
public class Circle implements Shape {
protected double radius;
public double area() {
return pi* radius * radius;
}
public double perimeter() {
p = 2πr
}
}
public interface X {
public void a();
public void b();
}
public interface Y {
public void c();
public void d();
}
public interface Y extends X {
public void c();
}
public class Z implements X, Y {
public void a() { System.out.println("Implementing value of function a") };
public void b() { System.out.println("Implementing value of function b") };
public void c() { System.out.println("Implementing value of function c") };
public void d() { System.out.println("Implementing value of function d") };
}
public class Driver {
public static void main(String[]){
Z obj = new Z();
obj.a();
obj.b();
obj.c();
obj.d();
}
}