• 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

HAS-A problem

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Horse has a Cow.because Horse declares an instance variable of type Cow.But,how can I call Cow class method in Horse class.Can you help me please? I tried my best but end up with error.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Horse is not a Cow.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Horse has a Cow.
class Horse{
Cow myCow;
}
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cow myCow=new Horse();



Take a look at this line though.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.Did you mean that i need to take off that line.Could you please explain it more clear for me.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are abusing the is-a and has-a relationships to the point where it becomes senseless:

class Cow extends Horse// this is saying Cow is a Horse if anything, both classes should extend Animal or FarmAnimal or the like



Why? A horse has legs, tail, ect. Can be ridden, eat, make noises, ect. But has a Cow? Is it freaking out or something?

class Horse extends Objective61 //why extend a class whose only member is main?

The problem Mr. Lynn pointed out is called derived class reference to a base class object. Look this up. You can't do this, because if you could it would have undefined behavior. You can do the converse and is a quite useful tool when used under a inheritance heirarchy that makes sense: Horse horse = new new Cow(); // not implying this makes sense
[ June 16, 2006: Message edited by: Rusty Shackleford ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Horse is not a Cow. It does not extend Cow, it does not implement Cow, it is not in any way a subclass of Cow. You cannot create a Horse and assign it to a reference variable declared to be a Cow because they are not in any way the same thing.

I suggest looking at the Java Tutorial.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.How about now?



But it's giving NullPointerException.I already declared a number for tie() method.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your reference to an instianted Cow object?

When myHorse is created, myCow is set to null by default, when not explicity created in a explicit constructor.

If a horse and a cow share attributes, they should be in their own class that implements some interface. That way Horse has a <whatever attribute that int is supposed to be>, and not a Cow which makes no sense.

Go back over the whole concept of inheritance and the reasons it is there.
[ June 16, 2006: Message edited by: Rusty Shackleford ]
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic