Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Overriding
1. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
OVERRIDING
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 21, 2015
Overriding Roaming Researchers, Inc. cbna
2. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
OUTLINE I
INTRODUCTION
OVERRIDING NOTATIONS
REPLACEMENT AND REFINEMENT
SHADOWING
COVARIANCE AND CONTRAVARIANCE
SUMMARY
REFERENCES
Overriding Roaming Researchers, Inc. cbna
3. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
INTRODUCTION
A method in a child class overrides a method in the parent
class if it has the name name and type signature.
In this chapter we will investigate some of the issues that
arise from the use of overriding.
Difference from Overloading Notations.
Replacement and Refinement.
Shadowing.
Covariance and Contravariance.
Overriding Roaming Researchers, Inc. cbna
4. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
DIFFERENCE FROM OVERLOADING
Like overloading, there are two distinct methods with the
same name. But there are differences:
Overriding only occurs in the context of the parent/child
relationship.
The type signatures must match.
Overridden methods are sometimes combined together.
Overriding is resolved at run-time, not at compile time.
Overriding Roaming Researchers, Inc. cbna
5. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
OVERRIDEN RELATIONALS
Child classes need only override one method (for example
<) to get effect of all relationals.
Overridden in class Integer to mean integer less than.
Overridden in class Char to be ascii ordering sequence.
Overridden in class String to mean lexicalgraphic ordering.
Overridden in class Point to mean lower-left quadrant.
Overriding Roaming Researchers, Inc. cbna
6. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
NOTATING OVERRIDING
In some languages (Smalltalk, Java) overriding occurs
automatically when a child class redefines a method with
the same name and type signature.
In some languages (C++) overriding will only occur if the
parent class has declared the method in some special way
(example, keyword virtual).
In some languages (Object Pascal) overriding will only
occur if the child class declares the method in some
special way (example, keyword override).
In some languages (C#, Delphi) overriding will only occur if
both the parent and the child class declare the method in
some special way.
OVERRIDING
class Parent { / / C# example
public virtual int example ( int a ) { . . . }
}Overriding Roaming Researchers, Inc. cbna
7. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
REPLACEMENT AND REFINEMENT
There are actually two different ways that overriding can be
handled:
A replacement totally and completely replaces the code in
the parent class the code in the child class.
A refinement executes the code in the parent class, and
adds to it the code in the child class.
Most languages use both types of semantics in different
situations.
Constructors, for example, almost always use refinement.
Overriding Roaming Researchers, Inc. cbna
8. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
REASONS TO USE REPLACEMENT
There are a number of reasons to use replacement of
methods.
The method in the parent class is abstract, it must be
replaced.
The method in the parent class is a default method, not
appropriate for all situations.
The method in the parent can be more efficiently executed
in the child.
We will give examples of the latter two.
Overriding Roaming Researchers, Inc. cbna
9. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
PRINT ANCHORS
Here is an example, printing html anchor tags.
PRINT ANCHORS
class Anchor {
public void printAnchor ( ) {
p r i n t ( ’<A href =" http : ’ ) ;
inner ;
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 . printAnchor ( ) ;
<A href=" http : ">
Overriding Roaming Researchers, Inc. cbna
10. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
MAKING CHILD CLASSES
We can create child classes to any level:
CHILD CLASSES
class OSUAnchor extends Anchor {
public void printAnchor ( ) {
p r i n t ( ’ / /www. cs . orst . edu / ’ ) ;
inner ;
}
}
class BuddAnchor extends OSUAnchor {
public void printAnchor ( ) {
p r i n t ( ’~budd / ’ ) ;
inner ;
}
}
Anchor anAnchor = new BuddAnchor ( ) ;
anAchor . printAnchor ( ) ;
<A href= http : " / /www. cs . orst . edu/~budd / ">
Trace carefully the flow of control, and see how it differs from
replacement.
Overriding Roaming Researchers, Inc. cbna
11. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
SIMULATING REFINEMENT WITH REPLACEMENT
In most languages the most important features of a
refinement can be simulated, even if the language uses
replacement.
SIMULATING REFINEMENT
void Parent : : example ( int a ) {
cout << " in parent code n" ;
}
void Child : : example ( int a ) {
Parent : : example ( 1 2 ) ; / / do parent code
cout << " in 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 child
wraps around the parent, while in Beta the parent wraps around
the child.
Overriding Roaming Researchers, Inc. cbna
12. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
CONSTRUCTORS USE REFINEMENT
In most languages that have constructors, a constructor
will always use refinement.
This guarantees that whatever initialization the parent
class performs will always be included as part of the
initialization of the child class.
Overriding Roaming Researchers, Inc. cbna
13. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
OVERRIDING VS SHADOWING
It is common in programming languages for one
declaration of a variable to shadow a previous variable of
the same name:
AN EXAMPLE
class S i l l y {
private int x ; / / an instance variable named x
public void example ( int x ) { / / x shadows instance variable
int a = x+1;
while ( a > 3) {
int x = 1; / / l o c a l variable shadows parameter
a = a − x ;
}
}
}
Shadowing can be resolved at compile time, does not require
any run-time search.
Overriding Roaming Researchers, Inc. cbna
14. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
SHADOWING OF INSTANCE VARIABLES IN JAVA
Java allows instance variables to be redefined, and uses
shadowing.
SHADOWING
class Parent {
public int x = 12;
}
class Child extend Parent {
public int x = 42; / / shadows variable from parent class
}
Parent p = new Parent ( ) ;
System . out . p r i n t l n ( p . x ) ;
12
Child c = new Child ( ) ;
System . out . p r i n t l n ( c . x ) ;
42
p = c ; / / be careful here !
System . out . p r i n t l n ( p . x ) ;
12
Overriding Roaming Researchers, Inc. cbna
15. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
SHADOWING METHODS
Many of those languages that require the virtual keyword in
the 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 Child : public Parent {
public :
void example ( ) { cout << " Child " << endl ; }
} ;
Parent ∗ p = new Parent ( ) ;
p−>example ( )
Parent
Child ∗ c = new Child ( ) ;
c−>example ( )
Child
p = c ; / / be careful here !
p−>example ( )
Parent
Overriding Roaming Researchers, Inc. cbna
16. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
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 differs
from that given in the parent class.
Overriding Roaming Researchers, Inc. cbna
17. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
COVARIANCE AND CONTRAVARIANCE I
Frequently it seems like it would be nice if when a method
is overridden we could change the argument types or
return types.
A change that moves down the inheritance hierarchy,
making it more specific, is said to be covariant.
A change that moves up the inheritance hierarchy is said to
be contravariant.
Overriding Roaming Researchers, Inc. cbna
18. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
COVARIANCE AND CONTRAVARIANCE II
COVARIANCE
class Parent {
void t e s t ( covar : Mammal, contravar : Mammal) : boolean
}
class Child extends Parent {
void t e s t ( covar : Cat , contravar : Animal ) : boolean
}
While appealing, this idea runs into trouble with the principle of substitution.
Parent aValue = new Child ( ) ;
aValue . t e x t (new Dog ( ) , new Mammal ( ) ) ; / / i s t h i s legal ??
Overriding Roaming Researchers, Inc. cbna
19. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
CONTRAVARIANT RETURN TYPES
To see how a contravariant change can get you into
trouble, consider changing the return types:
CONTRAVARIANCE
class Parent {
Mammal t e s t ( ) {
return new Cat ( ) ;
}
}
class Child extends Parent {
Animal t e s t ( ) {
return new Bird ( ) ;
}
}
Parent aParent = new Child ( ) ;
Mammal r e s u l t = aValue . t e s t ( ) ; / / i s t h i s legal ?
Most languages subscribe to the novariance rule: no change in
type signatures.
Overriding Roaming Researchers, Inc. cbna
20. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
A SAFE VARIANCE CHANGE
C++ allows the following type of change in signature:
VARIANCE CHANGE
class Parent {
public :
Parent ∗ clone ( ) { return new Parent ( ) ; }
} ;
class Child : public Parent {
public :
Child ∗ clone ( ) { return new Child ( ) ; }
} ;
No type errors can result from this change.
Overriding Roaming Researchers, Inc. cbna
21. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
SUMMARY
An override occurs when a method in the child classes
uses the same name and type signature as a method in
the parent class.
Unlike overloading, overriding is resolved at run-time.
There are two possible means for an overriding,
replacement and refinement.
A name can shadow another name.
Some languages permit both shadowing and overriding.
Shadowing is resolved at compile time.
A change in the type signature can be covariant or
contravariant, if it moves down or up the type hierarchy.
The semantics of both types of change can be subtle.
Overriding Roaming Researchers, Inc. cbna
22. Introduction Overriding Notations Replacement and Refinement Shadowing Covariance and Contravariance Summary
REFERENCES
Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
An Introduction to Object Oriented Programming, Timothy
Budd.
This presentation is developed using Beamer:
Szeged, beaver.
Overriding Roaming Researchers, Inc. cbna