File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes HAS-A problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HAS-A problem" Watch "HAS-A problem" New topic
Author

HAS-A problem

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



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

Joined: Feb 07, 2005
Posts: 2367
A Horse is not a Cow.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Horse has a Cow.
class Horse{
Cow myCow;
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Cow myCow=new Horse();


Take a look at this line though.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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

Joined: Jan 03, 2006
Posts: 490
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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jan 05, 2006
Posts: 477
Thanks for the reply.How about now?



But it's giving NullPointerException.I already declared a number for tie() method.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HAS-A problem
 
Similar Threads
Overloaded question
how to correct the error
Has-A relationship
how to use super keyword within the main method.
overload and override method question