<<

Object-Oriented Programming

• Class is a form of abstract type • Instances of class called objects • Operators of class called methods • Applying a method to an object in the class called message passing – Messages take object as implicit argument

Instance Variables and Variables and Methods Methods • Classes have two kinds of • Instance variables: – Instance variables hold state of single methods and two kinds of object variables: – Different objects have different (copies of) instance variables –Instance methods and instance – sometimes called fields variables • Instance methods: –Class methods and class variable – Instance methods act on a single object, usually acting on instance variables

Class Variables and Methods Creating Objects • Class variables hold state that is shared • Class usually has an initialization in common among all objects of class method (usually called the same as the • Class methods act on attributes of the class, or initialize) whole class through class variables • Creates an object of class c by • Class methods can’t access instance new c(args) variables • Creates new instance variables; executes code in def of initialization • Will only use instance variables and method methods here Sending a message Self

• A message is a method is a function • Methods have formal parameters for all • Sending a message to an object is arguments accept ojbect to which it is invoking that method on that object is sent applying the function to the object • Problem: How to access that argument? • Common notation: o . m(args) • Answer: use self • Apply m to o and additional args

Example Self and Recursion class a class oddeven { var i ; { method oddeven( ) { return true } var j ; method odd(n) method a( ) {if n = 0 then return false { i := #1 ; j := i + #1 ; return j }; else self . even (n - 1) }; method f( ) { return self . a( ) }; method even(n) method g( ) { return self . f( ) }; {if n = 0 then return true method h( ) { return (i + j) } else self . odd (n - 1) } } }

Subclasses Inheritance

• Classes form a hierarchy of parent • Inheritance allows all variables classes and children classes and methods exported from • Child is a subclass of, derived class of, parent class to be part of a inherits from, extends the parent, superclass subclass • Common syntax: class c extends c’ Single Versus Multiple Inheritance Inheritance • Export information: • Single inheritance: Class may only be a subclass of one parent – public: everybody sees • Multiple inheritance: Class may be immediate – private: only seen inside class subclass of two or more parents • Problem with multiple inheritance: – protected: exported only to class A:B,C {…} subclasses What if we have both B.m and C.m? Which method is A.m? • We will use only public for methods Answer: Language dependent, usually B.m (first protected for variables extended)

Variable Hiding and Method Hiding and Super Scope • Instance variables of superclass may be • Instance method of subclass may hide hidden by declarations in subclass (or shadow) method of superclass class a { var i; var j; …} class b extends a • Access hidden method m of parent {var j; var k; method f(){…} …} class by using super m (args) class c extends b { … } • Instance variables available to methods of b are i from a and j, k from b • Will not change when method of b sent to object of c

Example Example class b extends a { var j ; var k ; class c extends b method a() { return self . b() } { method a() { return super a() } method b() method b() { return super b() } { call super a() ; j = #10 ; k = j + #1 ; method c() { i = #100 ; j = i + #1 ; k = return k } j + #1 ; return k } method g() { return method g() { return super h() } (i + k * j) } } method h() { return self . g() } } Inheritance Polymorphism and Inheritance Polymorphism Dynamic Dispatch • If a method is added to a • Method of superclass may be overridden in subclass subclass with the same name • Dynamic dispatch: when a method of (and usually signature) as in the superclass is applied to object of parent class the new version subclass, methods of subclass are used in computing result, instead of methods overrides inherited version in of superclass subclass

Virtual (or Abstract) Methods

• Virtual method: has declaration but no function body

• For subclass to be instantiated it must override all virtual methods with methods with fully concrete bodies