Re: Very Confused on Page 33
From: Erwin Moller (since_humans_read_this_I_am_spammed_too_much_at_spamyourself.com)
Date: 01/29/05
- Next message: Matt Humphrey: "Re: Very Confused on Page 33"
- Previous message: eBob.com: "Very Confused on Page 33"
- In reply to: eBob.com: "Very Confused on Page 33"
- Next in thread: Erwin Moller: "Re: Very Confused on Page 33"
- Reply: Erwin Moller: "Re: Very Confused on Page 33"
- Reply: Andrew Thompson: "Re: Very Confused on Page 33"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 29 Jan 2005 17:30:31 +0100
eBob.com wrote:
Hi Bob,
> I am only on page 33 of Bruce Eckel's "Thinking in Java" and I am so
> confused by one point that I can go no further without getting this
> straightened out. On page 33 Bruce is explaining inheritance. The text
> which confuses me is " ... all the messages you can send to objects of the
> base class you can also send to objects of the derived class. Since we
> know
> the type of a class by the messages we can send to it, this means that
> THE
> DERIVED CLASS IS THE SAME TYPE AS THE BASE CLASS. ..." (emphasis added)
>
Yes, but don't let this confuse you.
Supose your baseclass is Animal.
You subclass from Animal a class called Cow.
A cow is an Animal, right?
A Cow is of TYPE Animal.
> I am not totally unfamiliar with OOP and I understand that all messages
> which can be sent to objects of a base class can also be sent to objects
> of
> the derived class. BUT, I would think that, in general, the inverse is
> not
> true.
Correct.
You can add methods to the derived class that are NOT in the base class.
for example:
You can subclass from Animal a Cow and add a method to milk().
The method milk() would not make a lot of sense to place in the baseclass
Animal, since many animals don't like to be milked.
> Without getting into what the meaning of is is, am I supposed to
> understand that the derived class is the same type as the base class, but
> the base class is not the same type as the derived class?
Indeed.
A cow is an animal.
An animal is not (always) a cow.
>
> But even if that would resolve the confusion which I have explained so
> far, what about "Overriding base-class functionality" - the topic which is
> comming up at the bottom of the page.
> Even assuming that "the derived
> class is the same type as the base class" is not meant to be a symmetric
> statement,
which is correct.
> is it true even though the derived class might have overridden
> functions?
yes.
for example:
If you have a method in your baseclass Animal getAge().
Supose it returns the age of the animal.
Now if you subclass a cat from Animal, you might want to use catyears.
You return 7 times the age.
(Silly example, I know.)
I practice:
If you have a nice method in your baseclass, but for some reason DO NOT want
to use it in your subclass, you override it.
You just define the method again in your subclass, and the
compliler/runtimeenvironment will use the correct method.
So:
Animal myAnimal = new Animal();
myAnimal.getAge() will return whatever it is that the baseclassmethod
returns.
but:
Cat myCat = new Cat();
myCat.getAge() will return whatever it is that is defined in the method
getAGe in Cat-class.
> I would think that the compiler would need to be able to
> distinguish between objects of the base class and objects of the derived
> class. How would it do that if they both are of the same type?
The fact that they are of the same type does not mean they are the same.
If you baseclass is 'animal', you can subclass it hundred times for all kind
of specific animals.
And don't worry about how the compliler manages its tricks. It saves you a
headache.
>
> If anyone can help get me past this confusion I'd appreciate it.
>
I hope this helped.
> Thanks, Bob
Regards,
Erwin Moller
- Next message: Matt Humphrey: "Re: Very Confused on Page 33"
- Previous message: eBob.com: "Very Confused on Page 33"
- In reply to: eBob.com: "Very Confused on Page 33"
- Next in thread: Erwin Moller: "Re: Very Confused on Page 33"
- Reply: Erwin Moller: "Re: Very Confused on Page 33"
- Reply: Andrew Thompson: "Re: Very Confused on Page 33"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|