1 / 18

COMP313A Programming Languages

COMP313A Programming Languages. Object Oriented Progamming Languages (1). Lecture Outline. Overview Polymorphism Inheritance and the Type System Polymorphism and Strong Typing. Overview of Object Oriented Programming paradigm. Pure object oriented programming languages

Download Presentation

COMP313A Programming 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. COMP313A Programming Languages Object Oriented Progamming Languages (1)

  2. Lecture Outline • Overview • Polymorphism • Inheritance and the Type System • Polymorphism and Strong Typing

  3. Overview of Object Oriented Programming paradigm • Pure object oriented programming languages • unit of modularity is an Abstract Data Type (ADT) implementation, especially the class • Can define new classes of objects by modifying existing classes • Java, Smalltalk, Eiffel, Dylan, C#

  4. Overview • Non pure object oriented programming languages • provide support for object oriented programming • not exclusively object-oriented • C++, Ada 95, CLOS

  5. Overview • Pure terminology • objects are instances of classes • object has instance variables and methods • local variables declared as part of the object • operations which are used to modify the object • message passing • smalltalk s push 5 • polymorphic – can’t tell which method to invoke until run-time • Eiffel, C++, Java restrict polymorphism • static type checking

  6. public Class Complex { public Complex() { re = 0; im = 0; } public Complex (double realpart, double imagpart) { re = realpart; im = imagpart } public double realpart() { return re; } public double imaginarypart() { return im; } public Complex add ( Complex c ) { return new Complex(re + c.realpart(), im + c.imaginarypart()); public Complex multiply (Complex c) { return new Complex(re * c.realpart() – im * c.imaginary part(), re * c.imaginarypart() + im * c.realpart());

  7. Complex z, w; … z = new Complex (1, 2); w = new Complex (-1, 1); z = z.add(w); z = z.add(w).multiply(z);

  8. Inheritance • The subtype principal • a “derived” class inherits all the operations and instance variables of its base class • derived class, sub class etc • base class, super class, parent class etc • Single inheritance versus Multiple inheritance

  9. furniture chair table • Relationship amongst the components • Use of inheritance • Sublasses versus subparts desk lounge chair sofa dining table

  10. closed figure polygon ellipse • Relationship amongst the components • Use of inheritance • Sublasses versus subparts triangle rectangle circle square

  11. Overview • Two main goals of object-oriented language paradigm are: • restricting access to the internal (implementation) details of data types and their operations • modifiability for reuse

  12. The Notion of Software Reuse and Independence • A corollary of Data Abstraction • Given a particular Abstract Data Type • Extension of data and /or operations (specialisation - subclasses) • Redefinition of one or more of the operations • Abstraction, or the collection of similar operations from two different components into a new component (multiple inheritance) • Extension of the type that operations apply to (polymorphism)

  13. public class Queue { // constructors and instance variables go here public void enqueue (int x) {…} public void dequeue() {…} public int front () {…} public boolean empty() {…} } public class Deque extends Queue {// constructors and instance variables go here public void addFront ( int x {… } public void deleteRear() {… } }

  14. Queue q; Deque d; d = new Deque(); q = d; q.dequeue() and d.queue() OK q.deleteRear() q = d; //a compile time error in Java q = (Deque) d; // downcast

  15. class stack{ public: void push(int, item){elements[top++] = item;}; int pop () {return elements[--top];}; private: int elements[100]; int top =0; }; class counting_stack: public stack { public: int size(); // return number of elements on stack stack s1, s2; // automatic variables stack* sp = new stack; sp->pop()

  16. stack* sp = new stack; counting_stack* csp = new counting_stack; sp = csp; // okay csp = sp; // statically can’t tell Why shouldn’t csp be allowed to point to an sp object? C++ strong type system

  17. Polymorphism • polymorphic variables could refer to objects of different classes • what is the problem for a type checker • How do we allow dynamic binding and still ensure type safety • strong type system limits polymorphism • restricted to objects of a class or its derived classes • e.g. variables of type stack may refer to a variable of type counting_stack • Strict object-oriented languages (Smalltalk, Eiffel, Java • all objects accessed through references which may be polymorphic • C++ - pointers, reference variables and by-reference parameters are polymorphic

  18. If we do not use pointers we do not get inclusion polymorphism. But… stack s; counting_stack cs; s = cs; //okay coerce cs to a stack cs = s; //not okay

More Related