In my earlier post about Difference between Classes, Interfaces in Java I have explained about the differences in theory. Current post will be discussing about the practical differences that lets you make the decision on when to define a Class or Interfaces.
I am going to discuss about a 5 simple principles to be followed for creating a Class or an Interface. These principles will guide you in taking a right decision while defining the Types.
Rule 1: Define an Interface, when you want to create a contract between two subsystems
If you are developing an application, which involves interaction between two systems. Then creation of a contract, on how these two systems will communicate will help you minimize the complexity and enhances the maintainability.
Consider you have a Car Object and you have a subsystem that creates Breakes for your car. Due to upgrade in the technology & for safety it was mandated that all the Cars to have Disc/Power brakes. In this scenario if your Car Object is tightly coupled with the breaks object given by the sybsystem then you are mandated to upgrade the complete Car object. To avoid these scenarios you need to create subsystems based on a contract so that tey will be loosely coupled and the upgradation is simple.
Rule 2: Define an Interface, when you want to create a Group of similar objects, Define an Interface to create several types with same behavior
In the above example Vehicle is an interface defined to group all the types that are having same behavior. Car & Bus are two types that are having same behavior. Hence they are grouped together with a type called Vehicle which defines a behavior that needs to be adhered by all its children.
The above construct holds true if and only if the grouping of these types are defined as stated above.
Rule 3: Define an Abstract Class, when you want to define a specific behavior for a Group of objects, Define an Abstract Class when want to have a generic behavior for all of your subtypes
In the above example plant defines a generic behavior of doing photosynthesis, while each of its subtype uses this generic behavior to generate food of different types (Flower, Fruit).
Caution when defining subtypes by extending classes make a note of the java limitation that you will not be able to create a subtype inheritance from 2 or more classes. While you can create this type of multiple inheritance using Interfaces
Rule 4: Define a Class, when you have a fixed definition in your system.
Rule 5: Define a Final Class, if you don’t want the behavior to be overridden
Create a final class if and only if you do not want to create any subtypes of that type and the behavior should be overridden. An example of Final class in Java is java.lang.Math class.
All the above examples and explanations are just based on my views with my experience and should not be considered as concrete rules for designing a system.
Thanks for the interest on the content, and hope the information is helpful and useful. Do share your feedback in Comments section.