Search code examples
javainheritancesuperclass

Purpose of implementing some subclasses and not others?


So, I am trying to understand a Java project in which there is an interface, then a class A with subclasses B, C, and D, each of which have their own respective number of subclasses.

I have noticed that some of the subclasses extend their given superclass and implement the interface, while others just extend their superclass. In addition, some of the subclasses of for instance class B will declare a method which is also declared in class B, as well as class A, and used in the interface.

My question is - what is the purpose of implementing the interface in the secondary subclasses, if extending them allows them to inherit the properties of their superclasses and thus implement the interface anyways?

I know that my logic must be flawed and there must be a reason, due to the fact that some of the secondary subclasses implement the interface while others don't.

Here's an example of one which implements Idump, while there are others which also contain the dumpProperties method which do not implement Idump. :

public class Q extends B implements Idump {
public void dumpProperties(StringBuffer buf)
{
    String appearance = "1";
    String dumpValue = "Sizable";

    buf.append("\t\t\t//\n");
    buf.append("\t\t\t// " + name + "\n");
    buf.append("\t\t\t//\n");
    buf.append("\t\t\tthis." + (owner != null ? name + "." : "") + "Name = \"" + name + "\";\n");
}


public class B extends A implements Idump {
public void dumpProperties(StringBuffer buf)
    {
        super.dumpProperties(buf);
        for(Control control : controls)
        {
            control.dumpProperties(buf);
        }
    }
}


public class A implements Idump{
public void dumpProperties(StringBuffer buf)
    {
        buf.append("\t\t\t//\n");
        buf.append("\t\t\t// " + name + "\n");
        buf.append("\t\t\t//\n");
        buf.append("\t\t\tthis." + (owner != null ? name + "." : "") + "Name = \"" + name + "\";\n");
        for(Property prop : properties)
        {
            prop.dump(buf);
        }

        if(left != -1 && top != -1 )
        {
            buf.append("\t\t\tthis." + name + ".Location = new System.Drawing.Point(" + left + "," + top + ");\n");
        }
        if(width != -1 && height != -1 )
        {
            buf.append("\t\t\tthis." + name + ".Size = new System.Drawing.Size(" + width + "," + height + ");\n");
        }

        for(Property prop : properties)
        {
            prop.dumpProperties(buf);
        }
    }
}

Any help is greatly appreciated!


Solution

  • If the sub-class doesn't explicitly contain an implementation then it will inherit the one from its' parent; note that means the other sub-classes (that do implement it explicitly) are overriding the implementation from the parent (this is known as Polymorphism).

    Similarly, every sub-class will inherit the interfaces that its' super-class implements (whether they are also marked as implementing the interface or not); that's a style and readability decision.