Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP313A Programming Languages Object Oriented Progamming Languages (2)

Similar presentations


Presentation on theme: "1 COMP313A Programming Languages Object Oriented Progamming Languages (2)"— Presentation transcript:

1 1 COMP313A Programming Languages Object Oriented Progamming Languages (2)

2 2 Lecture Outline The Class Hierarchy and Data Abstraction Polymorphism Polymorphism and Strong Typing

3 3 furniture chairtable lounge chairsofa dining table desk

4 4 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() {… } } Is Queue more abstract than Deque or vice versa

5 5 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()

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

7 7 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

8 8 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

9 9 The Type System The subtype principle a week day is also a day –is-a relationship similarly class and sub-class –counting_stack is-a stack but…. type day = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); weekday= (Monday..Friday);

10 10 The Type System need to state the conditions under which the isa relationship holds for subclasses of classes because… subclasses can hide the variables and functions or modify them in an incompatible way need to know when they are equivalent with the parent’s definition


Download ppt "1 COMP313A Programming Languages Object Oriented Progamming Languages (2)"

Similar presentations


Ads by Google