- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the purpose of interfaces in java?
An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.
To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.
All the methods of the interface are public and abstract and, we will define an interface using the interface keyword as shown below −
interface MyInterface{ public void display(); public void setName(String name); public void setAge(int age); }
Purpose of the interface
- Provides communication − One of the uses of the interface is to provide communication. Through interface you can specify how you want the methods and fields of a particular type.
- Multiple inheritance − Java doesn’t support multiple inheritance, using interfaces you can achieve multiple inheritance −
Example
interface MyInterface1{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface1"); } } interface MyInterface2{ public static int num = 1000; public default void display() { System.out.println("display method of MyInterface2"); } } public class InterfaceExample implements MyInterface1, MyInterface2{ public void display() { System.out.println("This is the implementation of the display method"); } public void show() { MyInterface1.super.display(); MyInterface2.super.display(); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.show(); } }
Output
display method of MyInterface1 display method of MyInterface2
Abstraction − Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.
Since all the methods of the interface are abstract and user doesn’t know how a method is written except the method signature/prototype. Using interfaces, you can achieve (complete) abstraction.
Loose coupling − Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.
You can achieve loose coupling in Java using interfaces −
Example
interface Animal { void child(); } class Cat implements Animal { public void child() { System.out.println("kitten"); } } class Dog implements Animal { public void child() { System.out.println("puppy"); } } public class LooseCoupling{ public static void main(String args[]) { Animal obj = new Cat(); obj.child(); } }
Output
kitten
- Related Articles
- What is the use of marker interfaces in Java?
- What is the purpose of System class in Java?
- What is the purpose of Runtime class in Java?
- What is the purpose of private constructor in Java?
- What is the purpose of a constructor in java?
- What is the purpose of Process class in Java?
- What is the purpose of using JLink in Java 9?
- What is the purpose of a default constructor in Java?
- What is marker or tagged interfaces in Java
- What are the SAM Interfaces in Java?
- What is the purpose of using a dumpStack() method in Java?
- What is the purpose of using Optional.ifPresentOrElse() method in Java 9?
- What is the purpose of overriding a finalize() method in Java?
- What are nested interfaces in Java?
- What is the difference between Externalizable and Serializable interfaces in Java?
