Implementing an Interface

Our Objective 

Write a Java programme about the topic Implementing an Interface.

 

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. 
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. 

  1. public class Point{ 
  2.  private int x; 
  3.  private int y; 
  4.  private void setX(int xCoord) { 
  5.   this.x = xCoord; 
  6.  } 
  7.  private void setY(int yCoord) { 
  8.   this.y = yCoord; 
  9.  } 
  10.  private int getX() { 
  11.   return this.x; 
  12.  } 
  13.  private int getY() { 
  14.   return this.y; 
  15.  } 
  16. public interface shape{ 
  17.  public static final double pi = 3.14; 
  18.  public double area(); 
  19.  public double perimeter(); 
  20. }; 
  21. public class Circle implements Shape { 
  22.  protected double radius; 
  23.  Circle(double r) { 
  24.   radius = r; 
  25.  } 
  26.  public double area() { 
  27.   return 3.14 * radius * radius; 
  28.  } 
  29.  public double perimeter() { 
  30.   p = 2*3.14*radius 
  31.  } 
  32. public static void main(String[]){ 
  33.  public class Driver { 
  34.   Circle c = new Circle(5); 
  35.   c.area(); 
  36.   c.parameter(); 
  37.  } 
  • Point Class: The Point class represents a 2D point with x and y coordinates. It has private instance variables x and y to store the coordinates. The class also contains private setter and getter methods for these coordinates. 
  • Shape Interface: The Shape interface defines the blueprint for geometric shapes. It includes two constant members: a public static final double pi which is set to 3.14, representing the value of π, and two abstract methods: area() and perimeter(). Any class that implements the Shape interface must provide concrete implementations for these methods. 
  • Circle Class: The Circle class implements the Shape interface, indicating that it adheres to the contract specified by the interface. The class has a protected instance variable radius to store the radius of the circle. The constructor accepts a radius and initializes the radius instance variable. 
  • The class provides implementations for the area() and perimeter() methods as required by the Shape interface. The area() method calculates the area of the circle using the formula π * radius^2, and the perimeter() method calculates the perimeter (circumference) of the circle using the formula 2 * π * radius. 
  • Driver Class: The Driver class contains the main method, which serves as the entry point of the program. Inside the main method: 
  • An instance of the Circle class is created with a radius of 5. 
  • The area() method of the Circle instance is called to calculate the area of the circle. 
  • The perimeter() method of the Circle instance is called to calculate the perimeter of the circle. 

 

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.