Novice Info: Difference between Classes, Interfaces in Java–Part 2


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.

   1: public class DrumBreakes{

   2:     public void applyBreaks(){

   3:         useDrumBreaks();

   4:     }

   5: }


   7: public class Car{

   8:     DrumBreaks breaks = new DrumBreaks();

   9:     public void stop(){

  10:         breaks.applyBreaks();

  11:     }

  12: }


   1: public interface Break{

   2:     public void applyBreak();

   3: }


   5: public class DrumBreak implements Break{

   6:     public void applyBreak(){

   7:         useDrumBreak();

   8:     }

   9: }


  11: public class BreakFactory{

  12:     public enum BreakType{

  13:         Drum, Disc, Power, Air

  14:     }

  15:     private BreakFactory(){}

  16:     private static instance = new BreakFactory();

  17:     private static BreakFactory getInstance(){

  18:         return instance;

  19:     }

  20:     public Break getBreak(BreakType type){

  21:         Break breaks;        

  22:         switch(type){

  23:         case Drum:

  24:             breaks = new DrumBreak();

  25:             break;

  26:         case Disc:

  27:             breaks = new DiscBreak();

  28:             break;

  29:         case Power:

  30:             breaks = new PowerBreak();

  31:             break;

  32:         case Air:

  33:             breaks = new AirBreak();

  34:             break;

  35:         default:

  36:             breaks = new DiscBreak();

  37:             break;

  38:         }

  39:         return breaks;

  40:     }

  41: }


  43: public class Car{

  44:     Break breaks = BreakFactory.getInstance().getBreaks(BreakType.Disc);

  45:     public void stop(){

  46:         breaks.applyBreak();

  47:     }

  48: }


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

   1: public interface Vehicle {

   2:     public void applyBreaks();

   3:     public void accelerate();

   4: }


   6: public class Car{

   7:     @Override

   8:     public void applyBreaks(){

   9:         applyDiscBreak();

  10:     }

  11:     @Override

12: public void accelerate(){

  13:         frontWheelAcceleration();

  14:     }

  15: }


  17: public class Bus{

  18:     @Override

  19:     public void applyBreaks(){

  20:         applyAirBreaks();

  21:     }

  22:     @Override

  23:     public void accelerate(){

  24:         backWheelAccelerate();

  25:     }

  26: }

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.

   1: Vehicle car = new Car();

   2: Vechcle bus = new Bus();

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

   1: public abstract class Plant(){

   2:     public void createFood(){

   3:         photosynthesis;

   4:     }

   5:     public abstract void getFood();

   6: }


   8: public class Hibiscus extends Plant{

   9:     @Override

  10:     public void getFood(){

  11:         fromFlower;

  12:     }

  13: }


  15: public class Tomato extends Plant{

  16:     @Override

  17:     public void getFood(){

  18:         fromFruit;

  19:     }

  20: }

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.

   1: public class Car{

   2:     public void accelerate(){

   3:         use gas

   4:     }

   5:     public void stop(){

   6:         apply breaks

   7:     }

   8: }


  10: public class TestCar{


  12:     public static void main(String []aa){

  13:         Car car = new Car();

  14:         car.accelerate();

  15:         car.stoop();

  16:     }

  17: }


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.


About Sharath

I am a passionate technologist, I have varied interests in Development, Testing and Maintenance phases of PDLC / SDLC. In my leisure I love to play with my daughter. I love eating out, hanging out with friends and reading blogs/articles.
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s