• Post Reply Bookmark Topic Watch Topic
  • New Topic

getter / setter question  RSS feed

 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<piggyback>
Or perhaps even...

</piggyback>
[ October 13, 2006: Message edited by: Garrett Rowe ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!