Extending Interface to an Interface

Our Objective 

Write a Java program about the topic extending interface to an interface  in object-oriented program.


The Theory 

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: 

Interface Inheritance: 

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. 

Syntax of Interface Extension: 

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. 

Code Reusability: 

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. 

Multiple Interface Extension: 

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(); 






  • Point Class: This class represents a 2D point with x and y coordinates. It has private fields x and y to store the coordinates, and private methods setX() and setY() to set the values of these coordinates. It also has private methods getX() and getY() to retrieve the values of the coordinates. 
  • Shape Interface: This interface defines the blueprint for shapes. It contains two methods: area() and perimeter(), which are meant to be implemented by classes that implement the interface. Additionally, it has a public static final constant pi with a value of 3.14, which is intended to be used for calculations involving pi. 
  • Circle Class: This class implements the Shape interface and represents a circle. It has a protected field radius to store the radius of the circle. It provides implementations for the area() and perimeter() methods based on the formulae for the area and perimeter of a circle. Note that the formula for the perimeter is incomplete (p = 2πr), and you would need to complete it by returning the correct value. 
  • X Interface: This interface defines two methods: a() and b(). It's used as a base interface for another interface and a class. 
  • Y Interface: This interface extends the X interface and adds two more methods: c() and d(). This means that any class implementing the Y interface needs to provide implementations for all four methods (a(), b(), c(), and d()). 
  • Z Class: This class implements both the X and Y interfaces. It provides implementations for all four methods, printing out messages to the console indicating which method is being implemented. 
  • Driver Class: This class contains the main method, which serves as the entry point of the program. It creates an instance of the Z class and calls its a(), b(), c(), and d() methods to demonstrate the implementation of those methods. 

Learning Outcomes  

  • Interface Modularity: Learners will grasp the importance of designing interfaces to be modular and cohesive. By breaking down functionality into smaller, specialized interfaces, they can create a more manageable and maintainable codebase. 
  • Interface Inheritance: Understanding how interfaces can inherit from one another teaches learners about interface hierarchy and how to organize interfaces in a logical and structured manner. 
  • Code Reusability: Extending interfaces promotes code reusability, allowing learners to create new functionalities without modifying existing code. This practice reinforces the principles of software development, such as the Open/Closed Principle (OCP), which emphasizes extending rather than modifying existing code. 
  • Interface Refinement: Learners will learn how to refine and specialize interfaces by extending them. This ability helps in building a clear and concise interface hierarchy, making it easier to work with complex software systems.