• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does a Dog object inherit Fur?

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say it inherits fur but not Fur.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pet "has a" fur
since Dog "is a" Pet
thus Dog "has a" fur
it didn't say Dog "is a" Fur, right?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question was does Pet inherit fur not is Pet a child of Fur.
How could this compile if Dog didn't inherit fur?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rana,
I see your point, but I think in your code you meant

[ April 16, 2003: Message edited by: chi Lin ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Kathy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic