370 likes | 776 Views
Object Oriented Programming. Multimedia Studio. B.Sc. Digital Media. 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
E N D
Object Oriented Programming Multimedia Studio B.Sc. Digital Media
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 object data 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 Software object data and functionality data and functionality
Transport System To model vehicles and drivers as an OOP software system think about the various objects, properties and functions of such a system: Vehicles - Type, Make, Model, Value, Registration Number Drivers - Name, Age, Date of Birth, License Type
Transport System: Vehicle Object: Vehicles Properties (attributes) Type Make Model Value Registration Number Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehilce Set the value of the vehicle
Transport System: Driver Object: Driver Properties (attributes) Name Date of Birth License Type Value Functions (methods) Create an object of type Driver Set the Name of a driver Get the name of a driver Set the type of licence Get the type of licence
Vehicle Object Functions (methods) to communicate with a Vehicle object Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Get Registration No.
Driver Object Functions (methods) to communicate with a Driver object Driver Properties: Name Date of Birth License Type Set Name Get Name Set Date of Birth Get Date of Birth Set Licence Type Get License Type
Vehicle Object Public Visibility (Interface to Object) Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Various Behavior Various Behavior
Encapsulation (Data Hiding) Known and consistent public interface Vehicle 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 Set Make Get Make Set Registration No. Various Behavior Various Behavior
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) varaCar:Vehicle = new Vehicle(); varreg:String; varmake:String; aCar.setRegNumber("BKD 1"); aCar.setMake("Ferrari"); // verify it works make = 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 Multimedia Authoring B.Sc. Multimedia Computing
Transport System Subclass Example Vehicle Superclass LeaseVehicle Subclass
Transport System: Vehicle Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehicle Set the value of the vehicle Object: Vehicles Properties (attributes) Type Make Model Value Registration Number Any subclass of Vehicle will inherit all the properties and methods of the Vehicle class Multimedia Authoring B.Sc. Multimedia Computing
Transport System: Vehicle and LeaseVehicle Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehicle Set the value of the vehicle Functions Create an object of type LeaseVehicle Set the leaseValue Get the leaseValue Set the leaseDate Get the LeaseDate Object: Vehicles Properties (attributes) Type Make Model Value Registration Number 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 ------ // varleaseValue:int; varleaseDate: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 { varaRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber
ActionScript 3.0 Implementation Details // instantiate a new LeaseVehicle object varaLeaseCar:LeaseVehicle = new LeaseVehicle(); //local variables varreg:String; varmake:String; // set the local variables by calling the inherited methods // from the Vehicle class make = aLeaseCar.getMake(); reg = aLeaseCar.getRegNumber(); // verify the data trace(make); trace(reg);
Overriding Methods Subclassed methods can be overridden to modify their behavior for a given software design requirement that most reflects the inherited behaviour. The Vehicle class has a getSaleValue method which is not suited for a LeaseVehicle - lease vehicles are sold at the end of the lease period for a well below market value. Thus the getSaleValue method in the LeaseVehicle class could be 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 Vehicle as 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
Summary • Encapsulation - data hiding • Inheritance - subclassing • Polymorphism - overriding behaviors • Access Modifiers - controlling access to objects and methods