180 likes | 312 Views
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
E N D
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 • 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#
Overview • Non pure object oriented programming languages • provide support for object oriented programming • not exclusively object-oriented • C++, Ada 95, CLOS
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
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());
Complex z, w; … z = new Complex (1, 2); w = new Complex (-1, 1); z = z.add(w); z = z.add(w).multiply(z);
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
furniture chair table • Relationship amongst the components • Use of inheritance • Sublasses versus subparts desk lounge chair sofa dining table
closed figure polygon ellipse • Relationship amongst the components • Use of inheritance • Sublasses versus subparts triangle rectangle circle square
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
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)
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() {… } }
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
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()
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
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
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