Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2004

Tip: Looking for answers? Try searching our database.

Java Inheritance, Objects as "Instance Variables"

Thread view: 
Sonja - 24 Feb 2004 19:06 GMT
I just had a discussion with one of my teachers that has me confused
about Java inheritance and Java terminology.  

Here is the scenario:

There is an Animal class at the top of the hierarchy.

The animal class is subclassed with subclasses for Herbivore,
Carnivore, and Omnivore.

One level down there are specific animal classes (Bear, Lion, Tiger)
that subclas Herbivore, Carnivore, and Omnivore.

So, the hierarchy looks like:

Animal
----Herbivore
--------Bear
----Carnivore
--------Lion
--------Tiger
----Omnivore

I was told that in Java, you couldn't have the verticle class
hierarchy of Tiger extending Carnivore extending Animal, because this
is "multiple inheritance".  My understanding is that this would just
be single inheritance because each class was really extending only one
class above it.  Question:  is this single inheritance or multiple
inheritance, and can you do this in Java?

In the above example, what I think wouldn't be allowed would be Bear
extending both Carnivore and Herbivore (if there wasn't an Omnivore
class).

My understanding is that in Java, pretty much everything extends down
from the Object class.  I was told that this isn't true, that packages
are what matter, and that java.io, java.net, java.lang for example are
their own set of unrelated inheritance structures.  True or false?

Also, I was told that an object reference isn't considered to be an
instance variable.  So, if a Zoo has one or more Animals, the
declaration (in Zoo) of  Animal animal = new Animal() wouldn't mean
that animal is an instance variable, it is "just the instantiation of
an Animal object.  I think it would be an instance variable.

Thank you for any clarification.  The discussion came up when I was
trying to understand how UML notation would translate into Java code
implementation.

Sonja
Christophe Vanfleteren - 24 Feb 2004 19:25 GMT
> I just had a discussion with one of my teachers that has me confused
> about Java inheritance and Java terminology.
[quoted text clipped - 25 lines]
> class above it.  Question:  is this single inheritance or multiple
> inheritance, and can you do this in Java?

Yes, you can do this in Java, and yes, this is still single inheritance.
As you say, multiple inheritance would be extending both omni- and
herbivore.

> In the above example, what I think wouldn't be allowed would be Bear
> extending both Carnivore and Herbivore (if there wasn't an Omnivore
> class).

That is correct.

> My understanding is that in Java, pretty much everything extends down
> from the Object class.  I was told that this isn't true, that packages
> are what matter, and that java.io, java.net, java.lang for example are
> their own set of unrelated inheritance structures.  True or false?

False. Whoever told you that doesn't have a clue. Stop listening to them :)
Everything does extend java.lang.Object, even if you don't have to
explicitly state this in your class declaration.

> Also, I was told that an object reference isn't considered to be an
> instance variable.  So, if a Zoo has one or more Animals, the
[quoted text clipped - 6 lines]
>
> Sonja

Signature

Kind regards,
Christophe Vanfleteren

Sonja - 24 Feb 2004 19:29 GMT
>False. Whoever told you that doesn't have a clue. Stop listening to them :)
>Everything does extend java.lang.Object, even if you don't have to
>explicitly state this in your class declaration.

Whew... thank you for your answer.  I am on my second semester of Java
and have done additional reading on the side, and if my understanding
were that off I would have considered not continuining my computer
education and looking for a different career path!

Sonja
Mark Haase - 24 Feb 2004 21:55 GMT
> Whew... thank you for your answer.  I am on my second semester of Java
> and have done additional reading on the side, and if my understanding
> were that off I would have considered not continuining my computer
> education and looking for a different career path!

I don't know who was trying to convince you otherwise, but all the
beliefs you expressed in your first post were dead on.

|\/|  /|  |2  |<
mehaase(at)sas(dot)upenn(dot)edu
Andrew Thompson - 24 Feb 2004 19:34 GMT
"Sonja"...
> I just had a discussion with one of my teachers that has me confused
> about Java inheritance and Java terminology.

> Animal
> ----Herbivore
[quoted text clipped - 7 lines]
> hierarchy of Tiger extending Carnivore extending Animal, because this
> is "multiple inheritance".

No.  Tiger inherits attributes and methods
off it's ancestor class Carnivore, which
itself inherits both attributes and methods
from _it's_ super-class, Animal.

*Multiple* *Inheritence* would be more
like trying to get an Omnivore (let's say
a Pig), by having it inherit from both
Herbivore and Carnivore.

Of course, a better structure might
be to have both Herbivore and Carnivore
as interfaces, simply declaring attributes
and specifying the methods of the respective
parasites (the animals), then implement both
interfaces for Pig.

>My understanding is that this would just
> be single inheritance because each class was really extending only one
> class above it.

Yes, but each class inherits..
Have a close look at the API
docs for something like JButton.

It has a slew of ancestors, listed in the
indented list at the top of the page,
and inherits attibutes and methods from
each.

> ..Question:  is this single inheritance or multiple
> inheritance, and can you do this in Java?

Single. Yes.

> In the above example, what I think wouldn't be allowed would be Bear
> extending both Carnivore and Herbivore (if there wasn't an Omnivore
[quoted text clipped - 4 lines]
> are what matter, and that java.io, java.net, java.lang for example are
> their own set of unrelated inheritance structures.  True or false?

False

I am suspecting more by the moment that
you have not yet discovered these..
http://java.sun.com/j2se/1.4.2/docs/api/

Click the 'package frame' on the upper
left for 'java.io' and then click any class
within it, the root class in the hierarchy
tree is Object, it is easy to confirm.

HTH

--
--

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Shane Mingins - 24 Feb 2004 20:18 GMT
> The discussion came up when I was
> trying to understand how UML notation would translate into Java code
> implementation.

Always an interesting discussion.  If you  wish, flick me an email with an
address I can send a 1mb PDF file to.  I have a draft copy of a book that
you will find a good read in regards to Java/UML and some OO principles.

Shane

Signature

shanemingins@yahoo.com.clothes
remove clothes before replying

Adam - 25 Feb 2004 07:44 GMT
> I just had a discussion with one of my teachers that has me confused
> about Java inheritance and Java terminology.

Well, the group already answered your questions,
so all I can say is...
change the so called teacher!

Adam
Sonja - 27 Feb 2004 22:08 GMT
>news:ev7n30lm5l7bn3k6r4r7hjthffhrdtu1qi@4ax.com...
>> I just had a discussion with one of my teachers that has me confused
[quoted text clipped - 5 lines]
>
>Adam

There is a happy and a sad ending to this story.

The happy ending is that the help on this group is great!  This group
is helping me get over some of the bumps in the road to learning Java.

The sad ending is that I e-mailed my instructor, very nicely, with the
correct information, and she did not even acknowledge or respond to
the e-mail.

So, before class, I asked her if she had received the e-mail, and all
she said was "yes".

Then, during class she said that "Java considers a child pointing to
two parents as multiple inheritance.  In UML terminology, though,
multiple inheritance can be one class inheriting from one class
inheriting from another" (i.e. single-level verticle inheritance).

That blew me away, because:

1)  She didn't even thank me for or acknowledge the feedback and
having the courage to _help_ the teacher learn some things about Java
(noone expects anyone, even teachers to be perfect).

and

2)  I sincerely doubt that multi-level single inheritance is referred
to as "multiple inheritance" in very many (or in any) book on UML (I
could be wrong.....).

So, and this may not be very wise, but...... it irked me enough that I
decided to say something.  I wonder if it will affect my grade, and if
I will now be on her "black list", but I feel better after sending he
a short followup e-mail:

<start e-mail clip>

"Hi <instructor name omitted>.  I just wanted to touch base about the
e-mail clarifying some of the things we had talked about.  It was
really hard for me to follow up and provide feedback about things like
everything in Java inheriting from the Object class, the fact that in
Java you can inherit vertically (which was different than what you had
said when we talked after class), and the multiple inheritance
terminology difference in UML....

Personally, I like it when people give me feedback that helps clarify
things and helps me to understand them better, and then I thank them
for that feedback.  I was a little surprised that you didn't respond
to the e-mail.  I took a risk to clarify some things based on my
knowledge of Java at this point of my education.  I guess I would have
expected some kind of email or thank you or at least some kind of
acknowledgement.

</ end e-mail clip>

Thanks again to this group -- it's good to have a place where I can
come and have things clarified.

Sonja
Andrew Thompson - 28 Feb 2004 00:04 GMT
>>news:ev7n30lm5l7bn3k6r4r7hjthffhrdtu1qi@4ax.com...
>>> I just had a discussion with one of my teachers that has me confused
[quoted text clipped - 3 lines]
>>so all I can say is...
>>change the so called teacher!
...
> The sad ending is that I e-mailed my instructor, very nicely, with the
> correct information, and she did not even acknowledge or respond to
> the e-mail.
<snip remainder>

:-(

It might be both safer, and more productive,
to direct your enthusiasm and inquisitive
nature to helping your fellow students who also
have to suffer this ..teacher, but do not have
the common sense that you display in consulting
further opinions.

[ But I admire you challenging the teacher,
that's just what I do on such occasions! ;-) ]

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Sonja - 29 Feb 2004 05:20 GMT
>It might be both safer, and more productive,
>to direct your enthusiasm and inquisitive
[quoted text clipped - 5 lines]
>[ But I admire you challenging the teacher,
>that's just what I do on such occasions! ;-) ]

You're right.  I dread seeing the teacher this week in class.  From
now on with this teacher she can be right (even if she is wrong).

Sonja


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.