This type of referencing is very well applied when you want to apply design patterns, (you may need to pass an advanced course of object oriented design, or start by reading book: head first, design patters, i suggest)
see for example how to implement a decorator pattern in java in the mentioned book.
public abstract class Beverage {
String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
then think we have espresso and DarkRoast as two child classes:
public class Espresso extends Beverage {
public Espresso() {
this.description = "Espresso";
}
public double cost() {
return 1.99;
}
}
public class DarkRoast extends Beverage {
public DarkRoast() {
description = "Dark Roast Coffee";
}
public double cost() {
return .99;
}
}
now, we are going to add decorator:
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
and build some concrete decorator:
public class Mocha extends CondimentDecorator {
Beverage beverage;
public Mocha(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Mocha";
}
public double cost() {
return .20 + beverage.cost();
}
}
and another wrapper:
public class Whip extends CondimentDecorator {
Beverage beverage;
public Whip(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Whip";
}
public double cost() {
return .10 + beverage.cost();
}
}
finally, see what happened in the main function and how we take advantages of the pointer to the father class:
public static void main(String[] args) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " $" + beverage.cost());
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
System.out.println(beverage2.getDescription() + " $" + beverage2.cost());
can you guess what is the output? well:
Espresso $1.99
Dark Roast Coffee, Mocha, Mocha, Whip $1.49