answersLogoWhite

0

Why do you use interfaces in java?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

An interface is used in java to outline specific behaviors of a group of classes, i.e. Comparable (able to be compared). These common behaviors translate into methods that are all public and unimplemented (since an interface lists what can be done not how). An interface is usually used when it doesn't make sense for something to be a class but it does have general behaviors that other classes should implement, i.e. there is not much sense to implement a Comparable class, since many different objects have vastly different ways to compare each other; thus, the behavior is outlined, and future classes can implement said behaviors (the methods).

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

An interface is a contract that guarantees that classes which implement the interface will have certain methods available. This allows the calling class to use the given object's methods without having to know details about its type.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

we use interface in java to implement the concept of late binding.

LATE BINDING:the concept of binding of the functionalites to the function definitions at run-time is know as late binding

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It is advisable to design relatively large applications using interfaces because it makes the whole system easier to modify, extend and integrate new features. To start with you may only have one implementation of a given interface, but if you find you need slightly different behaviour in special circumstances, you only need write a class that conforms to one of the existing interfaces and it will drop in place without major modifications.

Interfaces also allow you to adapt a class from a different hierarchy to work in an existing application. The class only needs to declare it implements the interface, provide the necessary methods and it can be integrated directly as if it were created for the job.

The above describes a good reason to use Interfaces, but does not explain why Java has interfaces in the first place (which is what the question really is asking).

Java uses Interfaces to simulate the concept of Multiple Inheritance. As Java itself is a strictly Single Inheritance design, specifically to avoid the problems that Multiple Inheritance can cause, the addition of Interfaces was made to gain as many of the advantages of M.I. without the corresponding faults.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Interfaces are a way to make your code more flexible.Interfaces are used to achieve "Polymorphic" behavior. Interface is just like Java class but it contain only abstract method and final attribute.

For more information, you can follow-

marcus-biel.com/java-interfaces/

YouTube.com/watch?v=8ybdWHKSlP8

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

With interfaces, you can be sure that a class implements a particular behavior. For example, if a class has the "comparable" interface, you can be sure that objects based on the class can be sorted, by calling appropriate methods to determine whether one object should be placed before or after another object.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you use interfaces in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the minimum number of interfaces that a java class can have?

Zero. By default they do not implement any interfaces.


Can a single Java class subclass extend both InputStream and Reader. True or False?

Java does not have multiple inheritance, so no. Java can use multiple interfaces, though, with the "implements" keyword.


Need of interface in java?

Interfaces are used in Java to accomplish most of the goals of Multiple Inheritance. For several reasons, Java only supports Single Inheritance for classes - i.e. a class can have only a single parent. The use of Interfaces is how Java attempts to implement most of the positives of the concept of Multiple Inheritance while avoiding its pitfalls.


What is Abstract-Interface in java?

All interfaces are abstract.


What are the alternatives to inheritence in java?

Java does not support multiple inheritance; a subclass cannot have more than one parent. Java compensates for this with interfaces. A class can implement multiple interfaces, but can only extend one class.


Give the structure of multiple inheritance?

Java does not support multiple inheritance. It is done with the help of interfaces in java. a class can implement n number of interfaces, thus showing multiple inheritance. but a class cannot extend multiple classes in java.


How can i see all the classes and interfaces present in a predefined package in java in command prompt?

use the jar command like this: jar tvf mypackage.jar


Why is interfaces used in java?

Interfaces are a way of imposing a type of functionality on all other java classes that are created using it. It is kind of a template that all child classes using this template must follow. All methods that are declared in an interface must be implemented by the child classes and hence the functionality offered by these classes can be controlled using them. Interfaces are a powerful tool that java provides to achieve multiple inheritance.


Which one is more advanced core java or java?

The Core Java technologies and application programming interfaces (APIs) are the foundation of the Java Platform, Standard Edition (Java SE). They are used in all classes of Java programming, from desktop applications to Java EE applications. Well What exactly you mean by Java?


Difference between awt and java script?

AWT is a Java package for creating graphical user interfaces. JavaScript is a completely unrelated programming language.


How do you achieve multiple inheritence using class in java?

Multiple Inheritance cannot be achieved only by using Classes in Java. You would have to use Interfaces as well to achieve multiple Inheritance. Java as such does not support direct multiple inheritance. We can have theoretical multiple inheritance by using interfaces using which you can outline the kind of functionality your child classes can have. For example you can have a declaration like this public class A implements X, Y, Z { } Here this class A would have to implement the methods that are declared in the interfaces X, Y & Z. So the outline of the functionality that A would have can be found by checking the interfaces but the exact implementation would depend on the programmer who codes class 'A'


When was the java virtual machine software created?

The Java platform, which consists 3 major parts: Java programming language, Java Virtual Machine (JVM), and several Java Application Programming Interfaces (APIs), was developed at Sun Microsystems in the early 1990s.