Agenda
Procedural Programming v OOP Software Objects Classes and Methods The Object Oriented Paradigm
Object Oriented Programming
Object-Oriented Programming is a way of describing
and structuring software systems that features objects
that contain functionality and data
Early program structure techniques kept program
functionality and data separate - so called
“procedural programming”
Procedural Programming
10 readln(sum)
20 sum = sum + x
30 writeln(“The answer is” ), sum)
.
60 GOSUB 200
.
200 readln(answer)
.
300 return
Object Oriented Programming
OOP places the data and functionality inside a software object - the data is said to be encapsulated within the object
Software objectdata and
functionality
Object Oriented Programming
Software objects communicate with other software objects to carry out the overall function of a software system by sending messages to each other
Software object data and
functionality
Software object data and
functionality
Transport System
To model vehicles and drivers as an OOP software systemthink about the various objects, properties and functions of such asystem:
Vehicles - Type, Make, Model, Value, Registration Number
Drivers - Name, Age, Date of Birth, License Type
Transport System: Vehicle
Object: VehiclesProperties (attributes)
TypeMakeModelValueRegistration Number
Functions (methods)
Create an object of type VehicleSet the Make of vehicleGet the make of vehilceSet the value of the vehicle
Transport System: Driver
Object: Driver Properties (attributes)
NameDate of BirthLicense TypeValue
Functions (methods)
Create an object of type DriverSet the Name of a driverGet the name of a driverSet the type of licenceGet the type of licence
Vehicle Object
Vehicle
Properties:
Make
Model
Registration No.
Value
Set Make
Get Make
Set Registration No.
Get Registration No.
Functions (methods) to communicate with a Vehicle object
Driver Object
Driver
Properties:
Name
Date of Birth
License Type
Set Name
Get Name
Set Date of Birth
Get Date of Birth
Functions (methods) to communicate with a Driver object
Set Licence Type
Get License Type
Vehicle Object
Vehicle
Properties:
Make
Model
Registration No.
Value
Set Make
Get Make
Set Registration No.
Various Behavior
Various Behavior
Public Visibility
(Interface to Object)
Encapsulation (Data Hiding)
Vehicle
Set Make
Get Make
Set Registration No.
Various Behavior
Various Behavior
Known and consistent public interface
Internal workings of class hidden users of the class access functionality via the public interface
Owners of the class may change internal representations inside the class but maintain known public interface for users
Object Oriented Programming Features
Classes are templates for objects, classes define properties and methods which dictate the state and behavior of the resulting software object
Data and related functions are contained within the same object space - encapsulation (data hiding)
Classes can be extended by creating subclasses which have similar properties and behaviors - inheritance
Inherited behaviors can be modified (overridden) to change the way an object responds to common messages- polymorphism
These three characteristics of OOP are often referred to as the “Object Oriented Paradigm”
Class Definition for a Vehicle (.as)public class Vehicle {
// ------ constructor------ //
public function Vehicle(){
} // end constructor
// ------ properties ------ //
var regNo:String;var make:String;var model:String;var saleValue:int;
Set Method Definition (.as)
// ------- set the registration number ------- //
public function setRegNumber(aRegNumber:String):void {
this.regNo = aRegNumber;
} // end setRegNumber
Get Method Definition (.as)
// -------- get the registration number ------ // public function getRegNumber():String {
var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber;
} // end getRegNumber
ActionScript 3.0 Implementation Details (.fla)
var aCar:Vehicle = new Vehicle();
var reg:String;var make:String;
aCar.setRegNumber("BKD 1");aCar.setMake("Ferrari");
// verify it worksmake = aCar.getMake();reg = aCar.getRegNumber();
trace(make);trace(reg);
Inheritance (subclassing)
A class can be extended by creating a subclass based on the class. The subclass will inherit the properties and behavior of the superclass from which it is based
Additional properties and behavior can be added to the subclass
B.Sc. Multimedia ComputingMultimedia Authoring
Transport System: Vehicle
Object: VehiclesProperties (attributes)
TypeMakeModelValueRegistration Number
B.Sc. Multimedia ComputingMultimedia Authoring
Functions (methods)Create an object of type VehicleSet the Make of vehicleGet the make of vehicleSet the value of the vehicle
Any subclass of Vehicle will inherit all the properties and methods of the Vehicle class
Transport System: Vehicle and LeaseVehicle
Object: VehiclesProperties (attributes)
TypeMakeModelValueRegistration Number
Functions (methods)Create an object of type VehicleSet the Make of vehicleGet the make of vehicleSet the value of the vehicle
FunctionsCreate an object of type LeaseVehicleSet the leaseValueGet the leaseValueSet the leaseDateGet the LeaseDate
Subclass LeaseVehicle
Properties
LeaseValue LeaseDate
Class Definition for LeaseVehicle
public class LeaseVehicle extends Vehicle {
// ------ constructor------ //
public function LeaseVehicle(){this.make = “Maserati”;this.regNo= “1 BKD”;} // end constructor
// ------ properties ------ //var leaseValue:int;var leaseDate:Date;
Class Definition for LeaseVehicle
public class LeaseVehicle extends Vehicle {// ------ constructor------ //
public function LeaseVehicle(){this.regNo = "1 BKD";this.make = "Maserati";} // end constructor
// ------ properties ------ //var leaseValue:int;var leaseDate:Date;
}}
Inherited Set Definitions
// ------- not required as inherited from superclass ------- //
public function setRegNumber(aRegNumber:String):void {
this.regNo = aRegNumber;
} // end setRegNumber
Inherited Get Definitions
// -------- not required as inherited from superclass ------ // public function getRegNumber():String {
var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber;
} // end getRegNumber
ActionScript 3.0 Implementation Details
// instantiate a new LeaseVehicle objectvar aLeaseCar:LeaseVehicle = new LeaseVehicle();
//local variablesvar reg:String;var make:String;
// set the local variables by calling the inherited methods// from the Vehicle class
make = aLeaseCar.getMake();reg = aLeaseCar.getRegNumber();
// verify the datatrace(make);trace(reg);
Overriding Methods
Subclassed methods can be overridden to modify their behavior for agiven software design requirement that most reflects the inheritedbehaviour.
The Vehicle class has a getSaleValue method which is not suited for aLeaseVehicle - lease vehicles are sold at the end of the lease period for awell below market value.
Thus the getSaleValue method in the LeaseVehicle class couldbe overridden to reflect this modified behaviour.
Vehicle Class getSaleValue Method
public method getSaleValue():int{var price:int;price = 12000;
return price};
Override Method for LeaseVehicle Class
override public method getSaleValue():int{var price:int;price = (12000 - discount);
return price};
Override Method for LeaseVehicle Class
salePrice = aVehicle.getSaleValue()
Gets the sale price for a Vehicle
salePrice = aLeaseVehicle.getSaleValue()
Gets the sale price – discount value for a Lease Vehicleas the getSaleValue method has been overidden (modified)in the LeaseVehicle class
Access Modifiers
Access to an object’s properties and methods by other objects I
controlled by using an access modifier - public, private , or protected
public - available to all code private- available only within the same class protected - available within the same class and subclasses