public class Horse extends Animal {
private Halter myHalter = new Halter();
public void tie(LeadRope rope) {
myHalter.tie(rope); // Delegate tie behavior to the
// Halter object
}
}
public class Halter {
public void tie(LeadRope aRope) {
// Do the actual tie work here
}
}
In this example Horse has-a Halter.Can we call myHalter.tie(rope); like this:
public class Horse extends Animal {
private Halter myHalter = new Halter();
myHalter.tie(rope); // Without using the public void tie method
}
It gives an error. My explanation to this is that its not a main() method but could anyone explain it in a better way.
Statements other than variable/field declarations in a class body must be put into a method body, a constructor or an initializer block. For example, it would be OK if you attempt to compile this code: