SlideShare a Scribd company logo
1 of 22
Download to read offline
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming ConceptsKwangshin Oh
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.netBhumivaghasiya
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHPVineet Kumar Saini
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritanceadil raja
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPshubham ghimire
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentationManeesha Caldera
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 

What's hot (20)

Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
RichControl in Asp.net
RichControl in Asp.netRichControl in Asp.net
RichControl in Asp.net
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Templates
TemplatesTemplates
Templates
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 

Viewers also liked

Implications of Substitution
Implications of SubstitutionImplications of Substitution
Implications of Substitutionadil raja
 
Implementation
ImplementationImplementation
Implementationadil raja
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuseadil raja
 
Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnectionsadil raja
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initializationadil raja
 
The Polymorphic Variable
The Polymorphic VariableThe Polymorphic Variable
The Polymorphic Variableadil raja
 
Container Classes
Container ClassesContainer Classes
Container Classesadil raja
 
Distributed Computing
Distributed ComputingDistributed Computing
Distributed Computingadil raja
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Designadil raja
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methodsadil raja
 
Thinking Object-Oriented
Thinking Object-OrientedThinking Object-Oriented
Thinking Object-Orientedadil raja
 
Static and Dynamic Behavior
Static and Dynamic BehaviorStatic and Dynamic Behavior
Static and Dynamic Behavioradil raja
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitutionadil raja
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypesadil raja
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspectionadil raja
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swingadil raja
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworksadil raja
 

Viewers also liked (20)

Implications of Substitution
Implications of SubstitutionImplications of Substitution
Implications of Substitution
 
Overloading
OverloadingOverloading
Overloading
 
Implementation
ImplementationImplementation
Implementation
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuse
 
The STL
The STLThe STL
The STL
 
Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnections
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initialization
 
The Polymorphic Variable
The Polymorphic VariableThe Polymorphic Variable
The Polymorphic Variable
 
Container Classes
Container ClassesContainer Classes
Container Classes
 
Distributed Computing
Distributed ComputingDistributed Computing
Distributed Computing
 
Generics
GenericsGenerics
Generics
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Design
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methods
 
Thinking Object-Oriented
Thinking Object-OrientedThinking Object-Oriented
Thinking Object-Oriented
 
Static and Dynamic Behavior
Static and Dynamic BehaviorStatic and Dynamic Behavior
Static and Dynamic Behavior
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitution
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypes
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworks
 

Similar to Overriding

2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)quantumiq448
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Abou Bakr Ashraf
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)Jyoti shukla
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsAjax Experience 2009
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibilitymachuga
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
The Art of Refactoring | Asmit Ghimire | Gurzu.pdf
The Art of Refactoring | Asmit Ghimire | Gurzu.pdfThe Art of Refactoring | Asmit Ghimire | Gurzu.pdf
The Art of Refactoring | Asmit Ghimire | Gurzu.pdfGurzuInc
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7dplunkett
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVMAndres Almiray
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxAtikur Rahman
 

Similar to Overriding (20)

2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
11slide
11slide11slide
11slide
 
JavaYDL11
JavaYDL11JavaYDL11
JavaYDL11
 
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
Java PRESENTATION(PACKAGES,CLASSES,VARIABLES,FLOW CONTROL,EXCEPTION)
 
Visula C# Programming Lecture 7
Visula C# Programming Lecture 7Visula C# Programming Lecture 7
Visula C# Programming Lecture 7
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Java
JavaJava
Java
 
java vs C#
java vs C#java vs C#
java vs C#
 
The Art of Refactoring | Asmit Ghimire | Gurzu.pdf
The Art of Refactoring | Asmit Ghimire | Gurzu.pdfThe Art of Refactoring | Asmit Ghimire | Gurzu.pdf
The Art of Refactoring | Asmit Ghimire | Gurzu.pdf
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
c# at f#
c# at f#c# at f#
c# at f#
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
Java
JavaJava
Java
 

More from adil raja

A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specificationadil raja
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehiclesadil raja
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystifiedadil raja
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)adil raja
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Researchadil raja
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocoladil raja
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Socketsadil raja
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Executionadil raja
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistanadil raja
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousingadil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...adil raja
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPadil raja
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specificationsadil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...adil raja
 

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
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