+ 3
Why have interfaces in Java?
I was studying for my final and watching a video that said anything that an interface can do can also be done with abstraction…so why have interfaces? I hadn’t even thought about it before, but abstract classes and interfaces do seem really similar so can anyone explain why we need both or how to know which one to use?
3 Respuestas
+ 17
A class can inherit only one abstract class, but it can inherit unlimited number of interfaces.
Sometimes you cannot extend an abstract class for some reason. This is when interfaces come in handy.
That's why we need both.
0
an interface is a little more abstract than an abstract class which eg. can use non-final member fields (variables).
So in order abstract -> concrete is
.interface used first for general list of methods and
.abstract class for partially implemented approximation for programers comfort
https://code.sololearn.com/cBZ58X6SN53O/?ref=app
0
Since java does not support multiple classes of inheritance to accomplish this
Java introduced a concept called interface
An interface can extends any no of interfaces
A class can imolements any no of interfaces
Interfaces that are not having any kind of methods are called markable market interface or tagging interface
Ex Remote
Interfaces having only one method called as functional interface
Ex Runnable
Every variable declared in interfaces are by default final
Every method is by default public scope
Interface is used if every feature is changing for each object
Subclass have to implement abstract methods defined in interface
If subclass is not implementing all abstract methods the subclass should be abstract
We can to create another subclass to implement remaining abstract methods
Ex
interface Welcome
{
void test();
}