This question already has answers here:
Closed 12 years ago.
I have a class which inherits from two different interfaces. Both interfaces declare a method with the same name. How can I provide a different implementation for each interface ?
In C#, the answer is there, but it does not work in java:
Inheritance from multiple interfaces with the same method name
I thought about providing a union implementation which uses type comparison but it's kind of ugly.
Thanks
EDIT : closed, my question was a duplicate of the following, thank you for the answers !
Java - Method name collision in interface implementation
You can't. Interfaces describe behavior, but they don't implement it. So if you implement a method, there's no way to tell which interface you are implementing it from.
No, there's no equivalent feature in Java.
And you can't do it yourself, because inside the method, you have no way of telling if the calling code is referencing the object as InterfaceA or InterfaceB. Even if you could, I think it would be a bad idea.
Related
This question already has answers here:
What is the "default" implementation of method defined in an Interface?
(3 answers)
Closed 6 years ago.
I was studying lambada and there was a point which states that in java 8 we can declare a method with definition in interfaces like
interface Test {
default String method(){
return "string";
}
}
and as per specification we can use two methods with same signature but depends on programmer how he wants to use it?
Now the question is same task can be if achieved by using definition not declaration then what's the point of using default method?
like they behave same as regular method definition and programmer need to declare body and rest part?
what is the actual point as it seems a bit hard to grasp
thanks #ElliottFrisch and #kagemusha for hint after searching i got the answer
Why default methods?
List<?> list = …
list.forEach(…); // lambda code goes here
The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.
So it’d be really frustrating if we have lambdas in Java 8 but couldn’t use those with the standard collections library since backwards compatibility can’t be sacrificed.
Due to the problem described above a new concept was introduced. Virtual extension methods, or, as they are often called, defender methods, can now be added to interfaces providing a default implementation of the declared behavior.
Simply speaking, interfaces in Java can now implement methods. The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.
It doesn’t seem to be the language feature that would be appropriate to use every day, but it seems to be essential for Java Collections API update to be able to use lambdas naturally.
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 6 years ago.
I can't understand a simple thing.
example: A is an interface, B is a class that implement correctly A.
What dose exactly mean doing:
A name = new B(some_argument);
I see lot of people using this instead of
B name = new B(some_argument);
But I can't figure out what dose that mean making an object with an interface.
And WHY they do this? There are some differences between them?
Sometimes one is better than other?
for example the opposite dose not work
B name = new A();
technically both works.
The reason most people use the first solution (and you should do that too) is that an interface defines a certain contract / API for a type of classes.
That means an interface is used when you dont know the implementation of it or the implementation is exchangeable.
So coding against the interface and not against the real specific implementation is the best way to make your code exchangeable and generic which helps you to modularize your code, keep hard-linked-dependency between classes as low as possible and make your code maintainable.
Also an interface can be used to add additional functionality to a class as you can implement multiple interface but only extend one class.
A practical example:
Think of an SDK for a plugin. It defines some interface.
How do you want to call a function of a class implementing this interface when the implementation of that interface is done by an other guy wrinting a plugin two years after you developed your application/sdk? ;)
This question already has answers here:
What is the difference between an interface and abstract class?
(38 answers)
Closed 7 years ago.
So I'm currently reading my java book and it doesn't seem to be to clear on the abstract classes and interfaces. Here is my understanding: Abstract classes are created to basically be the most generic form of a superclass, one of which no instance can be created, and an interface contains methods to be implemented by subclasses? Any help on what I'm missing to these aspects of coding. It would be greatly appreciated, thanks!
(I'm not asking what is different between the two, I just want an understanding of what each is)
Well, all that interfaces do are state required implementation. A contract, if you will, that the inheriting class will implement their own versions of those methods with the same parameters and return values.
Abstract classes are similar, except they can implement generic implementation without requiring the inheriting classes to implement it.
Another difference is, that one class can implement multiple interfaces but only inherit from one possibly abstract class.
That's my understanding of it anyways. Hope I helped!
This question already has answers here:
Why is method overloading and overriding needed in java? [duplicate]
(2 answers)
Closed 8 years ago.
Recently I was asked this question "why should one override a method? "
I replied, if I have a class with 10 methods and I want to use all of its functionality except one method, then I will override that method to have my own functionality.
Then the interviewer replied in that case why cant we write a new method with a different name and use that method instead.
Yes this is also right. Now I am confused. What is the real objective in overriding a method?
Can anyone please tell me? Thank you all in advance.
If you will give another name to the method in derived class, you cant invoke it with same interface. You can always invoke it through base class pointer.
i.e.
Base p = new Derived();
p.overrideMethod();
If Derived class is derived from Base then it will automatically call the derived version and not of Base. In case of different name, it is not possible. It is called code against interfaces and not implementations.
.
why cant we write a new method with a different name and use that method instead
It is because we want to use polymorphism. You could tell the interviewer this example: There is a module that calls specific methods on objects you give it; now imagine you can't change that module (e.g. no source). You can't tell it to use a different method but you can give it an object of a subclass which has overridden that method. To the module it will appear that nothing changed.
In practice it is also often the case that you could change that module but dont want to.
I replied, if I have a class with 10 methods and I want to use all of
its functionality except one method, then I will override that method
to have my own functionality.
=> Very often a way to break the Liskov Substitution principle ... => very bad OO design
You have many examples on the web of this "break" but a you can find a good explanation here.
The benefit of overriding is: ability to define a behavior that's specific to the subclass type which means a subclass can implement a parent class method based on its requirement.
One uses interfaces to allow for multiple implementations and one uses overriding to simplify the implementation of an interface (e.g. when implementing a WindowListener, one typically extends and overrides a method of WindowAdapter so that one does not need to provide definitions for the cases where the default behavior is sufficient). Adding a new method rather than overriding would not work in this case, because the caller understands the interface and invokes its methods; the whole point of overriding here is to change the behavior for the calls to the interface. If you simply added a new function, then the caller would have to know about it, which defeats the entire isolation between the consumer of a piece of functionality and the provider of that functionality which is what interfaces are intended to provide.
Overriding is a feature that is available while using Inheritance.
It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.
In such cases we can create methods with the same name and signature as in the parent class. This way the new method masks the parent method and would get invoked by default.
The main objective of overriding is code reuseablity which can be advatageous in big projects,it also provide flexiblity means you can pass different sets of input from any class and get the output
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interface vs Abstract Class (general OO)
I want to know the difference between an Interface and an Abstract class ?
I'm so confused about this.
Thanks already.
In an interface you simply define the methods that you are going to implement. In an abstract class you can actually write methods that contain some code. I'm sure this question has been asked a thousand times so look at some of the other posts.
Interfaces define contracts. Abstract classes provide for code reuse. An object interacts with other objects via their contracts (Interfaces). An object shares code with other, related, objects, by inheriting it from an abstract class.