Forums Register Login

Accessing methods in other classes

+Pie Number of slices to send: Send
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,

+Pie Number of slices to send: Send
to access a method of another class either you should have its instance or method should be static
+Pie Number of slices to send: Send
 

Coarse_Offered()


Surely you mean Course
+Pie Number of slices to send: Send

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

Please correct me if i am wrong.

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
[Deleted as not relevant to this thread]

+Pie Number of slices to send: Send
Temporarily closing this thread. Hope to reopen it in a few minutes.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 


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 can't beleive you just said that. Now I need to calm down with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 7448 times.
Similar Threads
package and import doubt
Drop and Add Students to Course
getFirstName from one class (set & get) to another class (Main Program)
Inheritance in Spring
HashMap and Input/Out Put file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:07:05.