Overriding CMPS 2143. Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

1 Inheritance Classes and Subclasses Or Extending a Class.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object Oriented Programming
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
OOP: Inheritance By: Lamiaa Said.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Inheritance Inheritance Reserved word protected Reserved word super
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism CS351 – Programming Paradigms.
Overloading, Overriding
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
What is inheritance? It is the ability to create a new class from an existing class.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Inheritance and Polymorphism
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Coming up: Inheritance
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Object-Oriented Programming
Inheritance and Run time Polymorphism
Learning Objectives Inheritance Virtual Function.
Chapter 9: Polymorphism and Inheritance
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Overriding CMPS 2143

Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature. Also known as inclusion polymorphism important when combined with substitution 2 Parent void method (int, float Child void method (int, float AnotherChild …

Overriding 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

Overriding vs Overloading OverloadingOverriding same method name two or more method bodies classes that methods appear in do NOT have to be in parent/child relationship signatures MUST NOT match methods are SEPARATE resolved at COMPILE-TIME same method name two or more method bodies classes that methods appear in MUST be in parent/child relationship signatures MUST match methods SOMETIMES COMBINED resolved at RUN-TIME 4

Notations for Overriding Java, Smalltalk, Objective-C : override with no special keywords, only similarity of type signatures C++ : notation is in parent class virtual method in parent class indicates method MAY be overridden Object Pascal: notation is in child class C# and Delphi Pascal – keyword required in BOTH parent and child C# use virtual method in parent class and override method in child class 5

Replacement vs 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. 6

Replacement vs Refinement Both forms useful and both OFTEN found in the same programming language Almost all languages use refinement for constructors void Square::Square (int s) : Rectangle (s,s) {…} even default child constructor calls default parent constructor THIS ENSURES the data fields for both the parent and child will be properly set. 7

Replacement Two major reasons for using replacement the method is the parent class is abstract (or pure virtual), it MUST be replaced the method in the parent class is a default method, not appropriate for all situations recall Animal speak() method, and Dog speak method() earlier the child method is more efficient than the parent method parent class polygon and child class square with methods computeArea(); 8

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. public abstract class Animal // class is abstract { private String name; public Animal(String nm) { name=nm; } public String getName() // regular method { return (name); } public abstract void speak(); // abstract method - note no {} } public class Dog extends Animal { …… public void speak() { System.out.println(Dog Speaks!); } }

Reasons to use Replacement The method in the parent class is a default method, not appropriate for all situations. public class Animal { private String name; public Animal(String nm) { name=nm; } public String getName() { return (name); } public void speak() {System.out.println(Animal Speaks!);} } public class Dog extends Animal { …… public void speak() { System.out.println(Woof!); } } public class Cat extends Animal { …… public void speak() { System.out.println(Miao!); } }

Reasons to use Replacement The method in the parent can be more efficiently executed in the child. class Shape { boolean equals(Shape arg) { …} } class Triangle extends Shape{ boolean equals(Shape arg) { Triangle right = (Triangle) arg; … } class Square extends Shape{ boolean equals(Shape arg) { Square right = (Square) arg; … }

Downside of Replacement No guarantee that child class will have any meaning at all similar to the parent class. For example, a child class could redefine sqrt to compute the cube root of its argument. This goes back to the difference between subclasses and subtypes. Refinement makes this more difficult to do, since whatever the parent does is guaranteed to be part of the child. This is why most languages use refinement semantics for constructors.

Refinement Code in child class combined with code in parent class and both executed constructors perform a refinement in most languages, code from parent class executed first 13

Refinement C++ example void Parent:: example(int a) { cout << in parent code\n; } void Child::example(int a) { Parent::example(12); //do parent action cout << in child code\n; } Java example class Parent { public void example(int a) {cout << in parent code\n;} } class Child { public void example(int a) { super.example(12); //do parent action cout << in child code\n; } 14

Refinement Advocates for languages that use refinement argue mechanism makes it almost impossible to write a subclass that is not a subtype. It is possible with replacement to have a child class compute a cube root in place of a square root. Advocates for languages that use replacement argue that those errors are uncommon 15

Overriding versus Shadowing It is common in programming languages for one declaration of a variable to shadow a previous variable of the same name Shadowing is resolved at compile time class Silly { 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; //local variable shadows parameter a = a - x; }

Shadowing of Instance Variables in Java Java allows instance variables to be redefined, and uses 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.println(p.x); //12 Child c = new Child(); System.out.println(c.x); //42 p = c; // be careful here! System.out.println(p.x); //12

Shadowing Methods Many of those languages that require the virtual keyword in the parent class will use shadowing if it is omitted: class Parent { public: // note, no virtual 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 in C# class Drawable { public virtual void DrawYourself() { System.Console.WriteLine("Drawable"); } } class Square : Drawable { public override void DrawYourself() { System.Console.WriteLine("Square"); } } class Circle : Drawable { public override void DrawYourself() { System.Console.WriteLine("Circle"); } } Drawable draw= new Drawable(); draw.DrawYourself(); //--> Drawable draw= new Square(); draw.DrawYourself(); //--> Square draw= new Circle(); draw.DrawYourself(); //--> Circle

Method Shadowing in C# class Drawable { public void DrawYourself() { System.Console.WriteLine("Drawable"); } } class Square : Drawable { public new void DrawYourself() { System.Console.WriteLine("Square"); } } class Circle : Drawable { public new void DrawYourself() { System.Console.WriteLine("Circle"); } } Drawable draw= new Drawable(); draw.DrawYourself(); //--> Drawable draw= new Square(); draw.DrawYourself(); //--> Drawable draw= new Circle(); draw.DrawYourself(); //--> Drawable

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. (Example available in Chapter 15)

Chapter 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.

Study Questions pg. 332: 1-3, 8, 9, 11 Quiz next week over overloading overriding by replacement and refinement shadowing redefinition