Professional Documents
Culture Documents
Topic :
Objects and Classes
• State
– The internal attributes of a thing, and their values
• Behavior
– Operations that affect the state of a thing
– Operations a thing performs based on its state
– Methods describe how objects behave and how objects from other classes
can interact with an object
– Mutator methods change state of object -- setAttribute
– Accessor methods return state of object -- getAttribute
• Identity
– Given any two things, we can tell them apart from each other
– Implementation: Record ID, SS#, memory address, etc.
• Class
– Template from which an object is actually made
– Specifies operations and states for a type of object
– An object that conforms to a class description is an instance of that class
– There may be multiple instances (objects) created from a class
– Classes themselves are not “objects”
– It’s the difference between saying
– “Colleges offer courses” and “AFIT offers CSCE694”
– One is a general statement true for all colleges; the other is a specific
example
– Classes describe the general case,
– Objects are the specific instances
* 1
uses has
is-a
* 4
Automobile Tire Manager
Course
-name: String
-credits: int
+printRoster()
+enroll(newStudent: Student)
attends teaches
1..*
Student Professor
-name: String -name: String
-studentID: long -title: String
+printSchedule() +printCourseLoad()
• Attributes
class Employee
{
public Employee(String n, double s)
{
_name = n;
_salary = s;
}
public double salary()
{
return _salary;
}
public void raiseSalary(double percent)
{
_salary *= 1 + percent/100;
}
private String _name;
private double _salary;
}
Air Force Institute of Technology
Apr 2, 2024 Electrical and Computer Engineering 14
Object-Oriented
Programming
Design
Methods
• All parameters are passed by value
– must use objects to pass by reference
class Employee
{ …
public void swapSalary(Employee b)
{ double temp = _salary;
_salary = b.salary
b._salary = temp;
}
}
– cannot change the value passed as b, but can change the contents
– Wrong:
class Employee
{ …
public void assign(Employee b, Employee a)
{
b = a;
}
}
private public
protected
• Creating an object
– Use the keyword new followed by a constructor
Employee newGuy = new Employee( “Tim”, 1000 );
– Constructors
– Default constructor used if none supplied by user
– User-defined constructors may be supplied
– Objects created using new are called “references”
• References
– Essentially a pointer without the C/C++ notation
– No explicit memory deallocation is required
– When used as function parameters, copies memory addresses … not the
objects (like C/C++)
class Employee
{
public Employee( String n, double s )
{
String _name = n;
double _salary = s;
}
…
private String _name;
private double _salary;
}
• Analysis
– Transform vague understanding of a problem into a precise description of
the tasks to be solved
– Concerned with description of what must be done, not how it should be
done
• Design
– Structure programming tasks into classes and packages (logically
grouped clusters of objects)
– Specify operations and attributes of each class
– Specify relationships with other classes in the system
• Implementation
– Classes and operations are coded, tested, and integrated
– Object-orientation encourages evolutionary development since behavior
and state is encapsulated into objects with predefined interfaces – thus
objects or packages can be incrementally added and tested more easily
• Finding classes
– Look for nouns in problem analysis
– Many are good choices for classes
– Other classes may be necessary
• Finding operations
– Look for verbs in problem analysis
– Each operation must have exactly one class responsible for carrying it out
• Finding class relationships
– Association uses; a class uses another class if manipulates objects of
that class in any way; should be minimized
– Aggregation “has-a”, contains;
– Inheritance “is-a”, specialization; useful in select places
Traditional Object-Oriented