0
How interface provide 100% abstraction
Give one example for interface
5 Antworten
+ 1
tq but i need simple example and explaination
0
I dont think it'll provide 100% abstraction. But it define an absolute behavior where it needs to be implemented.
For example i have interface of animal, with default method of move.
public interface Animal{
public int move();
}
But the problem is each animal has different way to move, depends on leg and/or body structure. The user dont need to know the details and just want it to move. So i implement the interface to the relevant classes
class Dog implements Animal{
//something dog-ish
@Override
public int move(){
//calculate dog move
return v;
}
}
class Snake implements Animal{
//something snake-ish
@Override
public int move(){
//calculate snake move
return v;
}
}
This way the user dont need to worry which animal they try to move, because all of them can move and can be access through polymorphism
public void chase(int state, Animal a){
if(state!=DEAD)posNow+=a.move
}
0
Taste thankyou for your explanation
0
what is difference between abstract class and interface?
0
There are some rules in abstract class that doesnt applied in interface, like abstract class can have an actual method within it etc. You can find it easily.
The usage is almost same as my example above, the main difference is multiple inheritance is doable using more than 1 Interface. but not for abstract class, as it still recognize as a class because in java we cant inherit from more than 1 class.
https://www.sololearn.com/discuss/1588353/?ref=app