0% found this document useful (0 votes)
15 views3 pages

Java Interfaces for Inheritance and Area

The document provides two practical programming examples demonstrating the use of interfaces in Java. The first example illustrates multiple inheritance through a class hierarchy involving a Student class, a Test class, and a Result class that implements a Sports interface. The second example shows how to calculate the area of a rectangle and a circle using an Area interface, with separate classes for Rectangle and Circle implementing the compute method.

Uploaded by

kalpraj Gore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Java Interfaces for Inheritance and Area

The document provides two practical programming examples demonstrating the use of interfaces in Java. The first example illustrates multiple inheritance through a class hierarchy involving a Student class, a Test class, and a Result class that implements a Sports interface. The second example shows how to calculate the area of a rectangle and a circle using an Area interface, with separate classes for Rectangle and Circle implementing the compute method.

Uploaded by

kalpraj Gore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No:-9

[Link] the use of interfaces to implement the concept of multiple inheritance.

class Student
{
int rollno;
void getdata( int n)
{
rollno=n;
}
void putdata()
{
[Link]("Roll No: "+rollno);
}
}
class Test extends Student
{
float m1,m2;
void getmarks(float a,float b)
{
m1=a;
m2=b;
}
void putmarks()
{
[Link]("Marks Obtained: ");
[Link]("Marks1: "+m1);
[Link]("Marks2: "+m2);
}
}
interface Sports
{
float sportwt=6.0f;
void putwt();
}
class Result extends Test implements Sports
{
float total;
public void putwt()
{
[Link]("Sports Wt= "+sportwt);
}
void display()
{
total=m1+m2+sportwt;
putdata();
putmarks();
putwt();
[Link]("Total Score: "+total);
}
}
class Hybrid
{
public static void main(String args[])
{
Result student1=new Result();
[Link](1234);
[Link](85.5f,90.6f);
[Link]();
}
}

Output:-
[Link] a program to find area of rectangle and circle using interfaces.

interface Area
{
final static float pi=3.14f;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
[Link]("Area of Rectangle= "+[Link](10,20));
area=cir;
[Link]("Area of Circle= "+[Link](10,0));
}
}

Output:-

Common questions

Powered by AI

The implementation of the 'Sports' interface within the 'Result' class demonstrates multiple inheritance by allowing the class to incorporate behavior from two sources: the `Test` class and the `Sports` interface. In effect, `Result` inherits properties and methods from the `Test` class and implements methods defined in the `Sports` interface. This emulates the concept of multiple inheritance, permitting the class to extend the functionality through inheritance hierarchy (via `Test`) and protocol-like structures (via `Sports`) in Java .

Static and default methods in interfaces provide new capabilities in Java interfaces. `Static methods` allow methods to be called on the interface itself without an implementing class. `Default methods` permit adding new methods to interfaces with implementation, preserving backward compatibility. This is significant since these methods support evolving interfaces without forcing changes to implementing classes. However, in the document's context, default or static methods aren't prominent, but understanding their significance is crucial as Java evolves to provide richer interface functionalities .

The `final` keyword is used in the `Area` interface to indicate that `pi` is a constant and its value cannot be changed. This ensures that any implementing class adheres to a consistent value for `pi`, maintaining integrity across applications. This use of `final` implies a design decision to standardize constant values across implementations, which is crucial when constants have universal applicability, such as mathematical constants. This promotes code safety and reduces error likelihood from inadvertent changes .

Interfaces promote maintainability and scalability by standardizing the interaction with various components without detailing their internal implementations. This abstraction allows the code to be extended with minimal changes. The system can be expanded by adding new shapes that implement the `Area` interface without modifying existing code, thus enhancing scalability. Maintenance is simplified because changes to computation algorithms can be made in individual implementing classes. The example provided defines a simple interface (`Area`) used by `Rectangle` and `Circle` which makes changes easy without affecting the rest of the system .

Using interfaces for computing areas allows for a flexible design where different shapes can implement the same interface and provide their specific implementations of the method. This supports polymorphism, where the same method call can produce different outcomes based on which object's method is invoked. In the provided document, this leads to separating the method structure from the actual implementation, enabling easy expansion and maintenance of the system. The `Area` interface is used for both `Rectangle` and `Circle`, allowing each to compute its area using its formula .

In Java, classes can inherit only one class due to single inheritance limitations, but they can implement multiple interfaces, which enables multiple inheritance-like behavior. An interface, unlike a class, is a reference type that can contain only constants, method signatures, default methods, static methods, and nested types. The method bodies (except default methods) appear within the classes that implement the interfaces. This is evident in the `Result` class from the example which extends the `Test` class and implements the `Sports` interface, thereby allowing access to both class functionality and additional functionality defined in the interface .

The program in the `Hybrid` class encapsulates data and behavior by defining specific classes (`Result`, `Test`, `Student`) and interfaces (`Sports`, `Area`) that represent distinct entities with specific characteristics. `Polymorphism` is exhibited by using the `Area` interface to refer to either `Rectangle` or `Circle` objects, demonstrating method overriding at runtime. When a method is called on the `Area` reference, different variants are executed based on the actual object type (`Rectangle` or `Circle`), allowing the same interface method call to yield different results depending on the context .

Using interfaces like `Area` and `Sports` provides a clear separation of concerns in the program, enhancing both readability and structure. Interfaces define a contract that implementing classes must satisfy, which organizes code logically and increases predictability. For the `Hybrid` class, interfaces aid in understanding at a high level what operations are available, without needing to examine implementation details. This abstraction improves readability and modular organization, essential for maintaining clear and scalable codebases .

The interplay between classes and interfaces in the document exemplifies core object-oriented design principles, such as abstraction, encapsulation, and modularity. Interfaces establish a contract for subclasses, abstracting operations independently of implementation specifics. This allows varying implementations (`Circle`, `Rectangle`) to coexist under a unified model (`Area`). Encapsulation is showcased as each class manages its state and behavior components, ensuring integrity and reusability. The modular approach separates concerns efficiently (`Student` for data, `Test` for scores, `Result` for aggregation), facilitating maintenance and scalability, reflecting a mature OOP architecture .

The choice of using an interface to calculate geometric areas standardizes how different shapes implement area calculation, enhancing flexibility and extensibility. However, potential drawbacks include added overhead for defining and maintaining interface structures when only a single shape type is used, leading to unnecessary complexity. Additionally, interfaces do not allow encapsulation of implementation details or reusable code parts, potentially duplicating efforts across implementations. Interfaces also can't store state, requiring strategy adjustments in scenarios where state retention is necessary .

You might also like