Upload
adil-raja
View
97
Download
2
Embed Size (px)
Citation preview
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
OVERRIDING
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 21, 2015
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
OUTLINE I
INTRODUCTION
OVERRIDING NOTATIONS
REPLACEMENT AND REFINEMENT
SHADOWING
COVARIANCE AND CONTRAVARIANCE
SUMMARY
REFERENCES
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
INTRODUCTION
I A method in a child class overrides a method in the parentclass if it has the name name and type signature.
I In this chapter we will investigate some of the issues thatarise from the use of overriding.
I Difference from Overloading Notations.I Replacement and Refinement.I Shadowing.I Covariance and Contravariance.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
DIFFERENCE FROM OVERLOADING
I Like overloading, there are two distinct methods with thesame name. But there are differences:
I Overriding only occurs in the context of the parent/childrelationship.
I The type signatures must match.I Overridden methods are sometimes combined together.I Overriding is resolved at run-time, not at compile time.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
OVERRIDEN RELATIONALS
I Child classes need only override one method (for example<) to get effect of all relationals.
I Overridden in class Integer to mean integer less than.I Overridden in class Char to be ascii ordering sequence.I Overridden in class String to mean lexicalgraphic ordering.I Overridden in class Point to mean lower-left quadrant.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
NOTATING OVERRIDING
I In some languages (Smalltalk, Java) overriding occursautomatically when a child class redefines a method withthe same name and type signature.
I In some languages (C++) overriding will only occur if theparent class has declared the method in some special way(example, keyword virtual).
I In some languages (Object Pascal) overriding will onlyoccur if the child class declares the method in somespecial way (example, keyword override).
I In some languages (C#, Delphi) overriding will only occur ifboth the parent and the child class declare the method insome special way.
OVERRIDING
class Parent { / / C# examplepublic v i r t u a l i n t example ( i n t a ) { . . . }
}class Chi ld : Parent {
public ove r r i de i n t example ( i n t a ) { . . . }}
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
REPLACEMENT AND REFINEMENT
I There are actually two different ways that overriding can behandled:
I A replacement totally and completely replaces the code inthe parent class the code in the child class.
I A refinement executes the code in the parent class, andadds to it the code in the child class.
I Most languages use both types of semantics in differentsituations.
I Constructors, for example, almost always use refinement.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
REASONS TO USE REPLACEMENT
I There are a number of reasons to use replacement ofmethods.
I The method in the parent class is abstract, it must bereplaced.
I The method in the parent class is a default method, notappropriate for all situations.
I The method in the parent can be more efficiently executedin the child.
I We will give examples of the latter two.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
PRINT ANCHORS
I Here is an example, printing html anchor tags.
PRINT ANCHORS
class Anchor {public void pr in tAnchor ( ) {
p r i n t ( ’ <A h re f =" h t t p : ’ ) ;i nne r ;p r i n t ( ’ "> ’ ) ;
}}
If we create an instance and class printAnchor, the output we expect will be produced.
Anchor a = new Anchor ( ) ;a . p r in tAnchor ( ) ;
<A h re f = " h t t p : ">
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
MAKING CHILD CLASSES
We can create child classes to any level:
CHILD CLASSES
class OSUAnchor extends Anchor {public void pr in tAnchor ( ) {
p r i n t ( ’ / /www. cs . o r s t . edu / ’ ) ;i nne r ;
}}
class BuddAnchor extends OSUAnchor {public void pr in tAnchor ( ) {
p r i n t ( ’ ~budd / ’ ) ;i nne r ;
}}
Anchor anAnchor = new BuddAnchor ( ) ;anAchor . p r in tAnchor ( ) ;
<A h re f = h t t p : " / /www. cs . o r s t . edu /~ budd / ">
Trace carefully the flow of control, and see how it differs fromreplacement.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
SIMULATING REFINEMENT WITH REPLACEMENT
I In most languages the most important features of arefinement can be simulated, even if the language usesreplacement.
SIMULATING REFINEMENT
void Parent : : example ( i n t a ) {cout << " i n parent code \ n " ;
}
void Chi ld : : example ( i n t a ) {Parent : : example ( 1 2 ) ; / / do parent codecout << " i n c h i l d code \ n " ; / / then c h i l d code
}
Note that this is not quite the same as Beta, as here the childwraps around the parent, while in Beta the parent wraps aroundthe child.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
CONSTRUCTORS USE REFINEMENT
I In most languages that have constructors, a constructorwill always use refinement.
I This guarantees that whatever initialization the parentclass performs will always be included as part of theinitialization of the child class.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
OVERRIDING VS SHADOWING
I It is common in programming languages for onedeclaration of a variable to shadow a previous variable ofthe same name:
AN EXAMPLE
class S i l l y {private i n t x ; / / an ins tance v a r i a b l e named x
public void example ( i n t x ) { / / x shadows ins tance v a r i a b l ei n t a = x +1;while ( a > 3) {
i n t x = 1; / / l o c a l v a r i a b l e shadows parametera = a − x ;
}}
}
Shadowing can be resolved at compile time, does not requireany run-time search.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
SHADOWING OF INSTANCE VARIABLES IN JAVA
I Java allows instance variables to be redefined, and usesshadowing.
SHADOWING
class Parent {public i n t x = 12;
}
class Chi ld extend Parent {public i n t x = 42; / / shadows v a r i a b l e from parent c lass
}
Parent p = new Parent ( ) ;System . out . p r i n t l n ( p . x ) ;
12Ch i ld c = new Chi ld ( ) ;System . out . p r i n t l n ( c . x ) ;
42p = c ; / / be c a r e f u l here !System . out . p r i n t l n ( p . x ) ;
12
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
SHADOWING METHODS
I Many of those languages that require the virtual keyword inthe parent class will use shadowing if it is omitted:
SHADOWING METHODS
class Parent {public : / / note , no v i r t u a l keyword here
void example ( ) { cout << " Parent " << endl ; }} ;
class Chi ld : public Parent {public :
void example ( ) { cout << " Ch i ld " << endl ; }} ;
Parent ∗ p = new Parent ( ) ;p−>example ( )
ParentCh i ld ∗ c = new Chi ld ( ) ;c−>example ( )
Ch i ldp = c ; / / be c a r e f u l here !p−>example ( )
Parent
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
OVERRIDING, SHADOWING AND REDEFINITION
Overriding The type signatures are the same in both parent and child classes,and the method is declared as virtual in the parent class.
Shadowing The type signatures are the same in both parent and child classes,but the method was not declared as virtual in the parent class.
Redefinition The type signature in the child class differsfrom that given in the parent class.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
COVARIANCE AND CONTRAVARIANCE I
I Frequently it seems like it would be nice if when a methodis overridden we could change the argument types orreturn types.
I A change that moves down the inheritance hierarchy,making it more specific, is said to be covariant.
I A change that moves up the inheritance hierarchy is said tobe contravariant.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
COVARIANCE AND CONTRAVARIANCE II
COVARIANCE
class Parent {void t e s t ( covar : Mammal, con t ravar : Mammal) : boolean
}
class Chi ld extends Parent {void t e s t ( covar : Cat , con t ravar : Animal ) : boolean
}
While appealing, this idea runs into trouble with the principle of substitution.
Parent aValue = new Chi ld ( ) ;aValue . t e x t (new Dog ( ) , new Mammal ( ) ) ; / / i s t h i s l e g a l ??
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
CONTRAVARIANT RETURN TYPES
I To see how a contravariant change can get you intotrouble, consider changing the return types:
CONTRAVARIANCE
class Parent {Mammal t e s t ( ) {
return new Cat ( ) ;}
}
class Chi ld extends Parent {Animal t e s t ( ) {
return new Bi rd ( ) ;}
}
Parent aParent = new Chi ld ( ) ;Mammal r e s u l t = aValue . t e s t ( ) ; / / i s t h i s l e g a l ?
Most languages subscribe to the novariance rule: no change intype signatures.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
A SAFE VARIANCE CHANGE
I C++ allows the following type of change in signature:
VARIANCE CHANGE
class Parent {public :
Parent ∗ clone ( ) { return new Parent ( ) ; }} ;
class Chi ld : public Parent {public :
Ch i ld ∗ clone ( ) { return new Chi ld ( ) ; }} ;
No type errors can result from this change.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
SUMMARY
I An override occurs when a method in the child classesuses the same name and type signature as a method inthe parent class.
I Unlike overloading, overriding is resolved at run-time.I There are two possible means for an overriding,
replacement and refinement.I A name can shadow another name.I Some languages permit both shadowing and overriding.I Shadowing is resolved at compile time.I A change in the type signature can be covariant or
contravariant, if it moves down or up the type hierarchy.I The semantics of both types of change can be subtle.
Overriding Roaming Researchers, Inc. cbna
Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary References
REFERENCES
I Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.
I An Introduction to Object Oriented Programming, TimothyBudd.
I This presentation is developed using Beamer:I Szeged, beaver.
Overriding Roaming Researchers, Inc. cbna