Interface vs Abstract | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Interface vs Abstract

If Interface and Abstract are almost same thing. So in, Real scenario, how should we decide whether we have to choose the Interface or Abstract Class.

14th Jan 2017, 4:34 PM
Ashish Kumar Singh
Ashish Kumar Singh - avatar
1 Answer
+ 4
Interfaces shows what object can do. They can only defined what operations it can perform. Example: Interface that show that object can be runned: interface Runnable { void run(); } Interfaces can not contain fields and have methods. They only shows, that someone who implemented it will be able to hold them. Anstract classes are used to create base structure. They can have fields/methods or define abstract ones. For example GameObject class. It may be created as abstract class, that contains his own properties(fields): public float x, float y; Also it can have methods like: public void moveTo(float x, float y) and more. Then, when you will try to create your own subclass, that is derrived from GameObject, you will be able to use it's methods. For example in update move your object.
14th Jan 2017, 4:41 PM
WittyBit
WittyBit - avatar