0
plzzzzzz don't give any theoretical answer..
what is the need of inerface ? i don't want theoretical answer but programming
2 Respostas
+ 4
Example:
class Animal{
int size; // and other things generic to all animals
}
interface Pettable{ // relevant to some but not all animals
// include specific pettable behaviours
}
class Cat extends Animal implements Pettable{
// cat specific things
}
class Lion extends Animal{ //wouldn't want to pet a Lion
// lion specific things
}
https://code.sololearn.com/cvC4yLbL85Ao/#java
Explanation:
An interface describes properties that can be reused by other classes.
Since a class can only subclass one other class, but can implement many interfaces, this gives you a way to reuse common behaviours.
Explanation of the example:
Let's say you have a class Animal, which is extended by different types of animals like Cat, Dog, Lion, Hippo, etc.
All animals would have a size, food, etc. which they could inherit from Animal class.
But not all animals are domesticated or are "Pettable" so this could be an interface implemented only by certain animals (the dog and cat, not the lion).
0
little bit advance