|
A new philosophy, a new way of life, is not given for nothing. It has to be paid dearly for and only acquired with much patience and great effort. Fyodor Dostoevsky
Object Interactions
Process of OO software development involves:
Example:
Method signature ( header ) -- The method's name and the number of its parameters
and the types of its parameters as well as its return type.
The purpose is to indicate the method's name and the number of
its parameters and the types of its parameters.
Java Syntax:
( In C++, a method signature is exactly the same thing as a function prototype. )
How do we represent in code that a method is being invoked on an object?
Ans: We create what is known as a message.
Example
//Instantiate a Student Object //Send a message to Student object 'x', asking it to register for course //CS 420, section 1 x.registerForCourse( "CS 420", 1 ); |
If a request is made of an object A and, in fufilling the request, A in turn requests assistance from another object B, this is known as delegation by A to B.
The delegated object can work 'behind the scene'.
same sense as real world examples.
Example: Java AWT Delegation Event Model
The only way that an ojbect A can pass a message to object B is if A has access to a handle on B. This can happen in several ways:
class Student {
//Attributes
String name;
Professor advisor; //handle to Professor object
//....
}
|
Student s; Course c; ..... c.resister ( s ); ..... |
c.register( s );
Course object c ~ client ( requesting service )Student object s ~ server ( providing service: e.g. getTranscript() )
publice static void main( String [] args )
{
Course c = new Course();
Student s = new Student();
.....
//invoke a method on Course object c
c.resister ( s );
.....
}
|
class Course {
.....
boolean register ( Student s ) {
//Request a handle on a the Transciprt object of Student s
Transcript t = s.getTranscript();
//now, request a service on Transcript object t,
// assuming that c2 is a handle on some prerequisite course ...
if ( t.successfullyCompleted ( c2 )
return true;
else
return false;
......
}
.....
}
|
Whenever an object A is a client of object B, object B in turn can be considered as a server or agent of A.
Information Hiding goes hand by hand with Encapsulation as it is implemented within a class by making its attributes and methods not visible from outside its boundary.
Why information hiding?
JAVA ways of implementing Information Hiding:
Example of JAVA class showing Information Hiding:
class Treasure
{
private double amount;
private int codedNumber ;
Treasure(double tmpMoney) //constructor
{
amount = tmpMoney;
}
private void calculate() //private method
{
amount*= codedNumber;
}
.....
}
|
Public visibility:
public methods and attributes can be accessed by client code via dot notation
class Treasure
{
private double amount;
private int codedNumber ;
Treasure(double tmpMoney) //constructor
{
amount = tmpMoney;
}
.....
private void calculate() //private method
{
amount*= codedNumber;
}
public double getValue() //public method
{
return amount;
}
.....
}
|
Protected visibility:
class Treasure
{
private double amount;
private int codedNumber ;
Treasure(double tmpMoney) //constructor
{
amount = tmpMoney;
}
.....
private void calculate() //modifier/mutator
{
amount*= codedNumber;
}
public double getValue() //accessor
{
return amount;
}
.....
}
|
|
|