{"id":8697,"date":"2023-05-05T14:25:42","date_gmt":"2023-05-05T08:55:42","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=8697"},"modified":"2023-05-05T14:25:43","modified_gmt":"2023-05-05T08:55:43","slug":"solid-principles-java","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/solid-principles-java\/","title":{"rendered":"SOLID Principles Java"},"content":{"rendered":"\n<div class=\"gutentoc tocactive ullist\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#what-is-java\">What is Java?<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#what-is-java-used-for\">What is Java used for?<\/a><\/li><li><a href=\"#java-features\">Java Features<\/a><\/li><\/ul><li><a href=\"#what-are-the-solid-principles-in-java\">What are the SOLID Principles in Java<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#what-is-the-meaning-of-solid\">What is the meaning of SOLID?<\/a><\/li><li><a href=\"#benefits-of-using-solid-principles-\">Benefits of using SOLID principles&nbsp;<\/a><\/li><\/ul><li><a href=\"#solid-principles-java\">SOLID PRINCIPLES JAVA<\/a><\/li><li><a href=\"#1-single-responsibility-principle\">1. Single responsibility principle:<\/a><\/li><li><a href=\"#2-open-closed-principle\">2. Open-Closed Principle<\/a><\/li><li><a href=\"#3-liskov-substitution-principle\">3. Liskov substitution principle<\/a><\/li><li><a href=\"#4-interface-segregation-principle\">4. Interface segregation principle<\/a><\/li><li><a href=\"#5-dependency-inversion-principle\">5. Dependency Inversion Principle<\/a><\/li><li><a href=\"#conclusion\">Conclusion<\/a><\/li><li><a href=\"#frequently-asked-questions\">Frequently Asked Questions<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#q1-how-are-solid-principles-implemented-in-java\">Q.1: How are solid principles implemented in Java?<\/a><\/li><li><a href=\"#q2-are-solid-principles-still-relevant\">Q.2: Are SOLID principles still relevant?<\/a><\/li><li><a href=\"#q3-how-do-you-write-solid-code\">Q.3: How do you write SOLID code?<\/a><\/li><\/ul><li><a href=\"#additional-resources\">Additional Resources<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<p>In this article, we are going to learn about the Solid principles of Java. We will walk you through the following topics: What is Java, What are Solid principles in Java, the Importance of solid principles in Java, the meaning of SOLID in SOLID principles, the benefits of using SOLID principles in Java, explaining all 5 solid principles one by one, Also, many more frequently asked questions about solid principles in java like How are solid principles implemented in java, are solid principles still relevant, how do you write solid code.\u00a0<\/p>\n\n\n\n<h2 id=\"what-is-java\">What is Java?<\/h2>\n\n\n\n<p>Sun Microsystems first introduced Java in 1995 as a programming language and computing platform. It has grown from humble origins to power a significant portion of today&#8217;s digital world by offering a secure platform on which many services and applications are built. Java is still used in new, innovative goods and digital services that are being developed for the future. OAK was the original name for the Java programming language. It was originally designed to handle portable devices and set-top boxes. Oak was a colossal flop. Sun renamed the language &#8220;Java&#8221; in 1995 and updated it to focus on the developing www (World Wide Web) development industry. Oracle Corporation later purchased Sun Microsystems in 2009, gaining control of three core Sun software assets: Java, MySQL, and Solaris.<\/p>\n\n\n\n<h3 id=\"what-is-java-used-for\">What is Java used for?<\/h3>\n\n\n\n<ul><li>It is used for developing Android Apps.<\/li><li>Wide range of Mobile Java Applications.<\/li><li>Helps in creating enterprise applications.<\/li><li>Scientific Computing Applications.<\/li><li>Creating web apps.<\/li><li>Used for Server-Side Technologies like Apache, JBoss, GlassFish, etc.<\/li><li>Use for Big Data Analytics<\/li><\/ul>\n\n\n\n<h3 id=\"java-features\">Java Features<\/h3>\n\n\n\n<ul><li>It is one of the most user-friendly programming languages.<\/li><li>Java is platform-independent. Some programs developed in one machine can be executed in another machine.<\/li><li>Java is an object-oriented programming language which means that In Java, everything is an Object. Java can be easily extended since it is based on the Object model.\u00a0<\/li><li>It is a multithreaded language with automatic memory management. It is feasible to develop programs that can accomplish multiple tasks at the same time using Java&#8217;s multithreaded capability. Developers can use this design aspect to create interactive applications that run smoothly.<\/li><li>Java is Dynamic in nature Because it is meant to adapt to a changing environment, Java is considered more dynamic than C or C++. Java programs can store a large quantity of run-time data that can be used to verify and resolve object accesses in real time.<\/li><\/ul>\n\n\n\n<p>Read More: <a href=\"https:\/\/www.interviewbit.com\/blog\/features-of-java\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Java Features<\/strong><\/a><\/p>\n\n\n\n<h2 id=\"what-are-the-solid-principles-in-java\">What are the SOLID Principles in Java<\/h2>\n\n\n\n<p>SOLID principles are an object-oriented approach to software structure design that is used in Java. Robert C. Martin is the one who came up with the idea (also known as Uncle Bob). These five principles have revolutionized the field of object-oriented programming, as well as the way software, is written. It also guarantees that the software is modular, understandable, debuggable, and refactorable.&nbsp;<\/p>\n\n\n\n<h3 id=\"what-is-the-meaning-of-solid\">What is the meaning of SOLID?<\/h3>\n\n\n\n<ul><li><strong>S: <\/strong>Single responsibility principle<\/li><li><strong>O:<\/strong> Open-closed principle<\/li><li><strong>L: <\/strong>Liskov substitution principle<\/li><li><strong>I:<\/strong> Interface segregation principle<\/li><li><strong>D:<\/strong> Dependency inversion principle<\/li><\/ul>\n\n\n\n<h3 id=\"benefits-of-using-solid-principles-\"><span id=\"benefits-of-using-solid-principles\">Benefits of using SOLID principles&nbsp;<\/span><\/h3>\n\n\n\n<p>When you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you to develop software that can be managed easily.<\/p>\n\n\n\n<ul><li><strong>Clean:<\/strong> SOLID principles make code clean and standard code.<\/li><li><strong>Maintainable:<\/strong> with the help of SOLID principles our code becomes more manageable and easy to maintain.<\/li><li><strong>Scalable:<\/strong> Easy to refactor or change code.<\/li><li><strong>Redundancy: <\/strong>SOLID principles avoid redundant code.<\/li><li><strong>Testable:<\/strong> can be easily unit tested.<\/li><li><strong>Readable:<\/strong> SOLID principles make the code easy and readable.<\/li><li><strong>Independent:<\/strong> code becomes independent by reducing dependencies.<\/li><li><strong>Reusable:<\/strong> code becomes reusable.<\/li><\/ul>\n\n\n\n<h2 id=\"solid-principles-java\">SOLID PRINCIPLES JAVA<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img  loading=\"lazy\"  width=\"1024\"  height=\"797\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"SOLID PRINCIPLES JAVA\"  class=\"wp-image-8699 pk-lazyload\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 1024px) 100vw, 1024px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-1024x797.png\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-1024x797.png 1024w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-300x233.png 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-768x598.png 768w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-1536x1195.png 1536w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-2048x1594.png 2048w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-380x296.png 380w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-550x428.png 550w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-800x623.png 800w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2022\/05\/Image-06-1160x903.png 1160w\" ><\/figure>\n\n\n\n<p>Let&#8217;s understand all 5 principles(SOLID) one by one that is:&nbsp;<\/p>\n\n\n\n<h2 id=\"1-single-responsibility-principle\">1. Single responsibility principle:<\/h2>\n\n\n\n<p>According to the single responsibility principle, each Java class must perform only one function. Multiple capabilities in a single class mashup the code, and any changes made to the code may influence the entire class. It specifies the code and makes it simple to maintain.<\/p>\n\n\n\n<p>Let&#8217;s understand this by using an example:<\/p>\n\n\n\n<p>Suppose we have a class FinalExam that has 3 methods that perform 3 operations AddQuestion(), ExpectedAnswer(), Marksdistribution(). Now all these 3 methods perform different actions. By using the single responsibility principle, we can separate these functionalities into three separate classes to fulfil the goal of the principle.<\/p>\n\n\n\n<p><strong>Before implementing the Single responsibility principle:<\/strong>&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class FinalExam{\npublic void AddQuestion(){\n\/\/functionality of the method\n}\npublic void ExpectedAnswer(){\n\/\/functionality of the method \n}\npublic void Marksdistribution(){\n\/\/functionality of the method \n} \n}<\/pre>\n\n\n\n<p>The point of implementing the Single responsibility principle is not that a class can\u2019t have more than one method but it\u2019s recommended to use a single method for each class so that multiple capabilities in a single class don\u2019t mash up the code, and any changes made to the code may influence the entire class. Using a single responsibility principle makes code easy to maintain.<\/p>\n\n\n\n<p><strong>After implementing the Single responsibility principle:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class FinalExam{\npublic void AddQuestion(){\n\/\/functionality of the method\n}\n}\npublic class Answer{\npublic void ExpectedAnswer(){\n\/\/functionality of the method \n}\n}\npublic class Marks{\npublic void Marksdistribution(){\n\/\/functionality of the method \n}\n}<\/pre>\n\n\n\n<p id=\"why-is-this-principle-required\"><strong>Why is this Principle Required?<\/strong><\/p>\n\n\n\n<p>Testing is made easier when the Single Responsibility Principle is implemented. The class will have fewer test cases with a single responsibility. Because there is less functionality, there are fewer dependencies on other classes. Because smaller, well-purposed classes are easier to search, it leads to better code organization.<\/p>\n\n\n\n<h2 id=\"2-open-closed-principle\">2. Open-Closed Principle<\/h2>\n\n\n\n<p>According to this idea, a class should be created in such a way that it does its task flawlessly without the expectation that it will be changed in the future. As a result, the class should stay closed to alteration, but it should be possible to extend it. Extending the class can be done in a variety of ways, including<\/p>\n\n\n\n<ul><li>Inheriting from class.<\/li><li>Overwriting the required behaviour from the class.<\/li><li>Extending certain behaviour of the class.&nbsp;<\/li><\/ul>\n\n\n\n<p>Let&#8217;s understand it by using an example:<\/p>\n\n\n\n<p>Suppose, <strong>StudentInfo<\/strong> is a class and it has the method <strong>Streamname()<\/strong> that returns the name of the stream.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class StudentInfo  \n{  \npublic String Streamname(Student st)   \n{  \nif (st instanceof Science)   \n{  \nreturn st.getStream();  \nif (st instanceof Commerce) \n{  \nreturn st.getStream();   \n}  \n} <\/pre>\n\n\n\n<p>If we want to add another subclass named arts, simply, we add one more if statement that violates the open-closed principle. The only way to add the subclass and achieve the goal of principle is by overriding the <strong>Streamname()<\/strong> method, as we have shown below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class StudentInfo  \n{  \npublic String Streamname()\n{  \n\/\/functionality   \n}  \n}  \npublic class Science extends StudentInfo   \n{  \npublic String Streamname()   \n{  \nreturn this.getValue();  \n}  \npublic class Commerce extends StudentInfo    \n{  \npublic String Streamname()  \n{  \nreturn this.getValue();  \n}  \npublic class arts extends StudentInfo    \n{  \npublic String Streamname()  \n{  \nreturn this.getValue();  \n}  \n}<\/pre>\n\n\n\n<p id=\"why-is-this-principle-required\"><strong>Why is this principle required?<\/strong><\/p>\n\n\n\n<p>Because classes may come from third-party libraries, OCP is essential. We should be able to extend those classes without having to worry about whether or not the base classes will be able to support our expansions. However, inheritance may result in subclasses that are dependent on the implementation of the base class. The use of interfaces is recommended to avoid this. Loose coupling occurs as a result of the added abstraction.<\/p>\n\n\n\n<h2 id=\"3-liskov-substitution-principle\">3. Liskov substitution principle<\/h2>\n\n\n\n<p>Barbara Liskov proposed the Liskov Substitution Principle (LSP). It pertains to inheritance in the sense that derived classes must be 100% interchangeable with their base classes. To put it another way, if class A is a subtype of class B, we should be able to substitute B with A without affecting the program&#8217;s behaviour.<\/p>\n\n\n\n<p>It goes beyond the open-close principle to look at how a superclass and its subclasses behave. Unless there is a compelling reason to do otherwise, we should build the classes to preserve the property. Let&#8217;s understand it with an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Rectangle {\n    private double height;\n    private double width;\n    public void setHeight(double h) { height = h; }\n    public void setWidht(double w) { width = w; }\n    ...\n}\npublic class Square extends Rectangle {\n    public void setHeight(double h) {\n        super.setHeight(h);\n        super.setWidth(h);\n    }\n    public void setWidth(double w) {\n        super.setHeight(w);\n        super.setWidth(w);\n    }\n}\n<\/pre>\n\n\n\n<p>The above classes do not obey LSP because you cannot replace the Rectangle base class with its derived class Square. The Square class has extra constraints, i.e., the height and width must be the same. Therefore, substituting Rectangle with Square class may result in unexpected behaviour.<\/p>\n\n\n\n<p id=\"why-is-this-principle-is-required\"><strong>Why is this principle is required?<\/strong><\/p>\n\n\n\n<p>This prevents inheritance from being abused. It assists us in adhering to the &#8220;is-a&#8221; relationship. Subclasses must also adhere to a contract established by the base class. In this way, it&#8217;s similar to Bertrand Meyer&#8217;s Design by Contract. It&#8217;s easy to think of a circle as a form of an ellipse, yet circles lack two foci or major\/minor axes.<\/p>\n\n\n\n<h2 id=\"4-interface-segregation-principle\">4. Interface segregation principle<\/h2>\n\n\n\n<p>According to the interface segregation concept, a client should never be required to implement an interface that it does not use, or to rely on any method that it does not use. So, fundamentally, the interface segregation principles are that you favour small, client-specific interfaces over monolithic, larger interfaces. In other words, forcing the client to rely on something they don&#8217;t require is not a good idea. In short, No client should be forced to depend on methods that it does not use.<\/p>\n\n\n\n<p>Let&#8217;s understand it with the help of an example:<\/p>\n\n\n\n<p>Suppose there\u2019s an interface for a vehicle and a Bike class:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface Vehicle {\n    public void drive();\n    public void stop();\n    public void refuel();\n    public void openDoors();\n}\npublic class Bike implements Vehicle {\n\n    \/\/ Can be implemented\n    public void drive() {...}\n    public void stop() {...}\n    public void refuel() {...}\n    \n    \/\/ Can not be implemented\n    public void openDoors() {...}\n}<\/pre>\n\n\n\n<p>As you can see, a Bike class should not implement the openDoors() method because a bike does not have any doors! To address this, ISP offers to break down the interfaces into several, small coherent interfaces so that no class is required to implement any interfaces (and thus methods) that it does not require.<\/p>\n\n\n\n<p><strong>After implying the interface segregation principle<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface Driving {\n    public void drive();\n   }\npublic interface stops{\npublic void stop();\n}\npublic interface refueling{\npublic void refuel();\n}\npublic interface opening{\npublic void openDoors();\n\n}\npublic class Bike implements Driving,stops,refueling {\n    public void drive() {...}\n    public void stop() {...}\n public void refuel() {...}\n     }\n<\/pre>\n\n\n\n<p id=\"why-is-this-principle-required\"><strong>Why is this principle required?<\/strong><\/p>\n\n\n\n<p>The Interface Segregation Principle makes our code more readable and maintainable. We&#8217;ve pared down our class implementation to simplify the operations that are required, with no extra or extraneous code.<strong> <\/strong>As we only use that method that is required and can leave other methods.<\/p>\n\n\n\n<h2 id=\"5-dependency-inversion-principle\">5. Dependency Inversion Principle<\/h2>\n\n\n\n<p>The Dependency Inversion Principle (DIP) asserts that rather than real implementations, we should rely on abstractions (interfaces and abstract classes) (classes). Details should not be dependent on abstractions; rather, abstractions should be dependent on details.<\/p>\n\n\n\n<p>Let&#8217;s understand it by using an example:<\/p>\n\n\n\n<p>We have a class WindowMachine that contains keyboard and monitor classes inside it and we create instances of monitor and keyboard class inside WindowMachine constructor to use them.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class WindowsMachine  \n{  \npublic final keyboard;  \npublic final monitor;  \npublic WindowsMachine()  \n{  \nmonitor = new monitor();  \/\/instance of monitor class  \nkeyboard = new keyboard(); \/\/instance of keyboard class  \n}  \n}<\/pre>\n\n\n\n<p>But the problem with this code is that now all three classes are tightly coupled to each other which makes it hard to test the WindowMachine class let&#8217;s make this code loosely coupled with the help of the Dependency Inversion principle.<\/p>\n\n\n\n<p>To make the code loosely coupled, we decouple the WindowsMachine from the keyboard by using the Keyboard interface and this keyword.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public interface Keyboard   \n{   \n\/\/functionality  \n}  \npublic class WindowsMachine  \n{  \nprivate final Keyboard keyboard;  \nprivate final Monitor monitor;  \npublic WindowsMachine(Keyboard keyboard, Monitor monitor)   \n{  \nthis.keyboard = keyboard;  \nthis.monitor = monitor;  \n}  \n}  \n\n<\/pre>\n\n\n\n<p id=\"why-is-this-principle-is-required\"><strong>Why is this principle is required?<\/strong><\/p>\n\n\n\n<p>It allows a programmer to remove hardcoded dependencies so that the application becomes loosely coupled and extendable.<\/p>\n\n\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>By now, we have a good understanding of what Java, the Full form of SOLID, Solid principles in java is, the benefits of using SOLID principles, all five SOLID principles(Single responsibility principle, Open\/closed principle, Liskov substitution principle, Interface Segregation Principle, Dependency Inversion principle) are in detail. In simple terms, SOLID is a systematic design strategy that assures that your program is modular, understandable, debuggable, and refactorable. Following SOLID helps developers and maintainers save time and effort in both development and maintenance. SOLID helps you construct long-lasting software by preventing your code from becoming inflexible and fragile.<\/p>\n\n\n\n<h2 id=\"frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 id=\"q1-how-are-solid-principles-implemented-in-java\"><span id=\"q-1-how-are-solid-principles-implemented-in-java\">Q.1: How are solid principles implemented in Java?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> To implement SOLID principles in Java you need to understand basic OOP(object-oriented programming) concepts like inheritance, polymorphism, abstraction, and encapsulation and then you can use these 5 SOLID principles in your Java code.<\/p>\n\n\n\n<h3 id=\"q2-are-solid-principles-still-relevant\"><span id=\"q-2-are-solid-principles-still-relevant\">Q.2: Are SOLID principles still relevant?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> Yes, using SOLID principles is still relevant as using SOLID principles makes your code maintainable, testable, reusable, and scalable. It reduces the dependencies so that a block of code can be changed without affecting the other code blocks.<\/p>\n\n\n\n<h3 id=\"q3-how-do-you-write-solid-code\"><span id=\"q-3-how-do-you-write-solid-code\">Q.3: How do you write SOLID code?<\/span><\/h3>\n\n\n\n<p><strong>Ans:<\/strong> The key principle of writing SOLID code is using the single responsibility principle, open\/closed principle, Liskov substitution principle, Interface segregation principle, and dependency inversion principle with the help of these principles you can write solid code.<\/p>\n\n\n\n<h2 id=\"additional-resources\">Additional Resources<\/h2>\n\n\n\n<ul><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/courses\/fast-track-java\/\" target=\"_blank\">Learn Java<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/online-java-compiler\/\" target=\"_blank\">Online Java Compiler<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/practice\/\" target=\"_blank\">Practice Coding<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/java-mcq\/\" target=\"_blank\">Java MCQ<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-developer\/\" target=\"_blank\">How To Become A Java Developer<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-8-features\/\" target=\"_blank\">Java 8 Features<\/a><\/li><li><a href=\"https:\/\/www.interviewbit.com\/blog\/java-11-features\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java 11 Features<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/top-java-applications\/\" target=\"_blank\">Top Java Applications<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/best-java-ide\/\" target=\"_blank\">Best Java IDE<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-books\/\" target=\"_blank\">Java Books<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/characteristics-of-java\/\" target=\"_blank\">Characteristics of Java<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-9-features\/\" target=\"_blank\">Java 9 Features<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-frameworks\/\" target=\"_blank\">Java Frameworks<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-developer-salary\/\" target=\"_blank\">Java Developer Salary<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-developer-skills\/\" target=\"_blank\">Java Developer Skills<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/blog\/java-projects\/\" target=\"_blank\">Java Projects<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"In this article, we are going to learn about the Solid principles of Java. We will walk you&hellip;\n","protected":false},"author":4,"featured_media":8895,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[949],"tags":[27,1422],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8697"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=8697"}],"version-history":[{"count":28,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8697\/revisions"}],"predecessor-version":[{"id":18698,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/8697\/revisions\/18698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/8895"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=8697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=8697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=8697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}