oops object oriented programming. based on adt principles representation of type and operations in...

25
OOPs Object oriented programming

Upload: bruno-beasley

Post on 11-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

OOPs

Object oriented programming

Page 2: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Based on ADT principles

Representation of type and operations in a single unit

Available for other units to create variables of the type

Possibility of hiding data structure of type and implementation of operations

Page 3: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Limitations of ADT -->OOP extensions

Software reuse: ADTs too rigid--> inheritance

No structure to collections of ADTs--> inheritance

OOP provides class hierarchies

Page 4: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

The three* hierarchies of OOP Inheritance:

1 - n

Instantiation1 - n

Composition1 - n

superclass

subclass

class

instance

object

instance variable

* Not including interfaces

Page 5: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Inheritance

Superclass – subclass Subclass inherits all methods, variables Subclass can add methods, variables Subclass can overwrite all methods, variables-> increased reusability-> structure collections of classes

Page 6: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Access to variables and methods

ADTs – public (global scope) or private (class scope)

Objects - public or private-plus protected (class plus subclasses)?

Design decision: info hiding vs efficiency Java adds package scope

Page 7: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Scope and objects - java

looking for a variable while executing a method:

block, local, instance, class

extensions:inner class, package, inheritancepublic, protected, private

Page 8: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

superclass w

class z

instance x

Scope & objects in java

block t

class Nest extends Bird{ int x=10; static int z=0; void doIt(int q) { int r = 100; { int t = r+x+z+q+w; } }}

class Nest extends Bird{ int x=10; static int z=0; void doIt(int q) { int r = 100; { int t = r+x+z+q+w; } }}

looking for a variable while executing a method:

1.block, 2.local, 3.instance,4.class,5.superclass

w in superclass, either protected or public, may be static

method r,q

Page 9: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Scope and objects - java

extensions:inner class - static scopingprotected, private, public(looking inward) packages access to classes, methods, public variables importing

Page 10: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Inheritance rules

Single inheritance (Smalltalk) – subclass has only one superclass; classes form an inheritance tree

Multiple inheritance (C++) – subclass can have multiple superclasses

Single inheritance plus interfaces (Java)

Page 11: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Need for multiple inheritance ‘orthogonal’ properties – the ‘diamond’ problem

Vehicle

GMVehicle KiaVehicle

GMCar GMSUV KiaCar KiaSUV

Vehicle

Car SUV

GMCar GMSUVKiaCar KiaSUV

Car, SUV properties repeated

Manufacturer properties repeated

Page 12: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Using multiple inheritance

Vehicle

KiaVehicleGMVehicle Car SUV

GMCar GMSUV KiaCar KiaSUV

Page 13: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Problems of multiple inheritance

Identifier conflict:

Class Super1

int x, y

Class Super2

double x, z

Class Sub extends Super1, Super2

Which x?

Page 14: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Interfaces solve the multiple inheritance problem

No variables in interfaces No method implementations in

interfaces – only protocolsBUT Weaker inheritance – no gain in code

reuse

Page 15: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Type checking

none (Smalltalk typeless) message compatibility

subclasses are subtypes* type compatibility – object of subtype

can be assigned to identifier declared of superclass type

dynamic type checking provides polymorphism

Page 16: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Polymorphism in method calls

class Super

method x()

class Sub1

method x()

class Sub3

method x()

class Sub2

Super a,b,c,d;

a = new Super();

b = new Sub1();

c = new Sub2();

d = new Sub3();

a.x();

b.x();

c.x();

d.x();

Super a,b,c,d;

a = new Super();

b = new Sub1();

c = new Sub2();

d = new Sub3();

a.x();

b.x();

c.x();

d.x();

Page 17: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Polymorphism means dynamic type checking

class of object must be determined at run time methods bound to messages dynamically parameters type checked dynamically

BUT protocol can be verified statically-> purpose of abstract methods

Page 18: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Forced static binding

if a method cannot be overridden, it can be statically bound C++ assumes methods cannot be

overridden -> statically bound – virtual keyword causes dynamic binding

Java (and Objective C) assumes methods can be overridden -> dynamic binding – final keyword allows static binding

Page 19: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Implementing dynamic method invocation

Superclass method table (VMT)

-methA

-methB

Superclass method table (VMT)

-methA

-methB

Subclass method table (VMT)

-methA

-methC

Ref to parent VMT

Subclass method table (VMT)

-methA

-methC

Ref to parent VMT

Superclass var1;

var1 = new Subclass();

var1.methA();

var1.methB();

var1.methC(); // compile error

var1 Instance vars;Ref to class VMTInstance vars;

Ref to class VMT

Page 20: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Allocation and de-allocation

What kinds of object memory allocation? Static Stack-dynamic Heap-dynamic

Heap reclamation Programmer control Garbage collection

C++ Smalltalk,Java

X

X

X X

X

X

Page 21: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

C++

Access: Classes private, public Members private, public, protected

Extending class can change access Override access to members in parent Valuable for ‘narrowing’ access polymorphism problem

List

Stack QueueRemove some access

Page 22: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

C++

Inheritance – partial – no single hierarchy

Efficient but complex

Page 23: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Java

Closer to pure object language than C++ No functions Dynamic by default One hierarchy of classes

BUT Imperative statements Primitive data types are not objects

Page 24: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Smalltalk - pure OOP

every statement is a message with an object recipient and method invocation

Page 25: OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create

Another ‘object’ model

Property lists / attribute-value pairs E.g., Lisp symbols: property list E.g., Javascript objects: hash table Properties are typeless – data and

methods Set of properties is dynamically varying Ideally suited to tabulation – visual

programming Are they objects? ADTs? Variant records?