• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HAS-A problem

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Horse is not a Cow.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Horse has a Cow.
class Horse{
Cow myCow;
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic