View Single Post
  #2 (permalink)  
Old November 14th, 2011, 06:43 PM
ciderpunx ciderpunx is offline
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by code_lover View Post
I have 2 classes in Java with attributes as follows:

public class Student{
String name;
String address;
String course;
Professor advisor; //here i have used a user-defined type
}


public class Professor{
String name;
String address;
String course;
Student advisee; //here i have used a user-defined type
}

I want to make sure as to whether i am using "Composition" as an OOP technique here or not. If not then what is Composition in OOP, please explain clearly in detail with an example.
Thanks
Yes you are using composition, in that you are composing your Student Class with a Professor attribute (and vice versa). Composition is a fancy way of saying has-a . You can read that a Student "has-a" Professor. The other common technique you might find is Inheritence. For which you can read "is-a". You could say a Student "is-a" Person (as is a Professor).

HTH
__________________
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk