0% found this document useful (0 votes)
2 views4 pages

Java Interface for Multiple Inheritance

Uploaded by

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

Java Interface for Multiple Inheritance

Uploaded by

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

Interface

how interface is used to achieve multiple Inheritance in Java. 4M

[Link] inheritances is not possible in java. Multiple inheritance happens when


a class is derived from two or more parent classes.

2. Java classes cannot extend more than one parent classes , instead it uses the
concept of interface to implement the multiple inheritance.

3. It contains final variables and the methods in an interface are abstract. A sub
class implements the interface

4. When such implementation happens, the class which implements the interface
must define all the methods of the interface.

5. A class can implement any number of interfaces.

Syntax:

interface <interface_name>{

// declare constant fields

// declare methods that abstract

// by default.

In other words, Interface fields are public, static and final by default, and the
methods are public and abstract.

Interface Example
[2-marks for Logic, 2-marks for correct Syntax]

interface Gross

double ta=500.0;

double da=1200.0;

void gross_sal();

class Employee

String name;

float basic_sal;

Employee(String n, float b)

name=n;

basic_sal=b;

}
void display()

[Link]("Name of Employee="+name);

[Link]("Basic Salary of Employee="+basic_sal);

class salary extends Employee implements Gross

float hra;

salary(String n, float b, float h)

super(n,b);

hra=h;

void disp()

display();

[Link]("HRA of Employee="+hra);

public void gross_sal()

double gross_sal=basic_sal+ta+hra;
[Link]("TA of Employee="+ta);

[Link]("DA of Employee="+da);

[Link]("Gross Salary of Employee="+gross_sal);

class Empdetail

public static void main(String args[])

salary s=new salary("ABC",6000,4000);

[Link]();

s.gross_sal();

Common questions

Powered by AI

Implementing multiple interfaces in Java permits classes to utilize multiple types, akin to multiple inheritance, by defining interfaces that declare methods the class must implement. This provides flexibility since a class can exhibit different behaviors by implementing various interfaces. However, unlike multiple inheritance of classes, this approach cannot share method implementations; all methods must be manually defined, which could lead to code duplication if different interfaces have overlapping or similar required functionalities. Moreover, state sharing is also not supported, as interfaces can't contain instance variables, only constants .

In Java interfaces, the fields are by default public, static, and final. This means that fields defined within interfaces are constants. Methods in an interface are public and abstract by default, meaning that they must be implemented by a class that implements the interface .

If a class implements a Java interface but fails to define all of the interface's methods, the class must be declared as abstract. It cannot be instantiated and any class that extends this abstract class must provide implementations for all the abstract methods to become a concrete class that can be instantiated. Failure to do so results in a compilation error .

Java's approach to multiple inheritance via interfaces offers several advantages over direct multiple class inheritance. It eliminates the diamond problem, where ambiguities arise in the inheritance hierarchy if a class inherits from two classes that contain the same method. Interfaces promote a clean and maintainable design, allowing classes to exhibit shared behaviors without inheriting code from multiple ancestors, reducing complexity and potential bugs. This maintains strong encapsulation, reducing tight coupling across classes, which aligns well with software design principles like SOLID .

The 'salary' class in the example is structured to extend the 'Employee' class, which provides inheritance of fields and methods like 'name' and 'display()'. It implements the 'Gross' interface to incorporate the additional contract enforced by the 'gross_sal()' abstract method, thereby providing a concrete implementation. This mixing of extending a class and implementing an interface allows 'salary' to inherit basic features from 'Employee' while also fulfilling the behavior expected by 'Gross', showcasing Java's model of using both inheritance and interfaces .

Methods in Java interfaces are inherently public and abstract. The 'public' keyword indicates that the methods are accessible universally, while 'abstract' means the methods do not have a body and must be implemented by any class that uses the interface. This enforces a contract that any implementing class must provide the concrete implementation of these methods, ensuring a consistent interface across classes .

To create an interface in Java, use the 'interface' keyword followed by the interface name. The key components that must be included are constant fields and abstract methods. Fields are implicitly public, static, and final, while methods are public and abstract by default. Example syntax: 'interface InterfaceName { // declare constant fields // declare methods that are abstract by default. }' .

Java uses interfaces to implement multiple inheritance. Although a Java class cannot extend more than one class, it can implement numerous interfaces. An interface in Java is a reference type, much like a class, that can contain only constants, method signatures, default methods, static methods, and nested types. The methods in the interfaces are by default abstract. When a class implements an interface, it should provide the body to all the abstract methods declared in the interface. This approach allows a Java class to exhibit behavior from multiple sources, akin to multiple inheritance, by implementing multiple interfaces .

Java's use of interfaces for achieving multiple inheritance aligns with object-oriented principles by promoting polymorphism, modularity, and separation of concerns. Interfaces allow objects to interact via abstractly defined behaviors, strengthening encapsulation and decreasing coupling. Multiple interfaces enable diverse object interactions, supporting polymorphism by allowing a class to be of multiple types. This methodology respects inheritance hierarchies without the diamond problem, frequently encountered in multiple inheritance with classes, reinforcing clear and maintainable code structure .

The example in the source illustrates Java interface implementation through the 'Gross' interface and the 'salary' class. The 'Gross' interface declares a method 'gross_sal()' and two final variables 'ta' and 'da'. The 'salary' class extends the 'Employee' class and implements the 'Gross' interface, thereby providing a concrete implementation for the 'gross_sal()' method. This implementation calculates the gross salary by summing up 'basic_sal', 'ta', and 'hra', demonstrating how interfaces can be used to ensure specific methods are included in the class .

You might also like