[PDF] [PDF] Advanced Concepts in Object-Oriented Programming - Harvard SEAS

With single-inheritance and the class/type confusion, we don't get all the subtyping we want ▷ Example: Taking any object that has an m method from int to int



Previous PDF Next PDF





[PDF] OBJECT-ORIENTED PROGRAMMING (OOP) CONCEPTS WITH

Object-Oriented Programming (OOP) consist of some important concepts namely Encapsulation, Polymorphism, Inheritance and Abstraction Static variables and methods are not purely object oriented because they are not specific to instances (objects) but common to all instances



[PDF] OOP Concepts

Object Oriented Programming, OOP, is the must influential paradigm of our time operations together according to what they operate on — all the hash table operations are part The DArray in C (an old CS107 C program), for example, fails



[PDF] OOP Examples Sheet 1 - Department of Computer Science and

of students this course has two challenges: the first is understanding the core OOP concepts; the second return -1 For example lowestCommon(14,25) would be 3 Your solution whould matrix should have all elements initialised to zero



[PDF] OOP concepts Basic concepts of object oriented programming are

For example every television is a complete unit in itself even if it is attached to some other device like DVD player it remains a television There is an interface 



[PDF] INTRODUCTION OF OBJECT ORIENTED PROGRAMMING - dde gjust

Object-oriented programming is the most recent concept among programming C+ + is a superset of C Almost all c programs are also C++ programs However Let us begin with a simple example of a C++ program that prints a string on the



[PDF] Advanced Concepts in Object-Oriented Programming - Harvard SEAS

With single-inheritance and the class/type confusion, we don't get all the subtyping we want ▷ Example: Taking any object that has an m method from int to int



[PDF] Oops concepts in java with real time examples - Squarespace

If you have not yet checked it out, I would highly recommend you to read it so that you have a basic overview of all the Object Oriented Programming Concepts In 



[PDF] Basic OOP Concepts Introduction

27 mai 2015 · 1 Basic OOP Concepts Goal of OOP: Reduce complexity of software All designs are imperfect - they all involve trade-offs Common example: Stuffing another object with its data instead of letting it initialize itself from the



[PDF] Introduction to Object-Oriented Programming

OOP: Introduction 1 Introduction to OOP: Introduction 2 All objects of a specific type can receive the same messages An object is an instance of an abstract data type A decomposition splits a single concept into a number of



[PDF] Teach all OOP principles in a single solution and - Citrenz

been taught along with theoretical principles and concepts Yang et al (2015) OOP is a programming model all organized around objects rather than “actions” and class is a template or blueprint that defines the general properties and 

[PDF] all oops concepts in one program

[PDF] all oops concepts in one program to implement

[PDF] all oops concepts in one program to implement in c++

[PDF] all pearson books free pdf download

[PDF] all piano chords

[PDF] all possible nets of a cuboid

[PDF] all prepositional phrase words

[PDF] all programming languages book pdf

[PDF] all programming languages list pdf

[PDF] all programming languages pdf download

[PDF] all programming languages tutorials pdf

[PDF] all result bd 2019

[PDF] all result bd jsc

[PDF] all result bd psc

[PDF] all results ableton

CS152: Programming Languages

Lecture 23 - Advanced Concepts in

Object-Oriented Programming

Dan Grossman

Spring 2011

Sofar...

The difference between OOP and "records of functions with shared private state" isdynamic-dispatch(a.k.a.late-binding)ofself (Informally) definedmethod-lookupto implement dynamic-dispatch correctly: use run-time tags or code-pointers

Now: Subclassing vs. subtyping

Then fancy stuff: multiple-inheritance, interfaces, static overloading, multiple dispatch Next lecture: Bounded polymorphism and classless OOPDan GrossmanCS152 Spring 2011, Lecture 232

Type-Safety in OOP

Should be clearer about what type-safety means...

"Not getting stuck" has meant "don"t apply numbers", "don"t add functions", "don"t read non-existent record fields", etc.? Pure OO has only method calls (and maybe field access)

Stuck if method-lookup fails (no method matches)

Stuck if method-lookup is ambiguous (no best match) So far only failure is receiver has no method with right name/arity

Dan GrossmanCS152 Spring 2011, Lecture 23

3Revisiting Subclassing is Subtyping

Recall we have been "confusing" classes and types:Cis a class and a type and ifCextendsDthenCis a subtype ofD Therefore, ifCoverridesm,thetypeofminCmust be a subtype of the type ofminD Just like functions, method-subtyping is contravariant arguments and covariant results? If code knows it has aC, it can call methods with "more" arguments and know there are "fewer" results

Dan GrossmanCS152 Spring 2011, Lecture 234

Subtyping and Dynamic Dispatch

We defined dynamic dispatch in terms of functions takingselfas an argument

But unlike other arguments,selfiscovariant!!?

Else overriding method couldn"t access new fields/methods Sound becauseselfmust be passed, not another value with the supertype This is the key reasonencodingOO in atypedλ-calculus requires ingenuity, fancy types, and/or run-time cost ?We won"t attempt it

Dan GrossmanCS152 Spring 2011, Lecture 235

More subtyping

With single-inheritance and the class/type confusion, we don"t get all the subtyping we want Example: Taking any object that has anmmethod fromint toint Interfaces help somewhat, but class declarations must stillsaythey implement an interface Object-typesbring the flexibility of structural subtyping to OOP With object-types, "subclassingimpliessubtyping"Dan GrossmanCS152 Spring 2011, Lecture 236

More subclassing

Breaking one direction of "subclassing = subtyping" allowed more subtyping (so more code reuse) Breaking the other direction ("subclassing does not imply subtyping") allows more inheritance (so more code reuse) Simple idea: IfCextendsDand overrides a method in a way that class P1 { ... Int get_x(); Bool compare(P1); ... } class P2 extends P1 { ... Bool compare(P2); ... }

But this isnotalways correct...

Dan GrossmanCS152 Spring 2011, Lecture 237

Subclass not a subtype

class P1 {

Int x;

Int get_x(){x}

Bool compare(P1 p) { self.get_x() == p.get_x() }

class P2 extends P1 {

Int y;

Int get_y(){y}

Bool compare(P2 p) { self.get_x() == p.get_x() &&

self.get_y() == p.get_y() } overriding unlike in Java or C++)

Dan GrossmanCS152 Spring 2011, Lecture 238

Subclass not a subtype

Can still inherit implementation (need not reimplement get_x)

We cannot always do this: what ifget_xcalled

self.compare? Possible solutions:

Re-typecheckget_xin subclass

Use a "Really Fancy Type System"

I see little use in allowing subclassing that is not subtyping But I see much use in understanding that typing is about interfaces and inheritance is about code-sharing

Dan GrossmanCS152 Spring 2011, Lecture 239

Whereweare

Summary of last few slides: Separating types and classes expands the language, but clarifies the concepts: Typing is about interfaces, subtyping about broader interfaces Inheritance (a.k.a. subclassing) is about code-sharing

Combining typing and inheritance restricts both

Most OO languages purposely confuse subtyping (about type-checking) and inheritance (about code-sharing), which is reasonble in practice

Dan GrossmanCS152 Spring 2011, Lecture 2310

Multiple Inheritance

Why not allowclass C extends C1,C2,...{...}

What everyone agrees: C++ has it and Java doesn"t

All we"ll do: Understand some basic problems it introduces and how interfaces get most of the benefits and some of the problems

Problem sources:

Class hierarchy is a dag, not a tree (not true with interfaces) Subtype hierarchy is a dag, not a tree (true with interfaces)

Dan GrossmanCS152 Spring 2011, Lecture 2311

Diamond Issues

IfCextendsC1andC2andC1,C2have a common superclass

D(perhaps transitively), our class hierarchy has a diamond

IfDhas a fieldf, shouldChave one fieldfor two?

IfDhas a methodm,C1andC2will have a clash

If subsumption is coercive (changing method-lookup), how we subsume fromCtoDaffects run-time behavior (incoherent) Diamonds are common, largely because of types likeObjectwith methods likeequals

Dan GrossmanCS152 Spring 2011, Lecture 2312

Multiple Inheritance, Method-Name Clash

IfCextendsC1andC2, which both define a methodm,what doesCmean?

Possibilities:

1.Reject declaration ofC(Too restrictive with diamonds)

2.RequireCto overridem(Possibly withdirected resends)

3."Left-side" (C1) wins (Must decide if upcast to "right-side"

(C2) coerces to useC2"smor not)

4.Cgets both methods (Now upcasts definitely coercive and

with diamonds we lose coherence)

5.Other? (I"m just brainstorming based on sound principles)

Dan GrossmanCS152 Spring 2011, Lecture 2313

Implementation Issues

This isn"t an implementation course, but many semantic issues regarding multiple inheritance have been heavily influenced by clever implementations In particular, accessing members ofselfvia compile-time offsets... ... which won"t work with multiple inheritance unless upcasts "adjust" theselfpointer That"s one reason C++ has different kinds of casts Better to think semantically first (how should subsumption affect the behavior of method-lookup) and implementation-wise second (what can I optimize based on the class/type hierarchy)

Dan GrossmanCS152 Spring 2011, Lecture 2314

Digression: Casts

A "cast" can mean many things (cf. C++).

At the language level:

upcast: no run-time effect until we get to static overloading downcast: run-time failure or no-effect conversion: key question is round-tripping "reinterpret bits": not well-defined

At the implementation level:

upcast: usually no run-time effect but see last slide downcast: usually only run-time effect is failure, but... conversion: same as at language level "reinterpret bits": no effect (by definition)

Dan GrossmanCS152 Spring 2011, Lecture 2315

Least Supertypes

Considerife

1 thene 2 elsee 3 (or in C++/Java,e 1 ?e 2 :e 3

We knowe

2 ande 3 must have the same type

With subtyping, they just need a common supertype

Should pick the least (most-specific) type

Single inheritance: the closest common ancestor in the class-hierarchy tree Multiple inheritance: there may be no least common supertype

Example:C1extendsD1,D2andC2extendsD1,D2

Solutions: Reject (i.e., programmer must insert explicit casts to pick a common supertype)

Dan GrossmanCS152 Spring 2011, Lecture 2316

Multiple Inheritance Summary

Method clashes (what does inheritingmmean)

Diamond issues (coherence issues, shared (?) fields)

Implementation issues (slower method-lookup)

Least supertypes (may be ambiguous)

Complicated constructs lead to difficult language design

Doesn"t necessarily mean they are bad ideas

Now discussinterfacesand see how (and how not) multiple interfaces are simpler than multiple inheritance...

Dan GrossmanCS152 Spring 2011, Lecture 2317

Interfaces

An interface isjust a (named) (object) type.Example: interface I { Int get_x(); Bool compare(I); }

Aclasscanimplementan interface. Example:

class C implements I {

Int x;

Int get_x() {x}

Bool compare(I i) {...} // note argument type

Requiringexplicit"implements" hinders extensibility, but simplifies type-checking (a little) Basically,CimplementsIifCcould extend a class with all abstractmethods fromI

Dan GrossmanCS152 Spring 2011, Lecture 2318

Interfaces, continued

Subinterfaces (interface J extends I { ...})workexactlyas subtyping suggests they should An unnecessary addition to a language with abstract classes and multiple inheritance, but what about single inheritance and multiple interfaces: class C extends D implements I1,I2,...,In

Method clashes (no problem, inherit fromD)

Diamond issues (no problem, no implementation diamond) Implementation issues (still a "problem", different object of typeIwill have different layouts) Least supertypes (still a problem, thisisa typing issue)

Dan GrossmanCS152 Spring 2011, Lecture 2319

Using Interfaces

Although it requires more keystrokes and makes efficient implementation harder, it may make sense (be more extensible) to:

Use interface types for all fields and variables

Don"t use constructors directly: For classCimplementingI, write:

I makeI(...) { new C(...) }

This is related to "factory patterns"; constructors are behind a level of indirection It is using named object-types instead of class-based types

Dan GrossmanCS152 Spring 2011, Lecture 2320

Static Overloading

So far, we have assumed every method had a different name Same name implied overriding and required a subtype Many OO languages allow the same name for methods with different argument types:

A f(B x) { ... }

C f(D x, E y) { ... }

F f(G x, H z) { ... }

Complicates definition of method-lookup fore1.m(e2,...,en) Previously, we had dynamic-dispatch one1: method-lookup a function of theclassof the objecte1evaluates to (at run-time) We now havestatic overloading: Method-lookup isalsoa function of thetypesofe2,...,en(at compile-time)

Dan GrossmanCS152 Spring 2011, Lecture 2321

Static Overloading Continued

Because of subtyping, multiple methods can match!

"Best-match" can be roughly "Subsume fewest arguments. For a tie, allow subsumption toimmediatesupertypes and recur"

Ambiguities remain (no best match):

A f(B)vs.C f(B)(usually rejected)

e1ande2have typeB Type systems often reject ambiguous calls or usead hocrules to give a best match (e.g., "left-argument precedence")

Dan GrossmanCS152 Spring 2011, Lecture 2322

Multiple Dispatch

Static overloading saves keystrokes from shorter method-names We know the compile-time types of arguments at each call-site, so we could call methods with different names Multiple (dynamic) dispatch (a.k.a. multimethods) is much more interesting: Method-lookup a function of the run-time types of argumentsquotesdbs_dbs17.pdfusesText_23