How interface provide 100% abstraction | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How interface provide 100% abstraction

Give one example for interface

24th Nov 2018, 3:11 AM
Sixface
Sixface - avatar
5 Answers
+ 1
tq but i need simple example and explaination
24th Nov 2018, 4:23 AM
Sixface
Sixface - avatar
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 }
24th Nov 2018, 4:35 AM
Taste
Taste - avatar
0
Taste thankyou for your explanation
24th Nov 2018, 9:05 AM
Sixface
Sixface - avatar
0
what is difference between abstract class and interface?
24th Nov 2018, 9:06 AM
Sixface
Sixface - avatar
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
24th Nov 2018, 9:35 AM
Taste
Taste - avatar