• No results found

Object-Oriented Approaches to Programming

N/A
N/A
Protected

Academic year: 2022

Share "Object-Oriented Approaches to Programming"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Object-Oriented Approaches to Programming

e Overloading, Masking, Overriding E Didier Verna

EPITA / LRDE

[email protected]

lrde/~didier @didierverna didier.verna in/didierverna

(2)

Outline

Static Polymorphism Overloading Masking

Dynamic Polymorphism Overriding

Overloading, Masking, Overriding Abstract Methods

Corollary: Java Interfaces (Sub)class / (Sub)Type Relation

Reminder

Covariance / Contravariance

Liskov Substitution Principle

(3)

Plan

Static Polymorphism Overloading Masking

Dynamic Polymorphism

(Sub)class / (Sub)Type Relation

(4)

Overloading

I Same name, different signature I Type / number of parameters I Possibly, return type

I Constructors, methods I including static methods I functions, operators I Usage:

I do slightly different things I do something in slightly different

ways

I Intra-class polymorphism (ad-hoc) I Advantage: static dispatch

compilation, performance

Example

Point -x : double -y : double

«create» +Point ()

«create» +Point (x : double, y : double) +translate (x : double) : void +translate (x : double, y : double) : void

Shape -center : Point

«create» +Shape (p : Point)

«create» +Shape (x : double, y : double)

(5)

Overloading

I Same name, different signature I Type / number of parameters I Possibly, return type

I Constructors, methods I including static methods I functions, operators I Usage:

I do slightly different things I do something in slightly different

ways

I Intra-class polymorphism (ad-hoc) I Advantage: static dispatch

compilation, performance

Example

Point -x : double -y : double

«create» +Point ()

«create» +Point (x : double, y : double) +translate (x : double) : void +translate (x : double, y : double) : void

Shape -center : Point

«create» +Shape (p : Point)

«create» +Shape (x : double, y : double)

C++

class Point { public:

// Constructor overloading

Point () : Point (0, 0) {};

Point (double x, double y) : x_ (x), y_ (y) {};

// Method overloading

void translate (double x) { x_ += x; };

void translate (double x, double y) { x_ += x; y_ += y; };

private:

double x_, y_;

};

(6)

Overloading

I Same name, different signature I Type / number of parameters I Possibly, return type

I Constructors, methods I including static methods I functions, operators I Usage:

I do slightly different things I do something in slightly different

ways

I Intra-class polymorphism (ad-hoc) I Advantage: static dispatch

compilation, performance

Example

Point -x : double -y : double

«create» +Point ()

«create» +Point (x : double, y : double) +translate (x : double) : void +translate (x : double, y : double) : void

Shape -center : Point

«create» +Shape (p : Point)

«create» +Shape (x : double, y : double)

Java

public class Point

{ // Constructor overloading

public Point () { this (0, 0); }

public Point (double _x, double _y) { x = _x; y = _y; } // Method overloading

public void translate (double _x) { x += _x; }

public void translate (double _x, double _y) { x += _x; y += _y; } private double x, y;

}

(7)

Masking

I Inter-class polymorphism I Usage: identical to overloading I Same name, signature different or

identical

I Class = distinction factor I Including static methods I Advantage: static dispatch

compilation, performance I Inconvenient: static dispatch…

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void

Employee -company : string -salary : unsigned ////////// +details//// ()//:////// void

+hello (details : bool) : void

+hello () : void

(8)

Masking

I Inter-class polymorphism I Usage: identical to overloading I Same name, signature different or

identical

I Class = distinction factor I Including static methods I Advantage: static dispatch

compilation, performance I Inconvenient: static dispatch…

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void

Employee -company : string -salary : unsigned ////////// +details//// ()//:////// void

+hello (details : bool) : void +hello () : void

C++

void Human::hello () const

{ std::cout << "Hello! I'm " << name_ << ", "

<< size_ << "m, "

<< age () << "yo.\n";

}

void Employee::hello (bool details) const { Human::hello ();

if (details)

std::cout << "Working at " << company_ << " for " << salary_ << "€, "

<< "started at the age of " << hiring_age () << ".\n";

}

void Employee::hello () const { hello (true); }

(9)

Masking

I Inter-class polymorphism I Usage: identical to overloading I Same name, signature different or

identical

I Class = distinction factor I Including static methods I Advantage: static dispatch

compilation, performance I Inconvenient: static dispatch…

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void

Employee -company : string -salary : unsigned ////////// +details//// ()//:////// void

+hello (details : bool) : void +hello () : void

C++

auto h = Human { ... };

h.hello (); // Human::hello auto e = Employee { ... };

e.hello (); // Employee::hello

Human* incognito = new Employee (...);

incognito->hello (); // Human::hello ! const Human& dont_know = unpredictable ();

dont_know.hello (); // Human::hello !

(10)

Plan

Static Polymorphism Dynamic Polymorphism

Overriding

Overloading, Masking, Overriding Abstract Methods

Corollary: Java Interfaces

(Sub)class / (Sub)Type Relation

(11)

Overriding

I “We also needed to group together common process properties in such a way that they could be applied later, in a variety of different situations not necessarily known in advance.”

[Dahl, 1978]

I Intra-hierarchy polymorphism (inclusion)

I Usage: Cf. overloading / masking I Same name, same signature I Advantage: dynamic dispatch I Inconvenient: performance cost I “Virtual” methods

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void {virtual}

Employee

-company : string

-salary : unsigned

+hello () : void {virtual}

(12)

Overriding

I “We also needed to group together common process properties in such a way that they could be applied later, in a variety of different situations not necessarily known in advance.”

[Dahl, 1978]

I Intra-hierarchy polymorphism (inclusion)

I Usage: Cf. overloading / masking I Same name, same signature I Advantage: dynamic dispatch I Inconvenient: performance cost I “Virtual” methods

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void {virtual}

Employee -company : string -salary : unsigned +hello () : void {virtual}

C++

class Human { public:

virtual void hello () const { ... };

};

class Employee : public Human { public:

void hello (bool details) const { ... };

void hello () const override { ... };

};

(13)

Overriding

I “We also needed to group together common process properties in such a way that they could be applied later, in a variety of different situations not necessarily known in advance.”

[Dahl, 1978]

I Intra-hierarchy polymorphism (inclusion)

I Usage: Cf. overloading / masking I Same name, same signature I Advantage: dynamic dispatch I Inconvenient: performance cost I “Virtual” methods

Example

Human -name : string -size : float

-birth_year : unsigned +hello () : void {virtual}

Employee -company : string -salary : unsigned +hello () : void {virtual}

Java

public class Human { public void hello ()

{ System.out.println ("Hello! I'm " + name + ", " + size + "m, "

+ age () + "yo.");

} }

public class Employee extends Human { public void hello (boolean details)

{ super.hello ();

if (details)

System.out.println ("Working at " + company + " for " + salary + "€, "

+ "started at the age of " + hiringAge () + ".");

}

@Override public void hello () { hello (true); }

(14)

Overloading, Masking, Overriding

I Propagation of overloading:

langage-dependent I Java: yes

I C++: non by default integral masking, Cf. using I Independent from the static or

virtual method status

I Pay attention to type conversions!

I Masking 6= overriding I Static vs. dynamic I Java: static methods only

same signature

I C++: static and non-virtual methods identical or different signatures

Example

Shape -center : Point

+translate (x : double, y : double) : void

Circle +translate (p : Point) : void +translate (x : int, y : int) : void

(15)

Overloading, Masking, Overriding

I Propagation of overloading:

langage-dependent I Java: yes

I C++: non by default integral masking, Cf. using I Independent from the static or

virtual method status

I Pay attention to type conversions!

I Masking 6= overriding I Static vs. dynamic I Java: static methods only

same signature

I C++: static and non-virtual methods identical or different signatures

Example

Shape -center : Point

+translate (x : double, y : double) : void

Circle +translate (p : Point) : void +translate (x : int, y : int) : void

C++

class Shape { public:

void translate (double x) { center_.translate (x); };

void translate (double x, double y) { center_.translate (x, y); };

};

class Circle : public Shape { public:

using Shape::translate;

void translate (Point p) { center_ = p; }

};

(16)

Overloading, Masking, Overriding

I Propagation of overloading:

langage-dependent I Java: yes

I C++: non by default integral masking, Cf. using I Independent from the static or

virtual method status

I Pay attention to type conversions!

I Masking 6= overriding I Static vs. dynamic I Java: static methods only

same signature

I C++: static and non-virtual methods identical or different signatures

Example

Shape -center : Point

+translate (x : double, y : double) : void

Circle +translate (p : Point) : void +translate (x : int, y : int) : void

Java

public class Shape

{ public void translate (double x) { center.translate (x); } public void translate (double x, double y) { center.translate (x, y); } }

public class Circle extends Shape

{ public void translate (Point p) { center = p; }

}

(17)

Abstract Methods

Example

ShapeShape -center : Point

+translate (x : double, y : double) : void +scale (a : double) : void

Rectangle +scale (a : double) : void Circle

-radius : double +scale (a : double) : void

Triangle +scale (a : double) : void

Abstract method:

Italics

I Methods declared, but without initial implementation a.k.a. “purely virtual”

I Implementation required in sub-classes

I Abstract Method(s) =⇒ Abstract Class

(18)

Abstract Methods

Example

ShapeShape -center : Point

+translate (x : double, y : double) : void +scale (a : double) : void

Rectangle +scale (a : double) : void Circle

-radius : double +scale (a : double) : void

Triangle +scale (a : double) : void

Abstract method:

Italics

I Methods declared, but without initial implementation a.k.a. “purely virtual”

I Implementation required in sub-classes I Abstract Method(s) =⇒ Abstract Class

C++

class Shape { public:

virtual void scale (double factor) = 0;

};

class Circle : public Shape { public:

void scale (double factor) override { radius_ *= factor; };

private:

double radius_;

};

(19)

Abstract Methods

Example

ShapeShape -center : Point

+translate (x : double, y : double) : void +scale (a : double) : void

Rectangle +scale (a : double) : void Circle

-radius : double +scale (a : double) : void

Triangle +scale (a : double) : void

Abstract method:

Italics

I Methods declared, but without initial implementation a.k.a. “purely virtual”

I Implementation required in sub-classes I Abstract Method(s) =⇒ Abstract Class

Java

public abstract class Shape

{ public abstract void scale (double factor);

}

public class Circle extends Shape { @Override

public void scale (double factor) { radius *= factor; };

private double radius;

}

(20)

Corollary: Java Interfaces

I Constants and abstract methods only

I Everything is public (abstract, public, and final keywords are optional)

I A class implements one or several interfaces I An interface extends one or several interfaces I Since Java 8

I Static Methods not inheritable (implementation required) I Default Methods inheritable (idem)

I Depuis Java 9

I Private Methods not inheritable, static or not

(21)

Application

Example

Animal -weight: int +weight (): int +cry (): void

«interface»

Oviparous +nurse (): void

«interface»

Mammal +nurse (): void

Bird +cry (): void

Cat +cry (): void Platypus

+nurse (): void +cry (): void Interfaces

Labelled Classes =

Implementation 6=

Inheritance

(22)

Application

Example

Animal -weight: int +weight (): int +cry (): void

«interface»

Oviparous +nurse (): void

«interface»

Mammal +nurse (): void

Bird +cry (): void

Cat +cry (): void Platypus

+nurse (): void +cry (): void Interfaces

Labelled Classes =

Implementation 6=

Inheritance

Java

public interface Nurse

{ public abstract void nurse ();

} public interface Oviparous extends Nurse { default void nurse ()

{ System.out.println ("I brood my eggs.");

} }

public interface Mammal extends Nurse { default void nurse ()

{ System.out.println ("I suckle my offsprings.");

} }

(23)

Application

Example

Animal -weight: int +weight (): int +cry (): void

«interface»

Oviparous +nurse (): void

«interface»

Mammal +nurse (): void

Bird +cry (): void

Cat +cry (): void Platypus

+nurse (): void +cry (): void Interfaces

Labelled Classes =

Implementation 6=

Inheritance

Java

public class Bird extends Animal implements Oviparous { @Override

public void cry () { System.out.println ("Tweet! Tweet!"); } }

public class Cat extends Animal implements Mammal { @Override

public void cry () { System.out.println ("Meow! Meow!"); }

}

(24)

Application

Example

Animal -weight: int +weight (): int +cry (): void

«interface»

Oviparous +nurse (): void

«interface»

Mammal +nurse (): void

Bird +cry (): void

Cat +cry (): void Platypus

+nurse (): void +cry (): void Interfaces

Labelled Classes =

Implementation 6=

Inheritance

Java

public final class Platypus extends Animal implements Oviparous, Mammal

{ @Override

public void cry () { System.out.println ("Platty! Platty!"); } // Required!

@Override public void nurse () { // Default method calls

Oviparous.super.nurse ();

Mammal.super.nurse ();

} }

(25)

Plan

Static Polymorphism Dynamic Polymorphism

(Sub)class / (Sub)Type Relation Reminder

Covariance / Contravariance

Liskov Substitution Principle

(26)

Reminder on the Concept of Inheritance

I “is a” relationship

I An object of a subclass can be seen as an object of a superclass I Ambivalence

I Implementation inheritance

I Interface inheritance (consequence of implementation inheritance) I Substitutability Principle

I If S <: T, then every term of type S may be use safely in a context in which a term of type T is expected

I For some definition of “safely” and “context”…

I Strong relation between inheritance (subclassing) et subtyping

(27)

Application to Methods

Example

Driver +license () : License +drive (vehicle : Car) : void +vehicle () : Car

+provide (parking : Parking): void

TruckDriver

+provide (parking : TruckParking) : void +vehicle () : Vehicle

+drive (vehicle : Vehicle): void +license () : TruckLicense License

TruckLicense Parking

TruckParking

Vehicle

Car Truck

Covariance Contravariance

(28)

Liskov Substitution Principle

I Substitutability Principle applied to objects I Strong behavioral subtyping

Let φ(x) be a provable property on every object x of type T.

Then, φ(y) must hold for every object y of type S such that S <: T.

I Covariance of return types in S I Contravariance of argument types in S

I The exceptions raised in S must be subtypes of those raised in T I The invariants of T must be preserved in S

I Pre-conditions cannot be stronger in S I Post-conditions cannot be weaker in S

I Historical constraint: mutation can only occur through an interface. No method

in S must allow mutations that are prohibited in T.

(29)

Plan

Bibliography

(30)

Bibliography

Ole-Johan Dahl and Kristen Nygaard.

The Development of the SIMULA Languages.

History of Programming Languages Conference, 1978.

Barbara Liskov.

Data Abstraction and Hierarchy.

OOPSLA’87 Keynote, 1988.

References

Related documents

In the current study, we examined the effect of oral administration of CNN on severity of colitis, inflammatory state in the colon, colonic histology, goblet cell number,

In this paper we focus on approaches which aim at discovering communities of people in Opportunistic Networks. We first study the behaviour of three community detection

'Megalopolis', he wrote, breeds 'anonymity' and 'impersonality' which act as 'a positive encour­ agement to asocial or anti-social actions' ( 1938: 266). 255) - newspapers and

Additionally, the ≥5 MW WTGs used in modern offshore farms require nearly 1 km of inter-turbine spacing[17], and 10 MW WTGs currently in development [15] may require &gt;1 km

Supply Chain Managers and Finance Managers Need to Partner. Dynamic Discounting

complete your browser providers charge premium plan license, could not a reference to be added sugars that worked.. If snow was not configured, margin,

One point more than 3.00 sigmas from center line... One point more than 3.00 sigmas from

Fields or local variables in Java are of one of two sorts Primitive type: variable stores the value. declaration implies memory allocation to store its value (depending on