Download presentation
Presentation is loading. Please wait.
Published byShawn Ford Modified over 9 years ago
1
Object Oriented Programming B.Sc. Digital MediaMultimedia Studio
2
Agenda Procedural Programming v OOP Software Objects Classes and Methods The Object Oriented Paradigm
3
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”
4
Procedural Programming 10 readln(sum) 20 sum = sum + x 30 writeln(“The answer is” ), sum). 60 GOSUB 200. 200 readln(answer). 300 return
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
Vehicle Object Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Various Behavior Public Visibility (Interface to Object)
13
Encapsulation (Data Hiding) Vehicle Set Make Get Make Set Registration No. 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
14
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”
15
Class Structures and Methods
16
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;
17
Set Method Definition (.as) // ------- set the registration number ------- // public function setRegNumber(aRegNumber:String):void { this.regNo = aRegNumber; } // end setRegNumber
18
Get Method Definition (.as) // -------- get the registration number ------ // public function getRegNumber():String { var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber
19
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 works make = aCar.getMake(); reg = aCar.getRegNumber(); trace(make); trace(reg);
20
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
21
Transport System Subclass Example LeaseVehicle SuperclassVehicle Subclass
22
Transport System: Vehicle Object: Vehicles Properties (attributes) Type Make Model Value Registration Number B.Sc. Multimedia ComputingMultimedia Authoring Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehicle Set the value of the vehicle Any subclass of Vehicle will inherit all the properties and methods of the Vehicle class
23
Transport System: Vehicle and LeaseVehicle 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 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 Subclass LeaseVehicle Properties LeaseValue LeaseDate
24
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;
25
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; }
26
Inherited Set Definitions // ------- not required as inherited from superclass ------- // public function setRegNumber(aRegNumber:String):void { this.regNo = aRegNumber; } // end setRegNumber
27
Inherited Get Definitions // -------- not required as inherited from superclass ------ // public function getRegNumber():String { var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber
28
ActionScript 3.0 Implementation Details // instantiate a new LeaseVehicle object var aLeaseCar:LeaseVehicle = new LeaseVehicle(); //local variables var 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 data trace(make); trace(reg);
29
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.
30
Vehicle Class getSaleValue Method public method getSaleValue():int{ var price:int; price = 12000; return price };
31
Override Method for LeaseVehicle Class override public method getSaleValue():int{ var price:int; price = (12000 - discount); return price };
32
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
33
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
34
Summary Encapsulation - data hiding Inheritance - subclassing Polymorphism - overriding behaviors Access Modifiers - controlling access to objects and methods
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.