Is-a, Has-a, and Knows-a Relationships, and data passing
CSC 140
Topics
Is-a Relationship or Inheritance
Syntax tip-off: the keyword extends
Your derived class IS an instance of the base class, in every way. It can be used anywhere the base class can be used.
Access to all public and protected data and methods in base class, but not private.
The derived class initiates the method calls. The base class is passive.
Data can pass either way: from derived class to base class or from base class to derived class.
Inheritance from Suns standard Java classes is very common. Using inheritance in your design should be done with more care.
Has-a Relationship or Composition
Syntax tip-off: the keyword new
The contained class is created within the containing class.
The containing class has-a instance of the contained class.
The containing class has access only to the public data and methods in contained class.
The containing class initiates the method calls. The contained class is passive.
Data can pass either way: from containing class to contained class or from contained class to containing class.
Knows-a Relationship or Reference
Syntax tip-off: absence of the keyword new
The knowing class must get a reference to the known class from some other object. The reference is how the knowing class knows the known class.
The knowing class has access only to the public data and methods in known class.
The knowing class initiates the method calls. The known class is passive.
Data can pass either way: from knowing class to known class or from known class to knowing class.
Very similar in actual use with a Has-a relationship. The only difference is where the object is created.
The Knows-a relationship is not talked about very much in the standard textbooks.
Data passing (not a relationship)
Syntax tip-off: lack of any of the above three relationships between two objects.
A third object, with relationships to both objects, must get data from one object and then pass that data to the other object.
Two step process.
Generally inferior design because the objects themselves to not actively exchange information.
In-Class Lab
None.
Readings
This material is not in the text.
Office: B132
Phone: 353-2250
sbadman@parkland.edu