OOPs - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

OOPs

Description:

Design decision: info hiding vs efficiency. Java adds package ... Car, SUV properties repeated. Manufacturer properties repeated. Using multiple inheritance ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 26
Provided by: laurentian3
Category:
Tags: oops

less

Transcript and Presenter's Notes

Title: OOPs


1
OOPs
  • Object oriented programming

2
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

3
Limitations of ADT --gtOOP extensions
  • Software reuse ADTs too rigid
  • --gt inheritance
  • No structure to collections of ADTs
  • --gt inheritance
  • OOP provides class hierarchies

4
The three hierarchies of OOP
  • Inheritance
  • 1 - n
  • Instantiation
  • 1 - n
  • Composition
  • 1 - n

superclass
subclass
class
instance
object
instance variable
Not including interfaces
5
Inheritance
  • Superclass subclass
  • Subclass inherits all methods, variables
  • Subclass can add methods, variables
  • Subclass can overwrite all methods, variables
  • -gt increased reusability
  • -gt structure collections of classes

6
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

7
Scope and objects - java
  • looking for a variable while executing a method
  • block, local, instance, class
  • extensions
  • inner class, package, inheritance
  • public, protected, private

8
Scope objects in java
  • looking for a variable while executing a method
  • block,
  • local,
  • instance,
  • class,
  • superclass

class Nest extends Bird int x10 static
int z0 void doIt(int q) int r 100
int t rxzqw
superclass w
class z
instance x
method r,q
block t
w in superclass, either protected or public, may
be static
9
Scope and objects - java
  • extensions
  • inner class - static scoping
  • protected, private, public(looking inward)
  • packages
  • access to classes, methods, public variables
  • importing

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

11
Need for multiple inheritance
  • orthogonal properties the diamond problem

Vehicle
GMVehicle
KiaVehicle
Car, SUV properties repeated Manufacturer
properties repeated
GMCar
GMSUV
KiaCar
KiaSUV
Vehicle
Car
SUV
GMCar
GMSUV
KiaCar
KiaSUV
12
Using multiple inheritance
Vehicle
KiaVehicle
GMVehicle
Car
SUV
GMCar
GMSUV
KiaCar
KiaSUV
13
Problems of multiple inheritance
  • Identifier conflict

Class Super1 int x, y
Class Super2 double x, z
Class Sub extends Super1, Super2
Which x?
14
Interfaces solve the multiple inheritance problem
  • No variables in interfaces
  • No method implementations in interfaces only
    protocols
  • BUT
  • Weaker inheritance no gain in code reuse

15
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

16
Polymorphism in method calls
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()
class Super method x()
class Sub1 method x()
class Sub3 method x()
class Sub2
17
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
  • -gt purpose of abstract methods

18
Forced static binding
  • if a method cannot be overridden, it can be
    statically bound
  • C assumes methods cannot be overridden -gt
    statically bound
  • virtual keyword causes dynamic binding
  • Java (and Objective C) assumes methods can be
    overridden -gt dynamic binding
  • final keyword allows static binding

19
Implementing dynamic method invocation
  • Superclass method table (VMT)
  • methA
  • methB

Superclass var1 var1 new Subclass() var1.methA
() var1.methB() var1.methC() // compile error
  • Subclass method table (VMT)
  • methA
  • methC
  • Ref to parent VMT

Instance vars Ref to class VMT
var1
20
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
21
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
Remove some access
Stack
Queue
22
C
  • Inheritance partial no single hierarchy
  • Efficient but complex

23
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

24
Smalltalk - pure OOP
  • every statement is a message with an object
    recipient and method invocation

25
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?
Write a Comment
User Comments (0)
About PowerShow.com