Tuesday, April 24, 2012

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

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.





No comments:

Post a Comment