KEMBAR78
Interface | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
18 views15 pages

Interface

An interface in Java is a collection of abstract methods and static constants, serving as a blueprint for classes to achieve abstraction and multiple inheritance. Interfaces cannot be instantiated and must be implemented by classes, which must provide definitions for the abstract methods. There are different types of interfaces, including functional interfaces with a single abstract method and marker interfaces that do not contain any methods or fields.

Uploaded by

pyvbvaraprasad
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)
18 views15 pages

Interface

An interface in Java is a collection of abstract methods and static constants, serving as a blueprint for classes to achieve abstraction and multiple inheritance. Interfaces cannot be instantiated and must be implemented by classes, which must provide definitions for the abstract methods. There are different types of interfaces, including functional interfaces with a single abstract method and marker interfaces that do not contain any methods or fields.

Uploaded by

pyvbvaraprasad
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
You are on page 1/ 15

What is Interface in Java?

 An interface is a collection of abstract method(methods without definition). We can't


instantiate an interface.
 An interface in Java is a blueprint of a class. It has static constants and abstract methods.
 The interface in Java is a mechanism to achieve abstraction
 There can be only abstract methods in the Java interface, not method body.
 It is used to achieve abstraction and multiple inheritance in Java
 Java Interface also represents the IS-A relationship.
 It cannot be instantiated just like the abstract class.
 A class implements an interface using the implements clause
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
Why use Java interface?
 There are mainly three reasons to use interface. They are given below.
 It is used to achieve abstraction.
 By interface, we can support the functionality of multiple inheritance.
 It can be used to achieve loose coupling.
 An interface is a collection of abstract methods and final variables
 The Java compiler adds public and abstract keywords before the interface method.
 Moreover, it adds public, static and final keywords before data members.
 Interface only contains the final fields and abstract methods not the regular methods.
Interface also don't contain constructor.
 The member of the class may or may not be private but the members of the interface can
never be private.
 The abstract method of the interface must be overridden in its child classes and the
interface supports the concept of multiple inheritance which was not supported using the
classes.
 An interface can't be extended by class, it can only be implemented by class.
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is
known as multiple inheritance.
// Different types of members in a class?
i)data members/ attributes [static/non-static, final/non-final]
ii)methods [static/non-static, final/non-final]
iii)constructors
Note: We can declare and create instances for a class

//Different types of members in abstract class?


i)data members/ attributes [static/non-static, final/non-final]
ii)methods [abstract(non-static,non-final))/non-abstract (static /non-static, final/non-final)]
iii)constructors
Note: We can declare objects of an abstract to make a reference to one of its sub class instance

//An interface is purely an abstract class (only abstract methods)


i) data members (but by default they are final and static)
ii) No constructors
iii) Methods ( by default they are abstract,non-static,public)
Note: We can't declare objects
Interfaces gives information about what should be implemented.
//But never gives info about how it should be implemented
Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final
variables.

Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static
and final variables.

Implementation: Abstract class can provide the implementation of the interface. Interface can’t provide the
implementation of an abstract class.

Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract
class can be extended using the keyword “extends”.

Multiple implementations: An interface can extend another Java interface only, an abstract class can extend another
Java class and implement multiple Java interfaces.

Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class
members like private, protected, etc.
When to use what?

Consider using abstract classes if any of these statements apply to your situation:

 In the java application, there are some related classes that need to share some lines of code then you can put these lines of
code within the abstract class and this abstract class should be extended by all these related classes.
 You can define the non-static or non-final field(s) in the abstract class so that via a method you can access and modify the
state of the object to which they belong.
 You can expect that the classes that extend an abstract class have many common methods or fields, or require access
modifiers other than public (such as protected and private).

Consider using interfaces if any of these statements apply to your situation:

 It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this
interface.
 A class can implement more than one interface. It is called multiple inheritances.
 You want to specify the behavior of a particular data type but are not concerned about who implements its behavior.
Interfaces References

package vit.demo; class Ece implements University{


interface University{ public void use(String dept) {
int hall=301; System.out.println(dept+" using "+hall);
void use(String dept); }
} }
class Cse implements University{ class Main {
@Override public static void main(String ar[]) {
public void use(String dept) { University obj = new Cse();
System.out.println(dept+" using "+hall); cse.use("bcd");
} obj = new Ece();
} ece.use("bec");
}
}
Extending Interfaces
• Just as classes can be inherited, interfaces can also be inherited
• One interface can extend one or more interfaces using the keyword extends
• When you implement an interface that extends another interface, you should
provide implementation for all the methods declared within the interface
hierarchy
Extending Interfaces

package vit.demo; class Report implements Employee {


interface Person{ @Override
void personalDetails(); public void personalDetails() {
} System.out.println("my name");
interface Employee extends Person{ }
void jobDetails(); @Override
} public void jobDetails() {
System.out.println("manager");
}
}
Multiple Inheritance

package vit.demo; class Report extends Employee implements Person {


interface Person{ public void personalDetails() {
void personalDetails(); System.out.println("my name");
} }
class Employee { }
void jobDetails(){ class Main {
System.out.println("manager"); public static void main(String ar[]) {
} Report report = new Report();
} report.personalDetails();
report.jobDetails();
}
}
Types of Interfaces
1.Functional Interface
2.Marker interface
1. Functional Interface:
•Functional Interface is an interface that has only pure one abstract method.
•It can have any number of static and default methods and also even public methods
of java.lang.Object classes
When an interface contains only one abstract method, then it is known as a Functional Interface.
Examples of Functional Interfaces:
•Runnable : It contains only run() method
•ActionListener : It contains only actionPerformed()
•ItemListener : It contains only itemStateChanged() method

2. Marker interface
• An interface that does not contain any methods, fields, Abstract Methods, and any Constants is Called a
Marker interface.
• Also, if an interface is empty, then it is known as Marker Interface.
• The Serializable and the Cloneable interfaces are examples of Marker interfaces.
Examples of Marker Interfaces :
java.lang.Cloneable
java.io.Serializable
java.rmi.Remote

You might also like