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

Accessing methods in other classes

Sowm Herur
Ranch Hand

Joined: Jul 07, 2008
Posts: 32
Say i have

Now to access the method Coarse_Offered() in the class University do i have to create an instance of the class University in the class Student even though both these classes are in the same package?

Please Clarify,

P Goel
Greenhorn

Joined: Nov 15, 2008
Posts: 12
to access a method of another class either you should have its instance or method should be static
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Coarse_Offered()

Surely you mean Course


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300

I think we should have instance of University in Student Class. It follows Has-A relationship.

Please correct me if i am wrong.

Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 210
Dear Sowm Herur,

From your question, I can say that you have a problem of understanding how really Java work as an object-oriented (OO) programming language, which is totally different from other language such as C or VB where calling a method is just simply invoked with the method name.

Also from the question, "package" have nothing to do with invoking a method (or in OO, means sending message from one object to another object), "package" will affect the access control of your class or member.

And from your code, clearly show that you did not put your effort to compile and test it ... can you explain whether is it a class ? abstract class ? or interface ? ... if it is a class, how come the method signature end with (); , which is an abstract method without implementation ?

If you keep on with this attitude, you are not able to pick up Java knowledge, not even the fundamental. I suggest you to have a good reference book with you rather than simply study in a mess, and please clarify yourself the concept of class and object.

ALL THE BEST


SCJP 6, SCWCD 5, SCBCD 5
Sowm Herur
Ranch Hand

Joined: Jul 07, 2008
Posts: 32
[Deleted as not relevant to this thread]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Temporarily closing this thread. Hope to reopen it in a few minutes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Anything related to what I have deleted should be discussed here in the Ranch Office forum.

Anything related to "accessing methods" should be discussed here, please.

Thread reopened.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Sowm Herur wrote:Now to access the method Coarse_Offered() in the class University do i have to create an instance of the class University in the class Student even though both these classes are in the same package?


Well, you probably wouldn't create University objects in the Student class, nor would you create Student objects in the Unversity class. It's because University and Student form an aggregation relationship. They exist independenly of each other and form temporary relationships only. Therefore each University object will have existing Student objects passed in. And each Student object will have existing University objects passed in.

When a realationship ends, say because a student leaves a university, the Student objects isn't destroyed, it's just removed from the University object. The Student object may be passed in to another University object indicating the student now belongs there.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
I was just looking for such an explanation on the same. I was also in doubt about the relationship between university and student.

nossnahoj, what will be the better way then to access course_offered method of the University.

I request you to explain more about the design university and student should have.

Thank a lot

Regards
Patricia
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Patricia Samuel wrote:nossnahoj, what will be the better way then to access course_offered method of the University.

I request you to explain more about the design university and student should have.


Well, because there's an aggregation relationship between University and Student, none of these should create objects of the other. If they did they would become owners of these objects and that would be an ordinary composition relationship. Instead existing objects should be passed in. For example when a student selects a university a University object is passed in to a Student object.

Nothing prevents the Student object from accessing the courses_offered method of the University object it holds. Also Student could have a courses_selected method which a University object could access. The university may want to access this information for different planning purposes for example.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 210
Without taking in consideration of design, the simple answer is sure. You need to create an instance of class University in class Student to access the method.

Since Sowm Herur touch the word "package", I try to move a step deeper to the explanation, the purpose of creating an instance of class University in class Student is to allow a Student object to talk (or sending message) to University object, so it doesn't matter whether both the class is in same package or in different package. By reading your code that having default access control for both class, if they are in different package, you will not able to access class University in class Student, thus, not able to create an instance of University in class Student.

Therefore, package doesn't have direct relationship to instantiation.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 210
Taking in the consideration of design




so the University instance only available in the scope of requestCourse() , Student doesn't hold composition/aggregation with University. Futhermore, plug in new classes like TrainingCompany, College with the corresponding Command subclass enable you to achieve more flexible information request.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hey Lee,

Please help me in figuring out the design pattern that are applied in this strucutre -

I think it follows command pattern ( I know its pretty obvious from the naming convention )

Is there any other pattern that is applicable here?

And i really apologize if this query is irrelevant but i am an eager learner of design pattern/principle

Thanks in advance.

Regards
Patricia

Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Patricia Samuel wrote:I think it follows command pattern ( I know its pretty obvious from the naming convention )


Yes it's the Command design pattern.

The reason to use it in this case would be to isolate "requests" and handle them uniformly. The design looks a little complicated though and not quite functional.

For example what's the purpose of the Invoker class? It seems like an unmotivated complication to me.

And how does a Student receive the requested info? It's not enougth to print it at the University. This design allows you to request stuff but not to get your hands on it.

Also it's stated the design will enable you to forward requests to other educational institutes apart from universities but that's not visible in the design.
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 210

Yes it's the Command design pattern.

The reason to use it in this case would be to isolate "requests" and handle them uniformly. The design looks a little complicated though and not quite functional.

For example what's the purpose of the Invoker class? It seems like an unmotivated complication to me.

And how does a Student receive the requested info? It's not enougth to print it at the University. This design allows you to request stuff but not to get your hands on it.

Also it's stated the design will enable you to forward requests to other educational institutes apart from universities but that's not visible in the design.


Right, command pattern, I just wrote it in a rush that day to show how you can delegate the task to the invoker without directly calling the university ... also because here have no specific requirement in this post for me to really show the usefulness of the pattern.

For the pattern to be useful, one might need to combine with strategy pattern.

First, move the "public static void main" to client programmer class. Create a superclass/interface which represent Company, so it can represent not just Education Institution, also for Company and non-profit Company. The having a interface inheritance which define the requestInfo(), so for different type of Company, you can provide different information which is not just Courses, maybe Courses means Products.

So, what I try to mean is one shouldn't simply using design pattern for fun, one have to decide it depend on the situation/requirement where it will help on improve the reusability, loose coupling, flexibility and which will help in future maintanance.

Having the above explanation, here I claim the code that I have written on previous post is not a good example since it brings complexity instead of solving a problem.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing methods in other classes
 
Similar Threads
Inheritance in Spring
HashMap and Input/Out Put file
getFirstName from one class (set & get) to another class (Main Program)
package and import doubt
Drop and Add Students to Course