1 / 75

Object Oriented Languages

Object Oriented Languages. Ed Sykes ed.sykes@sheridanc.on.ca. Objectives: Introduction/Overview Class Fundamentals classes / instances slots and methods Inheritance interpretation of inheritance code sharing single inheritance multiple inheritance

winda
Télécharger la présentation

Object Oriented Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Languages Ed Sykes ed.sykes@sheridanc.on.ca Objectives: Introduction/Overview Class Fundamentals classes / instances slots and methods Inheritance interpretation of inheritance code sharing single inheritance multiple inheritance alternatives to multiple inheritance

  2. Introduction / Overview 2 main approaches to OO programming languages: class-based prototype-based

  3. Introduction / Overview Class-based OO programming languages: A class is a template for the instantiation of objects: instance == object classes are static source code representations objects are dynamic entities which live during program execution. Objects are distinct entities.

  4. Introduction / Overview Objects exhibit 3 main properties: encapsulation inheritance polymorphism

  5. Introduction / Overview Encapsulation hiding how things are done within the object this includes: information hiding method hiding

  6. Introduction / Overview Encapsulation eg., ATM: properties: name/address/phone/account_type balance functionality: withdraw() deposit() get_balance()

  7. Introduction / Overview Inheritance “is a kind of” relation

  8. Introduction / Overview Polymorphism: “many” “forms” same general interface, different implementation: 2 types of polymorphism: compile-time polymorphism run-time polymorphism (dynamic binding)

  9. Introduction / Overview Polymorphism: run-time polymorphism (dynamic binding) closely related to inheritance e.g., load_vehicle(), drive_vehicle()

  10. Object Oriented Languages Objectives: Introduction/Overview Class Fundamentals classes / instances slots and methods Inheritance interpretation of inheritance code sharing single inheritance multiple inheritance alternatives to multiple inheritance

  11. Class Fundamentals Objectives: class/instance difference classes as abstract data types information hiding (encapsulation) internal structure of classes

  12. Class Fundamentals Classes are central to OO programming languages mechanism for defining sets of objects + operations to manipulate them Term “class” can be interpreted as: a set of objects a program structure or module a factor-like entity which creates objects a data type

  13. Class Fundamentals “class” as a set of objects: A class defines properties or operations that are common to a collection of objects. Objects are manipulated by programs and exist at runtime only.

  14. Class Fundamentals 2) “class” as a program structure or module: - modules encapsulate types, data (variables), and operations. modules export some entities (operations) modules hide their internal details Objects are instances of modules data operations Outside world

  15. Class Fundamentals 3) “class” as a factory-like entity: Emphasizes dynamic nature of OO languages class is a device that can create objects opens the way to describe objects in a parametric fashion  Dynamic unique way for instantiating objects object class data creates operations

  16. Class Fundamentals 4) “class” as a data type: classes in many OO languages are considered to be types operations defined over that type are part of the types definition e.g., given a class ‘C’, and a variable v. v : C; // variable ‘v’ is of type ‘C’

  17. Class Fundamentals public class Customer{ private String _name; private String _phone; ... public void setName(String Name) public String getName() ... Class (definition) Instances: Can be bound, pointed to, and passed in and out of procedures  first-class citizens Bobby Boogie (905) 234-2341 Instances (runtime entities) Sue MacDonald (416) 234-7642

  18. Class Fundamentals Typically, the instance creation process allows arguments: e.g., constructors (default and overloaded): public Customer(){ System.out.println("In default constructor"); } public Customer(String name, String phone) { this(); _name = name; _phone = phone; } public Customer(String name, String phone, double discount){ this(name, phone); this._discount = discount; }

  19. Class Fundamentals class slots Slots and Methods: 2 types: data slots procedural slots (methods) method method

  20. Class Fundamentals Slot access: Data slots represent variable or constant information 2 modes: Read-only (e.g., constant) Read-write mode (e.g., salary slot)

  21. Class Fundamentals class Externally visible slots Slot visibility Slots visible only within class

  22. Class Fundamentals Slot visibility: C++ and Java: public private protected applies to data slots and method slots public: - can be referenced, accessed and updated anywhere in the program private: - is only visible within the class in which it is defined. protected: - can be referenced, accessed and updated within the class and within all of the subclasses

  23. Class Fundamentals Slot visibility: Slots may be allocated in 2 different ways: the slot is allocated in each instance of the class (default) the slot is allocated once and is shared by all instances of a class (class variables or static variables in C++, Smalltalk)

  24. Class Fundamentals Example: public Customer(){ private static double minimum_labour = 55; // $55 per hour private String _name; private String _phone; public static void increment_labour_price(double increase){ minimum_labour += increase; // $60.50 now. } } Invoke by using class name or instance: Customer.increment_labour_price(5.50);

  25. Object Oriented Languages Objectives: Introduction/Overview Class Fundamentals classes / instances slots and methods Inheritance interpretation of inheritance code sharing single inheritance multiple inheritance alternatives to multiple inheritance

  26. Inheritance The definition of a new class can rely upon the existence of another definition. Core concept behind OO reusability Supports run-time polymorphism 3 views of inheritance: Inheritance is a method for code sharing Logical relationship between classes (specialization/generalization) Type-based account (enables subtypes to be produced given a definition of a supertype)

  27. Definitions: superclass: both the immediate superclass and all ancestors of a given class subclass: converse of superclass direct subclass: Van is a direct subclass of Truck indirect subclass: Van is an indirect subclass of Vehicle Inheritance superclass subclass

  28. C1: superclass Inheritance s11 s12 m11 m12 C2: subclass m13 s21 s22 s23 m21 m22

  29. Instantiated object of type C2 would appear like: s11 Inheritance s12 s21 s22 s23 m11 m12 m13 m21 m22

  30. For single inheritance there is 1 root class from which subclasses are derived. e.g., an inheritance graph Inheritance root C1 C2 B C3 D E F

  31. From C3, a class sees only a linear sequence of classes between it and the root class e.g., an inheritance chain Inheritance root C1 C2 C3

  32. Inheritance example: Car extends Vehicle: in Car,  inherited slots (instance variables) methods from the Vehicle class (potentially overriden)

  33. Inheritance public class Car extends Vehicle { ... } public class Sports_Car extends Car { ... } Each class inherits ALL variables, and methods from parent class.

  34. Inheritance public class Vehicle { private String _vin; private String _make; private String _model; private int _year; private long _odometer; } public class Car extends Vehicle { private float _max_speed; private int _num_of_passengers; private int _horse_power; private float _engine_size; private String _air_cond; }

  35. Inheritance super() used for calling parent class constructors 1st line in subclass constructor public Sports_Car() { super(); } public Sports_Car(String owner, String vin, String make, String model, int year, long odo) { super(owner,vin,make,model,year,odo); } What method is called here? what method is called here?

  36. Inheritance super.method() Within a subclass' overriden method, invoke parent's method. public class Building { ... public void drawBuilding() { ... } // implementation for Building public void calcArea() { ... } } public class House extends Building { ... public void calcArea() { super.calcArea(); // invoke Building’s calcArea() method ... } }

  37. Inheritance Adding specific methods to the subclass public class Building { private int _rooms; private int _floors; private int _area; public void setRooms(int num) { ... } public int getRooms() { ... } public void setFloors(int num) { ... } } public class House extends Building { private int _bedrooms; private int _baths; public void setBedrooms(int num) { ... } public int getBedrooms() { ... } public void setBaths(int num) { ... } }

  38. Abstract Classes serves as a place holder in an inheritance graph defines those properties and behaviours required by many other classes Note: no objects can be created of this class type. It is merely used as a foundation for subclasses. 2) But you can create references to this class. This is the principle way we achieve run-time polymorphism.

  39. Abstract Classes public abstract class Convert { protected double _init; // initial value protected double _conv; // converted value public Convert(double i) { _init = i; } public double getconv() { return _conv; } public double getinit() { return _init; } public abstract void compute(); // abstract method Abstract Method: implementation stub part of an abstract class must be defined in a concrete subclass

  40. Abstract Classes class l_to_g extends Convert { public l_to_g(double i) { super(i); } public void compute() { // definition of abstract method _conv = _init / 3.7854; } public String toString() { return (this.getinit() + " litres is " + this.getconv() “ gallons” ); } }

  41. // Fahrenheit to Celsius class f_to_c extends Convert { public f_to_c(double i) { super(i); } public void compute() { // definition of abstract method _conv = (_init-32) / 1.8; } public String toString() { return (this.getinit() + " Celsius is " + this.getconv() ); } } Abstract Classes

  42. Abstract Classes public static void main(String[] args) { Convert p; // pointer to base class l_to_g lgob = new l_to_g(5); // litres to gallons object p = lgob; p.compute(); System.out.println(p); f_to_c fcob = new f_to_c(82); p = fcob; p.compute(); System.out.println(p); } } would Convert p = new Convert(); work? why?

  43. Abstract Class Eg. in C++ class number { protected: int val: public: void setval(int i) {val = i;} virtual void show() = 0; // declare show() to be a pure virtual function }; class hextype : public number { public: void show() {cout << hex << val <<“\n”; } }; class dectype : public number { public: void show() {cout << val <<“\n”; } }; class octtype : public number { public: void show() {cout << oct << val <<“\n”; } }; main() { dectype d; hextype h; octtype o; d.setval(20); d.show(); // Output: 20 - decimal h.setval(20); h.show(); // Output: 14 - hexidecimal o.setval(20); o.show(); // Output: 24 - octal }

  44. Inheritance as Subtyping One problem with inheritance is that it should be hidden from its subtypes (its clients) In other words: the way in which a class is derived should not matter to users of that class. The effects of inheritance can be felt when redefining subclasses

  45. Inheritance as Subtyping Subtyping: consists of the rules by which objects of one type (class) are determined to be acceptable in contexts that expect another type (class) (e.g., downcasting) is important since the rules determine the legality of programs should be based on the behaviour of objects

  46. Inheritance as Subtyping Y Subtyping: if instances of class X meet the external interface of class Y then X should be a subtype of Y. X

  47. Inheritance as Subtyping The concept of a type hierarchy can be represented by abstract classes (or interfaces) behavioural subtyping cannot be deduced without formal semantic specification of behaviour. without these rules, subtypes can only be deduced on the basis of external interfaces of a syntactic nature (e.g., method signatures)

  48. Inheritance as Subtyping The programmer should be able to specify that the class is not a subtype of a parent or that the class is a subtype of an unrelated class (i.e., not its parent) The 1st case comes about when the behaviour of the objects is incompatible with the interface of parent objects. The 2nd case arises when the class supports the external interface of another class without sharing its implementation

  49. Multiple Inheritance Y Searching is required since slots and methods may not be defined locally C F Natural extension of single inheritance corresponds to the application domain in a natural fashion Inheritance structure is a lattice or Directed Acyclic Graph: B E H What are the paths from X to Y? D A G X

  50. Multiple Inheritance Y C F problems with multiple inheritance: how to search the inheritance lattice? replication of inherited slots (“common roots” problem) meaning of the program can be different depending on search algorithm B E H D A G X

More Related