Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Structures and Methods B.Sc. Multimedia ComputingMultimedia Authoring.

Similar presentations


Presentation on theme: "Class Structures and Methods B.Sc. Multimedia ComputingMultimedia Authoring."— Presentation transcript:

1 Class Structures and Methods B.Sc. Multimedia ComputingMultimedia Authoring

2 Agenda Encapsulation Inheritance Polymorphism Extending Classes and Overriding Methods Access Modifiers B.Sc. Multimedia ComputingMultimedia Authoring

3 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” B.Sc. Multimedia ComputingMultimedia Authoring

4 Vehicle Object B.Sc. Multimedia ComputingMultimedia Authoring Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Various Behavior Public Visibility (Interface to Object)

5 Encapsulation (Data Hiding) B.Sc. Multimedia ComputingMultimedia Authoring 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

6 Transport System Subclass Example B.Sc. Multimedia ComputingMultimedia Authoring LeaseVehicle SuperclassVehicle Subclass

7 Inheritance (subclassing) A class can be extended by creating a subclass bases 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

8 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

9 Transport System: Vehicle and LeaseVehicle 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 Functions Create an object of type LeaseVehicle Set the leaseValue Get the leaseValue Set the leaseDate Get the LeaseDate Subclass LeaseVehicle Properties LeaseValue LeaseDate

10 Class Definition for LeaseVehicle B.Sc. Multimedia ComputingMultimedia Authoring 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;

11 Class Definition for LeaseVehicle B.Sc. Multimedia ComputingMultimedia Authoring package uk.ac.uwe.multimedia.transport{ // import Vehcicle class to extend import uk.ac.uwe.multimedia.transport.Vehicle; // base this class on the Vehicle class public class LeaseVehicle extends Vehicle { // ------ constructor------ // public function LeaseVehicle(){ //super(); this.regNo = "1 BKD"; this.make = "Maserati"; } // end constructor // ------ properties ------ // var leaseValue:int; var leaseDate:Date; }

12 Inherited Set Definitions B.Sc. Multimedia ComputingMultimedia Authoring // ------- not required as inherited from superclass ------- // public function setRegNumber(aRegNumber:String):void { this.regNo = aRegNumber; } // end setRegNumber

13 Inherited Get Definitions B.Sc. Multimedia ComputingMultimedia Authoring // -------- not required as inherited from superclass ------ // public function getRegNumber():String { var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber

14 Subclass Instantiation and Method References B.Sc. Multimedia ComputingMultimedia Authoring var aLeaseCar:LeaseVehicle = new LeaseVehicle(); var reg:String; var make:String; // aLeaseCar.setRegNumber("BKD 2"); already set // aLeaseCar.setMake(”Maserati"); already set // via super() in Vehicle class construtor make = aLeaseCar.getMake(); reg = aLeaseCar.getRegNumber();

15 ActionScript 3.0 Implementation Details B.Sc. Multimedia ComputingMultimedia Authoring //import the class via the folder structure import uk.ac.uwe.multimedia.transport.LeaseVehicle; // 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);

16 Access Modifiers Access to an object’s properties and methods by other objects is 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 B.Sc. Multimedia ComputingMultimedia Authoring

17 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. B.Sc. Multimedia ComputingMultimedia Authoring

18 Vehicle Class getSaleValue Method public method getSaleValue():int{ var price:int; price = 12000; return price }; B.Sc. Multimedia ComputingMultimedia Authoring

19 Overriding Methods B.Sc. Multimedia ComputingMultimedia Authoring override public method getSaleValue():int{ var price:int; price = (12000 - discount); return price };

20 Summary Encapsulation - data hiding Inheritance - subclassing Polymorphism - overriding behaviors Access Modifiers - controlling access to objects and methods B.Sc. Multimedia ComputingMultimedia Authoring


Download ppt "Class Structures and Methods B.Sc. Multimedia ComputingMultimedia Authoring."

Similar presentations


Ads by Google