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 Objects question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects question" Watch "Objects question" New topic
Author

Objects question

Joseph Potts
Greenhorn

Joined: Jul 21, 2012
Posts: 7
I'm just trying to get this clear here. Imagine I create two classes, one named Person and one named Dog and imagine that Person contains a few methods and that I'm in the Dog class. If I wanted to make a reference object (correct me if i'm not saying it right) would the correct be:



Would I use to link a reference object to the Person class? If so, is the point of making a reference object to have a way to make it link to the other class? Or is there more to it?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
This

Person p = new Person();

creates an instance of the class Person called p. Creating an instance of Person (or another Java Object) uses one of the fundamental elements of Object Oriented Program. The Person instance p can then be used to represent a Person object but with its own characteristics as described by the class' fields, behaviors, etc. That instance p can be shared with other classes or used as needed throughout the program. So, in answer to your last question, I'd say, Yes, there's more to it.

I'm sure you'll have more questions, so ask away.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Joseph Potts
Greenhorn

Joined: Jul 21, 2012
Posts: 7
Greg Brannon wrote:This

Person p = new Person();

creates an instance of the class Person called p. Creating an instance of Person (or another Java Object) uses one of the fundamental elements of Object Oriented Program. The Person instance p can then be used to represent a Person object but with its own characteristics as described by the class' fields, behaviors, etc. That instance p can be shared with other classes or used as needed throughout the program. So, in answer to your last question, I'd say, Yes, there's more to it.

I'm sure you'll have more questions, so ask away.


Thank you for your answer . But i'm a little confused on your last comment, is your answer Yes to my thoughts that creating an instance of Person makes it an object that can be linked to the class the instance was made from or 'is there more to it' or both.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
I'm not sure what you mean by "linked to the class." Since that's not a common term or concept in Java, I'd say, No, that is not quite right. However, you might have something in mind that is appropriate, but you're not describing it right or using the right name for it. So instead of 'linked', I said the Person instance p could be shared throughout the program, if programmed correctly, of course.
Joseph Potts
Greenhorn

Joined: Jul 21, 2012
Posts: 7
Greg Brannon wrote:I'm not sure what you mean by "linked to the class." Since that's not a common term or concept in Java, I'd say, No, that is not quite right. However, you might have something in mind that is appropriate, but you're not describing it right or using the right name for it. So instead of 'linked', I said the Person instance p could be shared throughout the program, if programmed correctly, of course.


Got i, thank you so much Was stuck on this topic for quite some time.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

To put it a little differently, when one object, say --or if you prefer, imagine -- a Dog, holds a reference to a Person object, this is basically saying "A Dog knows about a Person and can interact with it." The "interaction" between the Dog and the Person is usually in the form of the Dog invoking methods on the Person object.

Many aspects of Object-Oriented Analysis and Design revolves around establishing these "knows about" relationships, determining whether they are appropriate or not, and limiting the scope (what methods can be called by other classes) and breadth (what other classes are allowed to interact with this class) of the relationship/interactions. To some extent, you will also consider things like reciprocity/direction (if this class knows about another, should the other class know about this one too?) and cardinality (does this class need to know about one instance only, or should it know about multiple instances). For example, in the case of a Person and Car, a Person can own multiple Cars, therefore the cardinality of the Person:Car relationship is 1:N (one-to-many). On the other hand, you may also have a problem domain/context in which you want to model the fact that throughout its existence, a Car may have been owned by multiple Persons, thereby adding a temporal aspect to the relationship (something considered over some period of time), and establish a 1:N relationship between Car and Person, with the added complication of figuring out how TimePeriod plays into this relationship.

I could go on but this just demonstrates that while, on the surface, the answer to your question may seem to be "It's simply establishing a 'knows about' relationship," when you dig a little deeper, there really is quite a bit more to it. All it takes is a little imagination.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects question