Monday 13 August 2018

Abstract class vs Interface : Use case scenarios


                Abstract class                                                                  Interface 


Most of the properties of these are same, except 

1. Its a class, use "Extends" to inherit it                                         1. Its an interface, use "implements" to inherit it

2. You can have some implement/unimplemented function          2. all functions are bydefault public abstract

3. You can have constructor                                                           3. No constructor allowed


4. Fields/attributes allowed                                                             4. Only constant allowed, Not fields/variable 



Use case scenarios 


                                                                     Interface

1.  To provide some special(HAS-A) capabilities to its all may  be unrelated children, like Employee can be "Serialization"  and Animal can be "Serialization" , after implementing "Serializable" interface both HAS-A property/capability of                 being serialized implementer, 
  
 2. as well as your base has no meaning on it's own
3. You want to define some sort contract/set some fixed behavior/capabilities for all your implementer , walk-able, runnable,flyable etc


                                                                     Abstract

1. IS-A type relation, Dog is Animal, Nano is a Car
2. When you want to reuse the existing code, make inherit properties of you parent and have some of your own.

No comments:

Post a Comment