1
votes

A "High-Level" component is a class with behavior defined in terms of other "low level" components. Example of this is Bulb class needs Socket class to implements its LightOn() behavior.

Not all superclass is the high-level component and not all subclass is the low-level component. Because of the following examples.

Template method pattern from head first design pattern.

public abstract class CaffeineBeverage {

    final void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }

    abstract void brew();

    abstract void addCondiments();

    void boilWater() {
        System.out.println("Boiling water");
    }

    void pourInCup() {
        System.out.println("Pouring int cup");
    }
}

public class Coffee extends CaffeineBeverage {

    public void brew() {
        System.out.println("Dripping Coffee through filter");
    }

    public void addCondiments() {
        System.out.println("Adding Sugar and Milk");
    }
}

In this example CaffeineBeverage class has a behavior prepareRecipe(). This behavior needs subclass implementation of brew() and addCondiments().

so... this means here CaffeineBeverage (superclass) is the high-level component and Coffee (subclass) is the low-level component.

public class superClass {
    public go() {
        //super class implementation
    }
}

public class subClass extends superClass {
    public go() {
        //sub class implementation
    }
}

In this case superClass didnt need subclass to implement its go() method.

Even if a class has abstract method that needs the subclass to implement it DOESNT mean super class is the high level component. See example below.

public abstract class superClass {
    public abstract go();
}

public class subClass extends superClass {
    public go() {
        //subclass implementation;
    }
}

main() {
    superClass s = new subClass();
    s.go();
}

Here s is not superClass object... s here is the subClass object.

1
It's possible you are conflating composition with inheritance. A light bulb is not a socket.Oliver Charlesworth
But still bulb still need socket to have its lightON behavior so bulb is high level component and socket is the low-level component. So you mean this "high-level component thing" is only applicable to inheritance? Sorry for my english BTW.user1348869

1 Answers

1
votes

Is it right to say that not all superclass is the high-level component of the subclass?

Yes it is right to say that ... assuming that you are using the word "component" the right way. In fact, as a general rule, inheritance does NOT model components.

For example:

  public abstract class Mammal extends Animal {
      ....
  }

  public class Cat extends Mammal {
      ....
  }

A Cat is not a "component" of a Mammal, and a Mammal is not a "component" of a Animal. This is not modelling a "composition" or "container" relationship. It is modelling a classification relationship; i.e. a Cat "is a" Mammal, and a Mammal "is a" Animal.


Your first example is a bit confusing / confused because the class names and methods have contradictory meanings (to my mind). But it looks like you are modeling beverage making devices. If that is correct then the relationship that you are really modelling is "is-a" not "component-of" / "part-of".


Finally, don't get stuck on the point of what classes and inheritance "means" in relation to the real world. In practice, Java classes are rarely used to directly model real world things. (You are unlikely to see classes that model cats and coffee-makers in real programs.) The true utility of inheritance is in its ability to provide structure for the code ... not in its ability to (crudely) model the real world.