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

Advantages of Interfaces in Java

Interfaces in Java allow for abstraction and multiple inheritance. They define behaviors and contain only abstract method signatures and static constants. Classes implement interfaces to inherit the defined behaviors. Key differences between interfaces and abstract classes are that interfaces contain only abstract methods while abstract classes can contain both abstract and non-abstract methods, and interfaces support multiple inheritance while abstract classes do not.

Uploaded by

Aman Rai
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 views6 pages

Advantages of Interfaces in Java

Interfaces in Java allow for abstraction and multiple inheritance. They define behaviors and contain only abstract method signatures and static constants. Classes implement interfaces to inherit the defined behaviors. Key differences between interfaces and abstract classes are that interfaces contain only abstract methods while abstract classes can contain both abstract and non-abstract methods, and interfaces support multiple inheritance while abstract classes do not.

Uploaded by

Aman Rai
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

Interface in Java

 It is a blue print of class.


 It has static constants and abstract methods.
 It is used to achieve abstraction and multiple inheritance in java.
 We can say interfaces can have abstract methods and variables. It can not have a body.
 It can not be instantiated like abstract class.

Reasons of using interface


a) To achieve abstraction
b) Support for multiple inheritance
c) To achieve loose coupling

Similarities and difference between abstract class and interface


 Abstract class and interface both are used to achieve abstraction where we can declare the
abstract methods.
 Abstract class and interface both can't be instantiated.

But there are many differences between abstract class and interface that are given below.

Abstract class Interface

1) Abstract class can have abstract 1) Interface can have only abstract methods.
and non-abstract methods.

2) Abstract class doesn't support 2) Interface supports multiple inheritance.


multiple inheritance.

3) Abstract class can have final, 3) Interface has only static and final variables.
non-final, static and non-static
variables.

4) Abstract class can provide the 4) Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to 5) The interface keyword is used to declare interface.
declare abstract class.

6) An abstract class can extend 6) An interface can extend another Java interface only.
another Java class and implement
multiple Java interfaces.

7) An abstract class can be 7) An interface can be implemented using keyword "


extended using keyword "extends". implements".

8) A Java abstract class can have 8) Members of a Java interface are public by default.
class members like private,
protected, etc.

We can say that abstract class achieves partial abstraction (0 to 100%) whereas interface
achieves fully abstraction (100%).

Syntax fo interface declaration:

interface interface-name
{
Declare constant fields
Declare abstract methods
}

Note: The java compiler adds public and abstract keywords before the interface method.
Moreover, it adds public, static and final keywords before data members

Relationship between classes and interfaces:

a) A class extends another class


b) An interface extends another interface
c) But class implements an interface
Let us see examples of interface in Java:-

Program-1 to demonstrate interface

interface test1
{
void print();
}
class Atest implements test1
{
public void print()
{
[Link]("Hello");
}

public static void main(String args[])


{

Atest obj=new Atest();


[Link]();

}
Program-2 to demonstrate interface

interface Banktest
{
float rateofinterest();
}
class PNB implements Banktest
{
public float rateofinterest()
{
return 9.25f;
}

}
class SBI implements Banktest
{
public float rateofinterest()
{
return 8.75f;
}

}
class HDFC implements Banktest
{
public float rateofinterest()
{
return 9.50f;
}

}
class Testinterface
{
public static void main(String args[])
{

Banktest obj=new PNB();


[Link](" Rate of interest = " + [Link]());

}
Multiple inheritance in java by interface:

Program-3 to demonstrate interface

interface A
{
void showA();
}
interface B
{
void showB();
}

class ABtest implements A,B


{
public void showA()
{
[Link]("ShowA");
}

public void showB()


{
[Link]("ShowB");
}
public static void main(String args[])
{

ABtest obj=new ABtest();


[Link]();
[Link]();

}
Program-4 to demonstrate interface

interface A
{
void show();
}
interface B
{
void show();
}

class ABmultipleinheritance implements A,B


{
public void show()
{
[Link]("Hello");
}

public static void main(String args[])


{

ABmultipleinheritance obj=new ABmultipleinheritance();


[Link]();

Common questions

Powered by AI

A Java class can implement multiple inheritance by implementing multiple interfaces. While a class cannot inherit from more than one class due to the single inheritance model in Java, it can implement multiple interfaces, thereby achieving multiple inheritance in terms of behavior. An example from the document is 'class ABtest' which implements interfaces 'A' and 'B'. Each interface defines a method ('showA()' and 'showB()'), and 'ABtest' provides the implementation for both methods, thus inheriting behavior from both interfaces .

Interfaces contribute to achieving loose coupling in Java by defining a set of abstract methods that other classes can implement, without specifying how these methods should be executed. This allows different implementations to be created, switched, or extended without modifying code that uses the interfaces. For instance, the document provides examples such as the 'Banktest' interface, which is implemented by classes like 'PNB', 'SBI', and 'HDFC'—each providing a different implementation of the 'rateofinterest()' method. This design permits flexibility and interchangeability among classes at runtime, promoting loose coupling .

The main purposes of using interfaces in Java are to achieve abstraction and support multiple inheritance. Interfaces in Java are similar to abstract classes in that they both are used to achieve abstraction, allowing for the declaration of abstract methods. However, interfaces differ in that they cannot have method bodies, whereas abstract classes can include both abstract and non-abstract methods. While abstract classes do not support multiple inheritance, interfaces do, allowing a class to implement multiple interfaces. This supports loose coupling by defining a contract that other classes can implement .

A Java interface achieves complete abstraction because it can only contain abstract methods (without bodies) and static final variables. It defines a pure abstract contract that any implementing class must fully adhere to. Conversely, an abstract class provides partial abstraction as it can include both abstract and concrete methods, allowing some level of implementation. In this way, an abstract class can offer some default functionality, while an interface only describes the methods without providing any implemented behavior .

A Java interface is declared using the 'interface' keyword and can only contain abstract methods and static final variables, with all members being public by default. It cannot have method bodies and cannot be instantiated. On the other hand, an abstract class is declared using the 'abstract' keyword and can contain a mix of abstract methods (without a body) and concrete methods (with a body), as well as both static and non-static variables. Abstract classes can have non-public members and can be extended by other classes using the 'extends' keyword. Interfaces, however, are implemented using the 'implements' keyword and can extend only other interfaces, not classes .

Using interfaces to define contracts in Java allows for the specification of methods that must be implemented by any class that claims to adhere to the contract, independent of class hierarchy. This separates 'what needs to be done' (method signatures) from 'how it should be done' (implementation), promoting flexibility and allowing classes to implement multiple interfaces. In contrast, inheritance from abstract classes implements a 'is-a' relationship and can provide partial implementation, but binds the class to a specific hierarchy, limiting its ability to reuse code across unrelated classes. While interfaces offer a lightweight means of establishing common ground across diverse programs, they require implementing classes to provide complete behavior, placing more responsibility on the implementer to ensure consistent contract fulfillment .

The Java compiler automatically treats all interface methods as public and abstract, and all variables as public, static, and final, regardless of their explicit declaration in the code. This handling ensures that any class implementing the interface must define concrete implementations for all of the interface's methods, enforcing the contract established by the interface. This behavior is significant because it guarantees uniform implementation requirements across different classes, fostering consistency and reliability in the way interfaces are employed within Java applications .

The primary benefit of using interfaces in Java is the ability to implement multiple inheritance, which promotes loose coupling and flexibility in design. Interfaces allow for defining a contract without enforcing any specific implementation, facilitating easy extension and mutation of behaviors. However, the limitation lies in interfaces' inability to provide any default behavior or method implementation, which abstract classes can. Abstract classes can have member variables and methods with specific access modifiers, offering more control over usable functionalities. Thus, whether to use an interface or an abstract class depends on whether full abstraction and multiple behavior implementations or partial abstraction with some predefined behavior is needed .

In Java interfaces, methods are public by default, and variables are public, static, and final. This default access allows interface methods and variables to be accessed from any class that implements the interface, regardless of package context. This means any implementing class in a different package can still fully utilize the interface's defined methods and constants, promoting consistent adherence to the contract defined by the interface across various parts of a program .

Interfaces in Java simulate multiple behavior sharing by allowing classes to implement multiple interfaces, each requiring the implementation of specific methods. For instance, in the document, 'class ABtest' implements two interfaces 'A' and 'B'. Both interfaces require their respective methods, 'showA()' and 'showB()'. By implementing both, 'ABtest' shares behavior across both interfaces, thereby achieving a form of behavior sharing. Another example is 'class ABmultipleinheritance' which implements interfaces 'A' and 'B', both having the method 'show()'. This setup ensures the behavior defined in multiple interfaces can be provided in a single class .

You might also like