Conceptual Question about inheritance relations? [closed]
Here is my Homework question I want to solve:
R-2.9 Consider the inheritance of classes from Exercise R-2.5, and let d be an object variable of type Horse. If d refers to an actual object of type Equestrian, can it be cast to the class Racer? Why or why not?
here is exercise R-2.5:
R-2.5 Draw a class inheritance diagram for the following set of classes:
• Class Goat extends Object and adds an instance variable tail and methods milk() and jump().
• Class Pig extends Object and adds an instance variable nose and methods eat() and wallow().
• Class Horse extends Object and adds instance variables height and color, and methods run() and jump().
• Class Racer extends Horse and adds a method race().
• Class Equestrian extends Horse and adds an instance variable weight and methods trot() and isTrained().
As for the "Can an Equestrian be cast to a Racer?" part of the question: Try to actually implement the classes as described, and write code that performs such a cast. Does it compile? If so, does it throw an exception when it is run?
As for the "Why?" part: If class B extends class A, this means that any B is also an A. This is the only thing Java knows about what these classes represent or how we intend to use them - Java only knows that every Racer is a Horse and that every Equestrian is also a Horse (and Java knows this only because we said so when we declared the classes). Based on only this information, is it reasonable that Java should allow you to take an Equestrian (which can do stuff all Horses can do, as well as some Equestrian specific things, such as to trot) and start treating it like it was a Racer (which can also do stuff all Horses can do, but can't necessarily trot, and can do some things that Equestrians can't do)?
Also, note that there is a difference between what the compiler will allow and what casts will actually go through when they are attempted. In general, the compiler will allow casts that might succeed. Try each of these code segments; each of them should give a different outcome (one won't compile, one will compile but will throw an exception when it is run, one will compile and run without exceptions). Then, reflect upon why Java is designed to behave in this way.
// Code segment A
Horse h = new Equestrian();
Goat g = (Goat)h;
// Code segment B
Horse h = new Equestrian();
Racer r = (Racer)h;
// Code segment C
Horse h = new Racer();
Racer r = (Racer)h;
Think of this as a tree:
Horse --> Equestrian
Horse --> Racer
Equestrian and Racer are two branches of the trunk Horse.
Inheritance works only from top to bottom (from trunk to branches). Methods and variables of the parent class can be inherited by the sub classes. Therefore, Equestrian inherits the methods and variables of the Horse class. Racer also inherits the methods and variables of the Horse class. BUT, objects of class Racer or Equestrian don't inherit each other's methods or variables.
The above should be enough information for you to figure out the answer. Once you're done with the homework, here's where you can find more information. It is very important to read up on basic concepts such as this as much as you can, early on.
精彩评论