Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes getter / setter question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "getter / setter question" Watch "getter / setter question" New topic
Author

getter / setter question

mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24
Hi.
I'm supposed to create 3 classes ... Course, Instructor and CourseInstructor. Classes Course and Instructor have their own specific instance variables. Class CourseInstructor uses one variable from Course and one variable from Instructor.



Will that work? I just used the same setter and getter methods from the other 2 classes. Is there a more elegant way to do it?
I just need to populate the variables and then I can manipulate it in a main class or something.
Thanks
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6603
    
    1

Your current code will not compile. It looks like you closed the class right after you defined your instance variables and then you are defining funtions after this that dont go into a class body. Also... ClassInstructor does not extend from any class so you are not using the instance variables of any other class. I suggest you extend from both classes. That way you dont have to redefine variables or methods (assuming you dont want to override getters and setters for a more specific operation).


SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
I suggest you extend from both classes...
Not in Java (no multiple inheritance here). Although when you consider the is-a and has-a relationships, intuitively a CourseInstructor is-a(n) Instructor, and has-a Course. So the CourseInstructor class might look like this.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
<piggyback>
Or perhaps even...

</piggyback>
[ October 13, 2006: Message edited by: Garrett Rowe ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6603
    
    1

I suggest you extend from both classes...


Whoops !!! my bad. Wrongly worded. This is what I meant...

CourseInstructor -> Instructor -> Course

Of course this is just to have access to the getters and setters. CourseInstructor is a Instructor and has a course makes more sense.
[ October 13, 2006: Message edited by: John Meyers ]
mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24
Thanks for the replies. I can't use inheritance and abstracts and interfaces because the prof has only covered (lesson 1) Intro to Java and (lesson 2) Classes and Objects.
The problem was: Make 3 classes course, ins.... they all have their specific variables... (without using inheritance, is there still a way I can just use the setter/getter for course and instructor for courseinstructor?) Then write a main method, populate the variables and given the course code, display the instructor name.
Here's the code I ended up writing, mind you, it's pretty long because of all the setters and getters I had to use. It works, but it's pretty clunky. So, is there anyway to shorten this? And/or improve this, without using inheritance, abstract or interfaces?

Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getter / setter question
 
Similar Threads
null pointer exception
problem in one-to-many mapping
Encapsulation in Java
Array Understanding
org.hibernate.PropertyAccessException