Object Oriented Design Principles in Java

In this article, we will learn the Object-Oriented Design Principles/Guidelines which can be applied in our day to day project work. It's important to learn basics of Object-oriented programming like AbstractionEncapsulationPolymorphism, and Inheritance. But, at the same time, it's equally important to know object-oriented design principles, to create the clean and modular design. These guidelines helps design strong object-oriented software design.
Let's list important Object-Oriented Design Principles and we will discuss each guideline in detail.

Encapsulate What Varies

  • If you know your code is going to change in future then separate it out.
  • The benefit of this OOPS Design principle is that It's easy to test and maintain proper encapsulated code.
  • Only one thing is constant in software field and that is "Change", So encapsulate the code you expect or suspect to be changed in future.
  • If you are coding in Java then follow the principle of making variable and methods private by default and increasing access step by step e.g. from private to protected and not public. Several of the design patterns in Java uses Encapsulation, Factory design pattern is one example of Encapsulation which encapsulates object creation code and provides flexibility to introduce a new product later with no impact on existing code.

Code to an Interface Rather Than to an Implementation

  • Always program for an interface and not for implementation this will lead to flexible code which can work with any new implementation of an interface.
  • Use interface type on variables, return types of method or argument type of methods in Java. This has been advised by many Java programmer including in Effective Java and Head First design pattern book.

Delegation Principle

  • Don't do all stuff by yourself, delegate it to respective class. Classical example of delegation design principle is equals() and hashCode()method in Java.
  • In order to compare two objects for equality, we ask class itself to do comparison instead of Client class doing that check. The benefit of this design principle is no duplication of code and pretty easy to modify behaviour.
Read how delegation works in Java on Delegation in Java

The Open Closed Principle (OCP)

  • Classes should be open for extension but close for modification.
  • Classes, methods or functions should be Open for extension (new functionality) and Closed for modification.
  • The benefit of this Object oriented design principle is, which prevents someone from changing already tried and tested code.
Read more on Open Closed Principle

Dry- Don't Repeat Yourself

  • Avoid duplicate code by abstracting common things and place them in a single location.
  • Use an abstract class to abstract common things in one place. If you have a block of code in more than two places consider making it a separate method, or if you use a hard-coded value more than one time make them public static final constant.
  • The benefit of this Object oriented design principle is in maintenance.

Single Responsibility Principle (SRP)

  • Every object in our web application should have a single responsibility, and all object's services should be focused on carrying that single responsibility(SRP).
  • If you put more than one functionality in one Class in Java it introduces coupling between two functionality and even if you change one functionality there is a chance you broke coupled functionality, which requires another round of testing to avoid any surprise on the production environment.

Liskov's Substitution Principle (LSP)

  • A subclass should be suitable for their base classes.
  • According to the Liskov Substitution Principle, Subtypes must be substitutable for supertype i.e. methods or functions which uses superclass type must be able to work with the object of subclass without any issue".
  • LSP is closely related to Single responsibility principle and Interface Segregation Principle. If a class has more functionality than subclass might not support some of the functionality and does violate LSP.

Interface Segregation Principle (ISP)

  • Interface Segregation Principle stats that, a client should not implement an interface if it doesn't use that.
  • This happens mostly when one interface contains more than one functionality, and the client only needs one functionality and no other.Interface design is a tricky job because once you release your interface you can not change it without breaking all implementation.
  • Another benefit of this design principle in Java is, an interface has the disadvantage to implementing all method before any class can use it so having single functionality means less method to implement.

Dependency Injection or Inversion Principle

  • Don't ask for dependency it will be provided to you by a framework. This has been very well implemented in Spring framework, the beauty of this design principle is that any class which is injected by DI framework is easy to test with the mock object and easier to maintain because object creation code is centralized in the framework and client code is not littered with that.
  • There are multiple ways to implemented Dependency injection like using bytecode instrumentation which some AOP (Aspect Oriented programming) framework like AspectJ does or by using proxies just like used in Spring. See this example of IOC and DI design pattern to learn more about this SOLID design principle.

Related Posts



  1. Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.

    white label website builder

  2. Thanks for the useful information, give more updates like First time I visit your site really nice, here after a daily visit.
    ecommerce website development company in chennai

  3. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me

    website builder for reseller

  4. Comfortabl y, the post is really the freshest on that deserving topic. I harmonise with your conclusions and definitely will thirstily look forward to your next updates.

    private label website builder

  5. Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.

    white label website builder

  6. Always so interesting to visit your site.What a great info, thank you for sharing.
    Visit: Web Design Minneapolis

  7. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website designing Service

  8. Good Product Design draws in even more clients thereby offering an organisation a hedge above other rivals out there.It is likewise important since it brings together three essential concerns, the principle, the package as well as the procedure.Good product design is essential in changing out-of-date designs.

  9. You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.

    Artificial Intelligence Training in Bangalore

  10. Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

    Digital Marketing Training in Bangalore

  11. I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.

    Data Science Training in Bangalore

  12. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.

    Machine Learning Course in Bangalore

  13. Thanks for sharing this blog with us. Great blog and knowledgeable content. I feel happy to read this blog. Keep sharing more blogs with us.
    Online Data Science Training in Hyderabad


Post a Comment