When and where should I use an interface [duplicate] - java

This question already has answers here:
When should I use an interface in java? [closed]
(12 answers)
Closed 9 years ago.
When and where should I use an interface ?

Use an interface when you want to define behavior but not provide an implementation.

When you want to separate what's done (method signature definition) from how it's done (method implementation). This is common when you have operations that might be implemented in different ways, but users would acknowledge a common abstracton for all of them.
See java.util.Collection package for examples. There are several implemetations of the java.util.List interface, but the method signatures are the same.

It is strange nobody mentioned the word "contract" (though the previous answer describes it). See for example:
Java interfaces - What exactly is in the contract?
All classes implementing an interface fulfill that interface's contract. This contract is often the only thing a client needs to know about seemingly different classes.
And of course - polymorphism, which is a very convenient way to deal simultaneously with all classes implementing the interface. You just write code using only one supertype - interface's type.

maybe start here?
The purpose of interfaces continued

Related

Why use Java interfaces when you have to redefine methods to override them? [duplicate]

This question already has answers here:
Is there more to an interface than having the correct methods
(17 answers)
Closed 1 year ago.
So, you create an interface and define some methods. Then if you implement the interface to a class you must override all of the methods of the interface. Then what is the purpose of an interface in this case, since you are just repeating yourself, rewriting methods?
Java interfaces help you define rules.
For example, if you had a class for animal, you would want every class implementing animal have a method for movement, and number of limbs, the way they emit sounds etc.
You needn't know beforehand what those methods would be, but you do want to establish those set of rules that make an animal, an animal.
To implement Polymorphism concept.

Advantages of Abstraction and Polymorphism in Java [duplicate]

This question already has answers here:
What is the difference between an interface and abstract class?
(38 answers)
Closed 7 years ago.
I was going through the concepts of abstraction in Java.
These are my understandings:
Abstraction is the method of presenting the signature of functions and hiding the implementation, leaving it to the users who can implement/extend the interface/abstract class.
This way we can achieve greater scope for less modification of code, re-usability.
We can closely relate the objects in real time to objects in program code.
These are my questions:
When an abstract class can behave like an interface when all the methods are made abstract, why do we need interface separately? Please explain with an example for better understanding.
Can we likely call Abstract class = Interface + Inheritance on a functionality basis? Because we can achieve the functionality of interface and inheritance together with Abstract class.
Simply saying: interface is a contract, abstract class is skeletal implementation. (Additionally, in Java interfaces are mostly used because it's not possible to extend multiple classes.)
Contract says what, implementation says how.
Example of interface: java.util.List. It has all methods that any list should have: add(), size(), indexOf() and so on.
Example of abstract class: java.util.AbstractList. Though it has many abstract methods, some List methods, that don't depend on the way elements are stored in the concrete list, are implemented there (addAll(), equals(), toString() and others). To create complete implementation, not all List methods should be implemented, thus making implementor's work easier.

Java: Why multiple interfaces instead of multiple inheritance? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Why does Java allow multiple inheritance from interfaces but not from abstract/concrete classes
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed
Instead of inheriting from multiple classes (which Java doesn't allow), why are we told to implement multiple interfaces instead?
Surely the point of inheriting from multiple classes is the inherit their functionality - if you have to manually re-insert functionality (for each class extending a set of interfaces) what's the point of using the interfaces? There's no guarantee that two classes implementing the same set of interfaces will provide the same functionality - or am I missing something?
Multiple inheritance is something that can cause multiple problems.
Interfaces are used to give abilities to instances of the class that implement them. I personally use interfaces with composition(using instance variables that are references to other objects) in order to provide functionality to my class that would otherwise be achieved with multiple inheritance.
In other words my class provides the functionality promised by the interface implemented but internally my class instance uses the instance variable to do the job.
"There's no guarantee that two classes
implementing the same set of
interfaces will provide the same
functionality - or am I missing
something?"
About your statement above:
Each method should adhere to a contract so no matter how you implement it the functionality of the method should always be the same if this is what is supposed to do. If it breaks the contract it means it was implemented wrongly.
multiple inheritance may lead to cyclic inheritance.. to avoid that we are going for interface based inheritance..
You should read about the diamond dependency problem http://en.wikipedia.org/wiki/Diamond_problem and to avoid this Java chose interfaces over extension of multiple classes

Java abstract class and interface [duplicate]

This question already has answers here:
Abstract class vs Interface in Java
(15 answers)
Closed 5 years ago.
In interview I have been asked following question. I tried to answer the question but I want exact answer of the question.
If I can simulate Abstract class as Interface, why java provided Interface?
This mean if in Abstract class I can mark all methods as abstract and then abstract class will work as interface, so why I need interface.
Can anyone explain me in brief.
That's a very standard interview question. The answer is: because you can implement multiple interfaces, but can't extend multiple abstract classes.
Example from the JRE: LinkedList is both a List and a Deque. These interfaces define the behaviour of the class. They do not provide any implementation details. While abstract classes could provide some.
Related questions: this and this. The latter is not directly related, but it shows why are interfaces needed, even in cases when an abstract class would suffice.
Interfaces define contracts & can define constants, but provide no implementation at all of the contracted methods.
Abstract classes can provide implementations of methods as well as member variables - if you want you can create an abstract class that defines everything except the fine-tuning you want in your concrete subclasses. You can't do this with interfaces, but you can implement multiple interfaces & extend only one parent class.
Both interfaces & abstract classes can be used to make use of concrete classes polymorphically.
Abstract classes do well to set default methods and set up the hierarchy. The issue is subclasses may only extend a superclass one-time. Interfaces on the other hand can extend each other multiple times and subclasses can implement any number of interfaces. This provides a lot of flexibility and affords the potential for change. Ideally, the can be combined i.e. abstract class implements interface1…interface2, best of both worlds.
The reason why interviewers ask this question is because your answer reflects your deep understanding of what a programming language (and a compiler) is. In particular, Java defines the concept of interface on top of (pure) abstract classes in order to (partially) support multiple inheritance (between interfaces). If this mechanism had not been introduced, we would have either no way of achieving some sort of multiple inheritance, or the big mess created by fully-fledged multiple inheritance in C++.
Answer
1) MULTIPLE INHERITANCE in java is achieved through interfaces.
2) If there is a situation where some explanation of a method is required, but not a full fledged one, the best way is to use abstract class.
3)Interfaces merely provide an AGREEMENT for the return type and the argument types.

Actual use of interface in java [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java Interfaces?
What is the use of interface in java? Is interface helps to provide multiple inheritance actually?
Interfaces are used as a contract.
If an Object implements an interface, it is guaranteed to offer the functionality specified in the interface.
Interfaces also allow you to treat Objects (that implement the interface) polymorphicly.
No, multiple inheritance is when you extend behaviours from more than an object.
Interfaces provide a safe way to declare what an object is able to do, not how it does it. That's why you can implement multiple interfaces.
They are used in many kinds of problems, for example when you want to give to your object the same capability that is orthogonal to what these object actually are: for example the interface Comparable states that object can be compared with other objects of the same type. This doesn't relate with what kind of object it is at all.

Categories