{"id":43674,"date":"2015-09-18T21:44:50","date_gmt":"2015-09-18T18:44:50","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=43674"},"modified":"2023-12-06T12:05:52","modified_gmt":"2023-12-06T10:05:52","slug":"how-to-design-classes-and-interfaces","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html","title":{"rendered":"How to design Classes and Interfaces"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">Advanced Java<\/a>.<\/p>\n<p>This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#Interfaces\">2. Interfaces<\/a><\/dt>\n<dt><a href=\"#Marker Interfaces\">3. Marker Interfaces<\/a><\/dt>\n<dt><a href=\"#static methods\">4. Functional interfaces, default and static methods<\/a><\/dt>\n<dt><a href=\"#Abstract classes\">5. Abstract classes<\/a><\/dt>\n<dt><a href=\"#Immutable classes\">6. Immutable classes<\/a><\/dt>\n<dt><a href=\"#Anonymous classes\">7. Anonymous classes<\/a><\/dt>\n<dt><a href=\"#Visibility\">8. Visibility<\/a><\/dt>\n<dt><a href=\"#Inheritance\">9. Inheritance<\/a><\/dt>\n<dt><a href=\"#Multiple inheritance\">10. Multiple inheritance<\/a><\/dt>\n<dt><a href=\"#Inheritance and composition\">11. Inheritance and composition<\/a><\/dt>\n<dt><a href=\"#encapsulation\">12. Encapsulation<\/a><\/dt>\n<dt><a href=\"#final classes\">13. Final classes and methods<\/a><\/dt>\n<dt><a href=\"#What_next\">14. What&#8217;s next<\/a><\/dt>\n<dt><a href=\"#download\">15. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>Whatever programming language you are using (and Java is not an exception here), following good design principles is a key factor to write clean, understandable, testable code and deliver long-living, easy to maintain solutions. In this part of the tutorial we are going to discuss the foundational building blocks which the Java language provides and introduce a couple of design principles, aiming to help you to make better design decisions.<\/p>\n<p>More precisely, we are going to discuss <strong>interfaces<\/strong> and<strong> interfaces with default methods<\/strong> (new feature of Java 8), <strong>abstract<\/strong> and <strong>final<\/strong> <strong>classes, immutable classes, inheritance, composition<\/strong> and revisit a bit the <strong>visibility<\/strong> (or accessibility) rules we have briefly touched in <strong>part 1<\/strong> of the tutorial, <strong><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-to-create-and-destroy-objects\/\">How to create and destroy objects<\/a><\/strong>.<\/p>\n<h2><a name=\"Interfaces\"><\/a>2. Interfaces<\/h2>\n<p>In object-oriented programming, the concept of interfaces forms the basics of contract-driven (or contract-based) development. In a nutshell, interfaces define the set of methods (contract) and every class which claims to support this particular interface must provide the implementation of those methods: a pretty simple, but powerful idea.<\/p>\n<p>Many programming languages do have interfaces in one form or another, but Java particularly provides language support for that. Let take a look on a simple interface definition in Java.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic interface SimpleInterface {\n    void performAction();\n} <\/pre>\n<p>In the code snippet above, the interface which we named <code>SimpleInterface<\/code> declares just one method with name <code>performAction<\/code>. The principal differences of interfaces in respect to classes is that interfaces outline what the contact is (declare methods), but do not provide their implementations.<\/p>\n<p>However, interfaces in Java can be more complicated than that: they can include nested interfaces, classes, enumerations, annotations (enumerations and annotations will be covered in details in <strong>part 5<\/strong> of the tutorial, <strong>How and when to use Enums and Annotations<\/strong>) and constants. For example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic interface InterfaceWithDefinitions {\n    String CONSTANT = \"CONSTANT\";\n\n    enum InnerEnum {\n        E1, E2;\n    }\n\n    class InnerClass {\n    }\n\n    interface InnerInterface {\n        void performInnerAction();\n    }\n\n    void performAction();\n} <\/pre>\n<p>With this more complicated example, there are a couple of constraints which interfaces implicitly impose with respect to the nested constructs and method declarations, and Java compiler enforces that. First and foremost, even if it is not being said explicitly, every declaration in the interface is <strong>public<\/strong> (and can be only <strong>public<\/strong>, for more details about visibility and accessibility rules, please refer to section <a href=\"#Visibility\">Visibility<\/a>). As such, the following method declarations are equivalent:<\/p>\n<pre class=\"brush:java\">\npublic void performAction();\nvoid performAction(); <\/pre>\n<p>Worth to mention that every single method in the interface is implicitly declared as <strong>abstract<\/strong> and even these method declarations are equivalent:<\/p>\n<pre class=\"brush:java\">\npublic abstract void performAction();\npublic void performAction();\nvoid performAction(); <\/pre>\n<p>As for the constant field declarations, additionally to being <code>public<\/code>, they are implicitly <code>static<\/code> and <code>final<\/code> so the following declarations are also equivalent:<\/p>\n<pre class=\"brush:java\">\nString CONSTANT = \"CONSTANT\";\npublic static final String CONSTANT = \"CONSTANT\"; <\/pre>\n<p>And finally, the nested classes, interfaces or enumerations, additionally to being <code>public<\/code>, are implicitly declared as <code> static<\/code>. For example, those class declarations are equivalent as well:<\/p>\n<pre class=\"brush:java\">\nclass InnerClass {\n}\n\nstatic class InnerClass {\n} <\/pre>\n<p>Which style you are going to choose is a personal preference, however knowledge of those simple qualities of interfaces could save you from unnecessary typing.<\/p>\n<h2><a name=\"Marker Interfaces\"><\/a>3. Marker Interfaces<\/h2>\n<p>Marker interfaces are a special kind of interfaces which have no methods or other nested constructs defined. We have already seen one example of the marker interface in <strong>part 2<\/strong> of the tutorial <strong><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects\/\">Using methods common to all objects<\/a><\/strong>, the interface <code>Cloneable<\/code>. Here is how it is defined in the Java library:<\/p>\n<pre class=\"brush:java\">\npublic interface Cloneable {\n} <\/pre>\n<p>Marker interfaces are not contracts per se but somewhat useful technique to \u201cattach\u201d or \u201ctie\u201d some particular trait to the class. For example, with respect to <code>Cloneable<\/code>, the class is marked as being available for cloning however the way it should or could be done is not a part of the interface. Another very well-known and widely used example of marker interface is <code>Serializable<\/code>:<\/p>\n<pre class=\"brush:java\">\npublic interface Serializable {\n} <\/pre>\n<p>This interface marks the class as being available for serialization and deserialization, and again, it does not specify the way it could or should be done.<\/p>\n<p>The marker interfaces have their place in object-oriented design, although they do not satisfy the main purpose of interface to be a contract.<\/p>\n<h2><a name=\"static methods\"><\/a>4. Functional interfaces, default and static methods<\/h2>\n<p>With the <a href=\"http:\/\/www.javacodegeeks.com\/2014\/05\/java-8-features-tutorial.html\">release of Java 8<\/a>, interfaces have obtained new very interesting capabilities: static methods, default methods and automatic conversion from lambdas (functional interfaces).<\/p>\n<p>In section <a href=\"#Interfaces\">Interfaces<\/a> we have emphasized on the fact that interfaces in Java can only declare methods but are not allowed to provide their implementations. With default methods it is not true anymore: an interface can mark a method with the <code>default<\/code> keyword and provide the implementation for it. For example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic interface InterfaceWithDefaultMethods {\n    void performAction();\n\n    default void performDefaulAction() {\n        \/\/ Implementation here\n    }\n} <\/pre>\n<p>Being an instance level, defaults methods could be overridden by each interface implementer, but from now, interfaces may also include <code>static<\/code> methods, for example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic interface InterfaceWithDefaultMethods {\n    static void createAction() {\n        \/\/ Implementation here\n    }\n} <\/pre>\n<p>One may say that providing an implementation in the interface defeats the whole purpose of contract-based development, but there are many reasons why these features were introduced into the Java language and no matter how useful or confusing they are, they are there for you to use.<\/p>\n<p>The functional interfaces are a different story and they are proven to be very helpful add-on to the language. Basically, the functional interface is the interface with just a single abstract method declared in it. The <code>Runnable<\/code> interface from Java standard library is a good example of this concept:<\/p>\n<pre class=\"brush:java\">\n@FunctionalInterface\npublic interface Runnable {\n    void run();\n} <\/pre>\n<p>The Java compiler treats functional interfaces differently and is able to convert the lambda function into the functional interface implementation where it makes sense. Let us take a look on following function definition:<\/p>\n<pre class=\"brush:java\">\npublic void runMe( final Runnable r ) {\n    r.run();\n} <\/pre>\n<p>To invoke this function in Java 7 and below, the implementation of the <code>Runnable<\/code> interface should be provided (for example using <a href=\"#Anonymous classes\">Anonymous classes<\/a>), but in Java 8 it is enough to pass <code>run()<\/code> method implementation using lambda syntax:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">\nrunMe( () -&gt; System.out.println( \"Run!\" ) ); <\/pre>\n<p>Additionally, the <code>@FunctionalInterface<\/code> annotation (annotations will be covered in details in <strong>part 5<\/strong> of the tutorial, <strong>How and when to use Enums and Annotations<\/strong>) hints the compiler to verify that the interface contains only one abstract method so any changes introduced to the interface in the future will not break this assumption.<\/p>\n<h2><a name=\"Abstract classes\"><\/a>5. Abstract classes<\/h2>\n<p>Another interesting concept supported by Java language is the notion of abstract classes. Abstract classes are somewhat similar to the interfaces in Java 7 and very close to interfaces with default methods in Java 8. By contrast to regular classes, abstract classes cannot be instantiated but could be subclassed (please refer to the section <a href=\"#Inheritance\">Inheritance<\/a> for more details). More importantly, abstract classes may contain abstract methods: the special kind of methods without implementations, much like interfaces do. For example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic abstract class SimpleAbstractClass {\n    public void performAction() {\n        \/\/ Implementation here\n    }\n\n    public abstract void performAnotherAction();\n} <\/pre>\n<p>In this example, the class <code>SimpleAbstractClass<\/code> is declared as <code>abstract<\/code> and has one <code>abstract<\/code> method declaration as well. Abstract classes are very useful when most or even some part of implementation details could be shared by many subclasses. However, they still leave the door open and allow customizing the intrinsic behavior of each subclass by means of abstract methods.<\/p>\n<p>One thing to mention, in contrast to interfaces which can contain only <code>public<\/code> declarations, abstract classes may use the full power of accessibility rules to control abstract methods visibility (please refer to the sections <a href=\"#Visibility\">Visibility<\/a> and <a href=\"#Inheritance\">Inheritance<\/a> for more details).<\/p>\n<h2><a name=\"Immutable classes\"><\/a>6. Immutable classes<\/h2>\n<p>Immutability is becoming more and more important in the software development nowadays. The rise of multi-core systems has raised a lot of concerns related to data sharing and concurrency (in the <strong>part 9<\/strong>, <strong>Concurrency best practices<\/strong>, we are going to discuss in details those topics). But the one thing definitely emerged: less (or even absence of) mutable state leads to better scalability and simpler reasoning about the systems.<\/p>\n<p>Unfortunately, the Java language does not provide strong support for class immutability. However using a combination of techniques it is possible to design classes which are immutable. First and foremost, all fields of the class should be <code>final<\/code>. It is a good start but does not guarantee immutability alone.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\nimport java.util.Collection;\n\npublic class ImmutableClass {\n    private final long id;\n    private final String[] arrayOfStrings;\n    private final Collection&lt; String &gt; collectionOfString;\n} <\/pre>\n<p>Secondly, follow the proper initialization: if the field is the reference to a collection or an array, do not assign those fields directly from constructor arguments, make the copies instead. It will guarantee that state of the collection or array will not be changed from outside.<\/p>\n<pre class=\"brush:java\">\npublic ImmutableClass( final long id, final String[] arrayOfStrings,\n        final Collection&lt; String &gt; collectionOfString) {\n    this.id = id;\n    this.arrayOfStrings = Arrays.copyOf( arrayOfStrings, arrayOfStrings.length );\n    this.collectionOfString = new ArrayList&lt;&gt;( collectionOfString );\n} <\/pre>\n<p>And lastly, provide the proper accessors (getters). For the collection, the immutable view should be exposed using <code>Collections.unmodifiableXxx<\/code> wrappers.<\/p>\n<pre class=\"brush:java\">\npublic Collection&lt;String&gt; getCollectionOfString() {\n    return Collections.unmodifiableCollection( collectionOfString );\n} <\/pre>\n<p>With arrays, the only way to ensure true immutability is to provide a copy instead of returning reference to the array. That might not be acceptable from a practical standpoint as it hugely depends on array size and may put a lot of pressure on garbage collector.<\/p>\n<pre class=\"brush:java\">\npublic String[] getArrayOfStrings() {\n    return Arrays.copyOf( arrayOfStrings, arrayOfStrings.length );\n} <\/pre>\n<p>Even this small example gives a good idea that immutability is not a first class citizen in Java yet. Things can get really complicated if an immutable class has fields referencing another class instances. Those classes should also be immutable however there is no simple way to enforce that.<\/p>\n<p>There are a couple of great Java source code analyzers like <strong><a href=\"http:\/\/findbugs.sourceforge.net\/\">FindBugs<\/a><\/strong>) and <strong><a href=\"http:\/\/pmd.sourceforge.net\/\">PMD<\/a><\/strong>) which may help a lot by inspecting your code and pointing to the common Java programming flaws. Those tools are great friends of any Java developer.<br \/>\n[ulp id=&#8217;w6F4W4SAMiyTapBF&#8217;]<br \/>\n&nbsp;<\/p>\n<h2><a name=\"Anonymous classes\"><\/a>7. Anonymous classes<\/h2>\n<p>In the pre-Java 8 era, anonymous classes were the only way to provide in-place class definitions and immediate instantiations. The purpose of the anonymous classes was to reduce boilerplate and provide a concise and easy way to represent classes as expressions. Let us take a look on the typical old-fashioned way to spawn new thread in Java:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class AnonymousClass {\n    public static void main( String[] args ) {\n        new Thread(\n            \/\/ Example of creating anonymous class which implements\n            \/\/ Runnable interface\n            new Runnable() {\n                @Override\n                public void run() {\n                    \/\/ Implementation here\n                }\n            }\n        ).start();\n    }\n} <\/pre>\n<p>In this example, the implementation of the <code>Runnable<\/code> interface is provided in place as anonymous class. Although there are some limitations associated with anonymous classes, the fundamental disadvantages of their usage are a quite verbose syntax constructs which Java imposes as a language. Even the simplest anonymous class which does nothing requires at least 5 lines of code to be written every time.<\/p>\n<pre class=\"brush:java\">\n   new Runnable() {\n      @Override\n      public void run() {\n      }\n   } <\/pre>\n<p>Luckily, with Java 8, lambdas and functional interfaces all this boilerplate is about to gone away, finally making the Java code to look truly concise.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class AnonymousClass {\n    public static void main( String[] args ) {\n        new Thread( () -&gt; { \/* Implementation here *\/ } ).start();\n    }\n} <\/pre>\n<h2><a name=\"Visibility\"><\/a>8. Visibility<\/h2>\n<p>We have already talked a bit about Java visibility and accessibility rules in <strong>part 1<\/strong> of the tutorial, <strong><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-to-create-and-destroy-objects\/\">How to design Classes and Interfaces<\/a><\/strong>. In this part we are going to get back to this subject again but in the context of subclassing.<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"134\"><strong>Modifier<\/strong><\/td>\n<td width=\"132\"><strong>Package<\/strong><\/td>\n<td width=\"142\"><strong>Subclass<\/strong><\/td>\n<td width=\"142\"><strong>Everyone Else<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"134\"><strong>public<\/strong><\/td>\n<td width=\"132\">accessible<\/td>\n<td width=\"142\">accessible<\/td>\n<td width=\"142\">Accessible<\/td>\n<\/tr>\n<tr>\n<td width=\"134\"><strong>protected<\/strong><\/td>\n<td width=\"132\">accessible<\/td>\n<td width=\"142\">accessible<\/td>\n<td width=\"142\"><strong>not accessible<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"134\"><strong>&lt;no modifier&gt;<\/strong><\/td>\n<td width=\"132\">accessible<\/td>\n<td width=\"142\"><strong>not accessible<\/strong><\/td>\n<td width=\"142\"><strong>not accessible<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"134\"><strong>private<\/strong><\/td>\n<td width=\"132\"><strong>not accessible<\/strong><\/td>\n<td width=\"142\"><strong>not accessible<\/strong><\/td>\n<td width=\"142\"><strong>not accessible<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">Table 1<\/p>\n<\/div>\n<p>Different visibility levels allow or disallow the classes to see other classes or interfaces (for example, if they are in different packages or nested in one another) or subclasses to see and access methods, constructors and fields of their parents.<\/p>\n<p>In next section, <a href=\"#Inheritance\">Inheritance<\/a>, we are going to see that in action.<\/p>\n<h2><a name=\"Inheritance\"><\/a>9. Inheritance<\/h2>\n<p>Inheritance is one of the key concepts of object-oriented programming, serving as a basis of building class relationships. Combined together with visibility and accessibility rules, inheritance allows designing extensible and maintainable class hierarchies.<\/p>\n<p>Conceptually, inheritance in Java is implemented using subclassing and the <code>extends<\/code> keyword, followed by the parent class. The subclass inherits all of the public and protected members of its parent class. Additionally, a subclass inherits the package-private members of the parent class if both reside in the same package. Having said that, it is very important no matter what you are trying to design, to keep the minimal set of the methods which class exposes publicly or to its subclasses. For example, let us take a look on a class <code>Parent<\/code> and its subclass <code>Child<\/code> to demonstrate different visibility levels and their effect:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class Parent {\n    \/\/ Everyone can see it\n    public static final String CONSTANT = \"Constant\";\n\n    \/\/ No one can access it\n    private String privateField;\n    \/\/ Only subclasses can access it\n    protected String protectedField;\n\n    \/\/ No one can see it\n    private class PrivateClass {\n    }\n\n    \/\/ Only visible to subclasses\n    protected interface ProtectedInterface {\n    }\n\n    \/\/ Everyone can call it\n    public void publicAction() {\n    }\n\n    \/\/ Only subclass can call it\n    protected void protectedAction() {\n    }\n\n    \/\/ No one can call it\n    private void privateAction() {\n    }\n\n    \/\/ Only subclasses in the same package can call it\n    void packageAction() {\n    }\n}\n<\/pre>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\n\/\/ Resides in the same package as parent class\npublic class Child extends Parent implements Parent.ProtectedInterface {\n    @Override\n    protected void protectedAction() {\n        \/\/ Calls parent's method implementation\n        super.protectedAction();\n    }\n\n    @Override\n    void packageAction() {\n        \/\/ Do nothing, no call to parent's method implementation\n    }\n\n    public void childAction() {\n        this.protectedField = \"value\";\n    }\n}\n<\/pre>\n<p>Inheritance is a very large topic by itself, with a lot of subtle details specific to Java. However, there are a couple of easy to follow rules which could help a lot to keep your class hierarchies concise. In Java, every subclass may override any inherited method of its parent unless it was declared as <code>final<\/code> (please refer to the section <a href=\"#final classes\">Final classes and methods<\/a>).<\/p>\n<p>However, there is no special syntax or keyword to mark the method as being overridden which may cause a lot of confusion. That is why the <code>@Override<\/code> annotation has been introduced: whenever your intention is to override the inherited method, please always use the <code>@Override<\/code> annotation to indicate that.<\/p>\n<p>Another dilemma Java developers are often facing in design is building class hierarchies (with concrete or abstract classes) versus interface implementations. It is strongly advised to prefer interfaces to classes or abstract classes whenever possible. Interfaces are much more lightweight, easier to test (using mocks) and maintain, plus they minimize the side effects of implementation changes. Many advanced programming techniques like creating class proxies in standard Java library heavily rely on interfaces.<\/p>\n<h2><a name=\"Multiple inheritance\"><\/a>10. Multiple inheritance<\/h2>\n<p>In contrast to C++ and some other languages, Java does not support multiple inheritance: in Java every class has exactly one direct parent (with <code>Object<\/code> class being on top of the hierarchy as we have already known from <strong>part 2<\/strong> of the tutorial, <strong><a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/using-methods-common-to-all-objects\/\">Using methods common to all objects<\/a><\/strong>). However, the class may implement multiple interfaces and as such, stacking interfaces is the only way to achieve (or mimic) multiple inheritance in Java.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class MultipleInterfaces implements Runnable, AutoCloseable {\n    @Override\n    public void run() {\n        \/\/ Some implementation here\n    }\n\n    @Override\n    public void close() throws Exception {\n       \/\/ Some implementation here\n    }\n} <\/pre>\n<p>Implementation of multiple interfaces is in fact quite powerful, but often the need to reuse an implementation leads to deep class hierarchies as a way to overcome the absence of multiple inheritance support in Java.<\/p>\n<pre class=\"brush:java\">\npublic class A implements Runnable {\n    @Override\n    public void run() {\n        \/\/ Some implementation here\n    }\n}\n<\/pre>\n<pre class=\"brush:java\">\n\/\/ Class B wants to inherit the implementation of run() method from class A.\npublic class B extends A implements AutoCloseable {\n    @Override\n    public void close() throws Exception {\n       \/\/ Some implementation here\n    }\n}\n<\/pre>\n<pre class=\"brush:java\">\n\/\/ Class C wants to inherit the implementation of run() method from class A\n\/\/ and the implementation of close() method from class B.\npublic class C extends B implements Readable {\n    @Override\n    public int read(java.nio.CharBuffer cb) throws IOException {\n       \/\/ Some implementation here\n    }\n}\n<\/pre>\n<p>And so on&#8230; The recent Java 8 release somewhat addressed the problem with the introduction of default methods. Because of default methods, interfaces actually have started to provide not only contract but also implementation. Consequently, the classes which implement those interfaces are automatically inheriting these implemented methods as well. For example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic interface DefaultMethods extends Runnable, AutoCloseable {\n    @Override\n    default void run() {\n        \/\/ Some implementation here\n    }\n\n    @Override\n    default void close() throws Exception {\n       \/\/ Some implementation here\n    }\n}\n\n\/\/ Class C inherits the implementation of run() and close() methods from the\n\/\/ DefaultMethods interface.\npublic class C implements DefaultMethods, Readable {\n    @Override\n    public int read(java.nio.CharBuffer cb) throws IOException {\n       \/\/ Some implementation here\n    }\n} <\/pre>\n<p>Be aware that multiple inheritance is a powerful, but at the same time a dangerous tool to use. The well known \u201cDiamond of Death\u201d problem is often cited as the fundamental flaw of multiple inheritance implementations, so developers are urged to design class hierarchies very carefully. Unfortunately, the Java 8 interfaces with default methods are becoming the victims of those flaws as well.<\/p>\n<pre class=\"brush:java\">\ninterface A {\n    default void performAction() {\n    }\n}\n\ninterface B extends A {\n    @Override\n    default void performAction() {\n    }\n}\n\ninterface C extends A {\n    @Override\n    default void performAction() {\n    }\n} <\/pre>\n<p>For example, the following code snippet fails to compile:<\/p>\n<pre class=\"brush:java\">\n\/\/ E is not compilable unless it overrides performAction() as well\ninterface E extends B, C {\n} <\/pre>\n<p>At this point it is fair to say that Java as a language always tried to escape the corner cases of object-oriented programming, but as the language evolves, some of those cases are started to pop up.<\/p>\n<h2><a name=\"Inheritance and composition\"><\/a>11. Inheritance and composition<\/h2>\n<p>Fortunately, inheritance is not the only way to design your classes. Another alternative, which many developers consider being better than inheritance, is composition. The idea is very simple: instead of building class hierarchies, the classes should be composed from other classes.<\/p>\n<p>Let us take a look on this example:<\/p>\n<pre class=\"brush:java\">\npublic class Vehicle {\n    private Engine engine;\n    private Wheels[] wheels;\n    \/\/ ...\n} <\/pre>\n<p>The <code>Vehicle<\/code> class is composed out of <code>engine<\/code> and <code>wheels<\/code> (plus many other parts which are left aside for simplicity). However, one may say that <code>Vehicle<\/code> class is also an engine and so could be designed using the inheritance.<\/p>\n<pre class=\"brush:java\">\npublic class Vehicle extends Engine {\n    private Wheels[] wheels;\n    \/\/ ...\n} <\/pre>\n<p>Which design decision is right? The general guidelines are known as <strong>IS-A<\/strong> and <strong>HAS-A<\/strong> principles. <strong>IS-A<\/strong> is the inheritance relationship: the subclass also satisfies the parent class specification and a such <strong>IS-A<\/strong> variation of parent class. Consequently,<strong> HAS-A <\/strong>is the composition relationship: the class owns (or <strong>HAS-A<\/strong>) the objects which belong to it. In most cases, the <strong>HAS-A <\/strong>principle works better then <strong>IS-A <\/strong>for couple of reasons:<\/p>\n<ul>\n<li>The design is more flexible in a way it could be changed<\/li>\n<li>The model is more stable as changes are not propagating through class hierarchies<\/li>\n<li>The class and its composites are loosely coupled compared to inheritance which tightly couples parent and its subclasses<\/li>\n<li>The reasoning about class is simpler as all its dependencies are included in it, in one place<\/li>\n<\/ul>\n<p>However, the inheritance has its own place, solves real design issues in different way and should not be neglected. Please keep those two alternatives in mind while designing your object-oriented models.<\/p>\n<h2><a name=\"encapsulation\"><\/a>12. Encapsulation<\/h2>\n<p>The concept of encapsulation in object-oriented programming is all about hiding the implementation details (like state, internal methods, etc.) from the outside world. The benefits of encapsulation are maintainability and ease of change. The less intrinsic details classes expose, the more control the developers have over changing their internal implementation, without the fear to break the existing code (a real problem if you are developing a library or framework used by many people).<\/p>\n<p>Encapsulation in Java is achieved using visibility and accessibility rules. It is considered a best practice in Java to never expose the fields directly, only by means of getters and setters (if the field is not declared as <code>final<\/code>). For example:<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class Encapsulation {\n    private final String email;\n    private String address;\n\n    public Encapsulation( final String email ) {\n        this.email = email;\n    }\n\n    public String getAddress() {\n        return address;\n    }\n\n    public void setAddress(String address) {\n        this.address = address;\n    }\n\n    public String getEmail() {\n        return email;\n    }\n} <\/pre>\n<p>This example resembles what is being called <code><a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/javabeans\/\">JavaBeans<\/a><\/code> in Java language: the regular Java classes written by following the set of conventions, one of those being allow the access to fields using getter and setter methods only.<\/p>\n<p>As we already emphasized in the <a href=\"#Inheritance\">Inheritance<\/a> section, please always try to keep the class public contract  minimal, following the encapsulation principle. Whatever should not be <code>public<\/code>, should be <code>private<\/code> instead (or <code>protected<\/code> \/ <code>package private<\/code>, depending on the problem you are solving). In long run it will pay off, giving you the freedom to evolve your design without introducing breaking changes (or at least minimize them).<\/p>\n<h2><a name=\"final classes\"><\/a>13. Final classes and methods<\/h2>\n<p>In Java, there is a way to prevent the class to be subclassed by any other class: it should be declared as <code>final<\/code>.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic final class FinalClass {\n} <\/pre>\n<p>The same <code>final<\/code> keyword in the method declaration prevents the method in question to be overridden in subclasses.<\/p>\n<pre class=\"brush:java\">\npackage com.javacodegeeks.advanced.design;\n\npublic class FinalMethod {\n    public final void performAction() {\n    }\n} <\/pre>\n<p>There are no general rules to decide if class or method should be <code>final<\/code> or not. Final classes and methods limit the extensibility and it is very hard to think ahead if the class should or should not be subclassed, or method should or should not be overridden. This is particularly important to library developers as the design decisions like that could significantly limit the applicability of the library.<\/p>\n<p>Java standard library has some examples of <code>final<\/code> classes, with most known being <code>String<\/code> class. On an early stage, the decision has been taken to proactively prevent any developer\u2019s attempts to come up with own, \u201cbetter\u201d string implementations.<\/p>\n<h2><a name=\"What_next\"><\/a>14. What\u2019s next<\/h2>\n<p>In this part of the tutorial we have looked at object-oriented design concepts in Java. We also briefly walked through contract-based development, touched some functional concepts and saw how the language evolved over time. In next part of the tutorial we are going to meet generics and how they are changing the way we approach type-safe programming.<\/p>\n<h2><a name=\"download\"><\/a>15. Download the Source Code<\/h2>\n<p>This was a lesson on How to design Classes and Interfaces. <\/p>\n<ul>\n<li>You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/advanced-java-part-3.zip\">advanced-java-part-3<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out here! Table Of Contents &hellip;<\/p>\n","protected":false},"author":141,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[343,88,580,557,65],"class_list":["post-43674","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-annotations","tag-concurrency","tag-generics","tag-reflection","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to design Classes and Interfaces - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to design Classes and Interfaces - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-18T18:44:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-06T10:05:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrey Redko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrey Redko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html\"},\"author\":{\"name\":\"Andrey Redko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\"},\"headline\":\"How to design Classes and Interfaces\",\"datePublished\":\"2015-09-18T18:44:50+00:00\",\"dateModified\":\"2023-12-06T10:05:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html\"},\"wordCount\":3057,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Annotations\",\"Concurrency\",\"Generics\",\"Reflection\",\"Serialization\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html\",\"name\":\"How to design Classes and Interfaces - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-18T18:44:50+00:00\",\"dateModified\":\"2023-12-06T10:05:52+00:00\",\"description\":\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-design-classes-and-interfaces.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to design Classes and Interfaces\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\",\"name\":\"Andrey Redko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"caption\":\"Andrey Redko\"},\"description\":\"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\\\/EE, C#\\\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).\",\"sameAs\":[\"http:\\\/\\\/aredko.blogspot.com\\\/\",\"http:\\\/\\\/ca.linkedin.com\\\/in\\\/aredko\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-redko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to design Classes and Interfaces - Java Code Geeks","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html","og_locale":"en_US","og_type":"article","og_title":"How to design Classes and Interfaces - Java Code Geeks","og_description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-18T18:44:50+00:00","article_modified_time":"2023-12-06T10:05:52+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Redko","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Redko","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html"},"author":{"name":"Andrey Redko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413"},"headline":"How to design Classes and Interfaces","datePublished":"2015-09-18T18:44:50+00:00","dateModified":"2023-12-06T10:05:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html"},"wordCount":3057,"commentCount":3,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Annotations","Concurrency","Generics","Reflection","Serialization"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html","name":"How to design Classes and Interfaces - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-18T18:44:50+00:00","dateModified":"2023-12-06T10:05:52+00:00","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-design-classes-and-interfaces.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"How to design Classes and Interfaces"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413","name":"Andrey Redko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","caption":"Andrey Redko"},"description":"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\/EE, C#\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).","sameAs":["http:\/\/aredko.blogspot.com\/","http:\/\/ca.linkedin.com\/in\/aredko"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-redko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=43674"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43674\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=43674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=43674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=43674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}