We spoke about the benefits of using both inheritance and delegation for reuse a
ID: 3755257 • Letter: W
Question
We spoke about the benefits of using both inheritance and delegation for reuse and for good design. Keeping both of these in mind, answer the following (A). What is delegation and how specifically does it improve design? (B). Given the following model: BEAR Color Weight Swim0 Grow10 Display0 PolarBEAR Color Weight Swim0 Grow10 Display0 //Looks like a Polar Bear/ BrownBEAR Color Weight Swim0 Grow10 Display0 //Looks like a Brown Bear/ Assume the requirements change to include the need to model TeddyBEAR as a type of BEAR. The problem is that TeddyBEARs Squeak instead of Growl. Show me TWO ways of resolving this problem via class drawings like we've done in class (informal syntax like the modeling homework). Then pick the ONE way you think is best and explain WHY it is best. HINT: Delegation uses HAS-A and you will want to include that in one of your solutionsExplanation / Answer
Delegation is simply passing a duty off to someone/something else.Delegation is alternative to inheritance. Delegation means that you use an object of another class as an instance variable, and forward messages to the instance. It is better than inheritance because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense.Delegation is dynamic binding (run-time binding) while Inheritance is static binding (compile time binding)
package relationship;
class Bear{
private string color;
private int weight;
public void display(){
system.out.println("Color="+color+" weight="+weight);
}
public void setcolor(string color){
this.color=color;
}
public void setweight(int weight){
this.weight=weight;
}
public void swim(){
system.out.println("It swims");
}
public void grawl(){
system.out.println("It grawls");
}
}
IN ISA(inheritance)
class PolarBear extends Bear{
}
class BrownBear extends Bear{
}
class TeddyBear extends Bear{
public void sneek(){
system.out.println("It sneeks");
}
}
package relationship;
public class RelationDemo{
public static void main(String[ ],args){
PolarBear pbear = new PolarBear();
pbear.setcolor("white");
pbear.setweight(266);
pbear.display();
pbear.swim();
pbear.grawl();
BrownBear bbear = new BrownBear();
bbear.setcolor("brown");
bbear.setweight(226);
bbear.display();
bbear.swim();
bbear.grawl();
TeddyBear tbear = new TeddyBear();
tbear.setcolor("grey");
tbear.setweight(206);
tbear.display();
tbear.swim();
tbear.sneek();
}
}
IN HAS(Delegation)
class PolarBear{
public void start(){
Bear ppbear=new Bear();
ppbear.setcolor("white");
ppbear.setweight(266);
ppbear.display();
ppbear.swim();
ppbear.grawl();
}
}
class BrownBear{
public void start(){
Bear bbbear=new Bear();
bbbear.setcolor("brown");
bbbear.setweight(206);
bbbear.display();
bbbear.swim();
bbbear.grawl();
}
}
class TeddyBear{
public void sneek(){
system.out.println("It sneeks");
}
public void start(){
Bear ttbear=new Bear();
ttbear.setcolor("grey");
ttbear.setweight(262);
ttbear.display();
ttbear.swim();
system.out.println("It Sneeks");
}
}
package relationship;
public class RelationDemo{
public static void main(String[ ],args){
PolarBear pbear=new PolarBear();
pbear.start();
BrownBear bbear=new BrownBear();
bbear.start();
TeddyBear tbear=new TeddyBear();
tbear.start();
}
}
I prefer ISA ie inheritance as it is easy to implement and easy to understand than delegation.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.