aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does a Dog object inherit Fur? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does a Dog object inherit Fur?" Watch "Does a Dog object inherit Fur?" New topic
Author

Does a Dog object inherit Fur?

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
class Fur {}
class Pet { Fur fur = new Fur(); }
class Dog extends Pet {}
Does a Dog object inherit Fur from Pet?
I would say No, because a class inherits members, that is, fields, methods and nested classes and interfaces. Fur is a top-level class.
Why am I wrong?
[ April 15, 2003: Message edited by: Marlene Miller ]
Ravi Anamalay
Ranch Hand

Joined: Apr 15, 2003
Posts: 38
I'd say it inherits fur but not Fur.


with kind regards,<br />Ravi
Charles Leung
Greenhorn

Joined: Mar 02, 2003
Posts: 16
Pet "has a" fur
since Dog "is a" Pet
thus Dog "has a" fur
it didn't say Dog "is a" Fur, right?
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
code reuse can be done in two ways one is through composition and the other inheritance.
Pet has Fur ( composition) not inheritance.
Dog is a Pet( inheritance) inherits Pet so implies that Dog has Fur
one cannot say that Dog inherits Fur ( i.e is Dog is a Fur)
my answer would be no. Dog is not a Fur,
but has a Fur.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The question was does Pet inherit fur not is Pet a child of Fur.
How could this compile if Dog didn't inherit fur?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you one and all.
Actually, the original question was �Which of the following are true statements? a. A Cat object inherits Fur and four Legs from the Dog super class. ...�
I was curious to know how other people understand such a question. In this context, is-a and has-a are more appropriate than JLS definitions.
Anushkha Rana
Greenhorn

Joined: Apr 13, 2003
Posts: 17

posted by Thomas Paul
How could this compile if Dog didn't inherit fur?
code:
--------------------------------------------------------------------------------
class Fur {}class Pet { Fur fur = new Fur(); }public class Dog extends Pet {Fur a = fur;}
--------------------------------------------------------------------------------

I think this code will get compiled not because of Fur is being inherited by Dog but because class Fur is being defined in the same file as class Dog. And having package accessiblity, all the classes within this package can access class Fur.
if Fur is inherited by Dog then this should compile..

*i guess*, i am not making any mistake.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Rana,
I see your point, but I think in your code you meant

[ April 16, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
We seem to be getting confused with inheriting and extending. When I extend a class, I inherit all the non-private members of the class that I extend.
The question was, does Dog inherit Fur from Pet. First the question is phrased incorrectly. We can't inherit Fur but we might inherit fur. (Case is important.)
Let's simplify the question to eliminate the confusion:
class Pet { int a = 10; }
class Dog extends Pet {}
Does Dog inherit a?
Yes, since we inherit non-private member variables from our parent.
class Pet { public void m() {} }
class Dog extends Pet {}
Does Dog inherit m()?
Yes, since we always inherit non-private methods from our parent.
So the original question can be rephrased as, Does Dog inherit the member variable fur from the Pet class? The answer is yes.
Asking if Dog inherits Fur is as meanigless as asking if Dog inherits int in this example:
class Pet { int a = 10; }
class Dog extends Pet {}
[ April 16, 2003: Message edited by: Thomas Paul ]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
class Fur
{
//extended part
int a=2;
int b=3;
public int calculateTheMoose()
{
return (a+b);
}
//End of extended part
}
class Pet {
Fur fur = new Fur();
//following is extended part
public Fur getReferenceFur()
{
return fur;
}
//End of the extended part
}
class Dog extends Pet {}
class Test
{
public static void main(String[] argv)
{
Dog dog=new Dog();
}
}
Does a Dog object inherit Fur from Pet?
No
because if you want Dog to inherit the property of Fur from Pet,you need to do following
class Pet extends Fur{}
then you can use the method of directly to print out the result
calculateTheMoose()
but without using the "extends Fur"
It is not the inherit
you can only use the method to get the reference of fur to print out the num of The Moose
getReferenceFur().calculateTheMoose()
I hope that I can answer your question


Francis Siu
SCJP, MCDBA
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
Originally posted by Thomas Paul:
We seem to be getting confused with inheriting and extending. When I extend a class, I inherit all the non-private members of the class that I extend.

As I understood it, subcalss inherits everything from its superclass. The access modifier private would not prevent inheritance but would prevent access (overloading/overriding) by a subclass. Am I right?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Lih Wang:

As I understood it, subcalss inherits everything from its superclass. The access modifier private would not prevent inheritance but would prevent access (overloading/overriding) by a subclass. Am I right?


From the JLS 8.2:
Members of a class that are declared private are not inherited by subclasses of that class.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Lih and Thomas,
In one of the exam prep books I have, they say (more than once) a class inherits *all* the members of the superclass and *then* overrides some of the methods. Well, this is inconsistent with the JLS, so (due to this and other inconsistencies) I stopped reading that book. Lih, I wonder if you are reading the same book.
Lih Chang
Greenhorn

Joined: Mar 21, 2003
Posts: 19
After reading JLS 8.2, I'm still not quite understand what's inheritance supposed to mean, even get more confused. If private fields not inherited from its superclass, why an instance of subclass coctains all the member fields - although they are not all visible in the subclass - that superclass has. Does it mean there are two objects (one for all private member fields, one for the others) created for each subclass object created? Of course, this is not true. Then, how do I perceive this? Hpoe someone can help me understand the concept. Thanks.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

B b = new B();
When an object of class B is created, memory space is allocated for six variables.
The members of B are the declared fields a, b, d and the inherited field c.
a and b are not members of B. a in A is hidden by a in B, so it is not inherited. b in A is private, so it is not inherited.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- I'll do my best to add to the confusion
Memory has to be allocated for even the non-inheritable things of the supeclass portion of an object, so EVERYTHING from the superclasses are there when a subclass is instantiated.
But the subclass doesn't have access. Although the JLS makes a distinction that those 'private' things are not inherited, they are still *present* as part of the superclass portion of an object. Some people like to think of an object as a thing that is made up of a clumped-together grouping of the subclass plus each of its individual superclass object portions of itself. I tend to think of it more like layers of an onion, where the inner core of the onion/object is the class Object part of an object, and each layer on top adds things for the next part of the inheritance tree.
So if Dog extends Animal,
(and Animal extends Object) then a Dog has an inner "Object" core, wrapped in a layer of type Animal, and the outer layer is type Dog.
If you have a polymorphic reference of type Object, then using that reference you are allowed to see ONLY the Object portions of the Dog, even though at runtime any virtual method invocations will still cause the Dog version of the method to run (if the Dog has overridden a method).
Object o = new Dog(); // can see only Object methods (equals(), toString(), etc.)
If you have a polymorphic reference of type Animal, you can call the Animal stuff:
Animal a = new Dog();
a.eat(); // OK, because eat() is in class Animal
// if Dog has overridden eat(), you'll get the Dog version
If you have a reference of type Dog, you can see into the outermost layer, which has all the Dogness:
Dog d = new Dog();
d.bark(); // now you can call the things which are defined ONLY in class Dog.
But by the way, the whole premise of this original topic is wrong. Everyone KNOWS that Pet should be an interface, not a class
Although Fur is indeed likely to be a HAS-A relationship.
cheers,
Kathy
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you, Kathy.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Does a Dog object inherit Fur?