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

What's hot (20)

JDBC
JDBCJDBC
JDBC
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++Abstraction in c++ and Real Life Example of Abstraction in C++
Abstraction in c++ and Real Life Example of Abstraction in C++
 
Clean code
Clean codeClean code
Clean code
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Web Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NETWeb Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NET
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 

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
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritanceadil raja
 

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#
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 

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

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 

Recently uploaded (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 

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