Chp1 1
Chp1 1
Programming
Chapter 1 – Review of object-oriented programming
MARSHIMA MOHD ROSLI
COMPUTER SCIENCE
Review of Object Oriented Programming
Concepts
Chapter 1
Outline a) Object Oriented Programming Concepts
◦ Objects, classes, packages
Forget programming for a while.
Think about the World and the things that
are in it.
◦ What things are objects? What things are not objects?
The last three items on the list seem clear enough. In fact,
they have names:
◦ An object has identity (it acts as a single whole).
◦ An object has state (it has various properties, which
might change).
◦ An object has behavior (it can do things and can have
things done to it).
More about objects..
◦ Software entity
Linked List
A More Formal Definition Attributes
State is a condition or situation during the life of an object, which satisfies some
condition, performs some activity, or waits for some event.
The state of an object normally changes over time.
Name: J Clark
Employee ID:
567138
HireDate:
07/25/1991
Status: Tenured
Name: J Clark Discipline:
Employee ID: 567138 Finance
Date Hired: July 25, 1991 MaxLoad: 3
Status: Tenured
Discipline: Finance Professor Clark
Maximum Course Load: 3 classes
An Object Has Behavior
class Bicycle {
// Data Member
private String ownerName;
//Constructor: Initialzes the data member
public void Bicycle( ) {
ownerName = "Unknown";
}
//Returns the name of this bicycle's owner
public String getOwnerName( ) {
return ownerName;
}
//Assigns the name of this bicycle's owner
public void setOwnerName(String name) {
ownerName = name;
}
}
Multiple Instances
Once the Bicycle class is defined, we can create multiple instances.
bike1 bike2
: Bicycle : Bicycle
bike1 = new Bicycle( );
bike1.setOwnerName("Adam Smith");
ownerName ownerName
bike2.setOwnerName("Ben Jones");
Sample Code
The Program Structure and
Source Files
BicycleRegistration Bicycle
Class Comment
Data Members
Methods
(incl. Constructor)
}
Data Member Declaration
}
Quick Check!
1. Extend the Bicycle class by adding the second data
member tagNo of String. Declare this data member as
private.
public Bicycle ( ) {
ownerName = "Unassigned";
} Statements
Constructors
Type of constructor
◦ Default constructor (no argument)
public Bicycle() {
gear = 1; public Bicycle() {
cadence = 10;
speed = 0; }
}
public Bicycle() {
gear = 1;
cadence = 10;
speed = 0;
}
Constructor
class Test {
private double score;
public Test(double val) {
//assign the value of parameter to
//the data member
}
}
Accessor (get method)
//Returns String
public the name getOwnerName()
of this bicycle's owner
{
return ownerName;
}
Page 1 Page 2
The Program Structure for
SecondMain
Bicycle
SecondMain
Account
Object Orientation
Encapsulation
Abstraction
Modularity
Hierarchy
What Is Abstraction?
Student Professor
Improves Resiliency
Encapsulation Illustrated
Professor Clark
Name: J Clark
Employee ID: 567138
SetMaxLoad(4)
HireDate: 07/25/1991
Status: Tenured
TakeSabbatical()
Discipline: Finance
MaxLoad:3
What Is Modularity?
Course
Catalog
System
Course Registration
System Student
Management
System
What Is Hierarchy?
Increasing Asset
abstraction
Manufacturer B
Manufacturer A Manufacturer C
OO Principle:
Encapsulation
Remote Control
Example: Polymorphism
financialInstrument.getCurrentValue()