Understanding Polymorphism

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance, Polymorphism, and Virtual Functions
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Lecture 9 Polymorphism Richard Gesick.
Programming Pillars Introduction to Object- Oriented Programming.
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
Object Oriented Software Development
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.
Abstraction ADTs, Information Hiding and Encapsulation.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter -6 Polymorphism
Introduction to Object-Oriented Programming Lesson 2.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Polymorphism
Object-Oriented Programming Concepts
Classes and Inheritance
Understand the Fundamentals of Classes
Inheritance and Polymorphism
Inheritance AKEEL AHMED.
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Module 4: Implementing Object-Oriented Programming Techniques in C#
Object-Oriented Programming: Inheritance
Inheritance Basics Programming with Inheritance
Abstract Classes AKEEL AHMED.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Inheritance.
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Review of Previous Lesson
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Object-Oriented Programming: Inheritance
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Understanding Polymorphism The syntax and examples in this lesson are specific to C#.

Module Learning Outcomes Understand the models and methodologies used in software development Identify and explain the most appropriate software solution to meet a project specification, including user requirements, constraints and limitations of available resources Demonstrate the use of teamwork and software to communicate ideas and progression of agreed tasks within a commercial setting Design a prototype software solution to a given specification. Develop and test a prototype software solution to a given specification, using own programming skills. Report on how the artefact can be improved and maintained by other software developers.

Lesson Overview When you see code on the slides give it a try!! Students will understand polymorphism. In this lesson, you will learn about: Extending the functionality in a class after inheriting from a base class Overriding methods in a derived class When you see code on the slides give it a try!!

Guiding Questions How can the function of a class be changed after inheriting from a base class? How are methods overridden? Use the guiding questions as a class starter, allowing the students time to answer the questions in their journals. Discuss student answers to the questions.

Activator—Predict the output public class A { public virtual void say(){ Console.WriteLine(“A”); } } public class B : A { public override void say(){ Console.WriteLine(“B”); } public class C { public static void main() { A b = new B(); b.say(); }

Review Terms base—a keyword is used to access members of the base class from within a derived class. new—when used as a modifier, this keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. override—a modifier required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Review Terms (continued) polymorphism—the ability to redefine a method in a derived class (a class that inherited its data structures and methods from another class). A class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. sealed—cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. virtual—a keyword used to modify a method or property declaration, in which case the method or the property is called a virtual member. — The virtual member allows its implementation to be replaced within derived classes. Polymorphism allows the programmer to define a base class that includes routines that perform standard operations on groups of related objects, without regard to the exact type of each object. The programmer then redefines the routines in the derived class for each type, taking into account the characteristics of the object.

Polymorphism Refers to the ability to redefine methods in a derived class and use a class as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. Is important not only to the derived classes, but to the base classes as well. Using the base class could be the same as using an object of the derived class that has been cast to the base class type. When a derived class inherits from a base class, it gains all the methods, fields, properties, and events of the base class. To change the data and behavior of a base class, you have two choices: You can replace the base member with a new derived member, or you can override a virtual base member.

Using the new Keyword Replacing a member of a base class with a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property in a derived class.

Using the new Keyword (continued) The new keyword is placed before the return type of a class member that is being replaced: public class BaseClass { public int WorkField; public void DoWork() { } } public class DerivedClass : BaseClass { public new int WorkField; public new void DoWork() { } }

Using the new keyword (continued) When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class. DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method BaseClass A = (BaseClass) B; A.DoWork(); // Calls the old method

Using the virtual and override Keywords For an instance of a derived class to completely take over a class member from a base class, the base class has to declare that member as virtual. A derived class then uses the override keyword, instead of new, to replace the base class implementation with its own. public class BaseClass { public int WorkField; public virtual void DoWork() { } } public class DerivedClass : BaseClass { public int WorkField; public override void DoWork() { } }

Using the virtual and override Keywords (continued) Fields cannot be virtual; only methods, properties, events, and indexers can be virtual. When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class. DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method BaseClass A = (BaseClass) B; A.DoWork(); // Also calls the new method

Virtual members remain virtual If class A declares a virtual member, class B derives from A, and class C derives from B, class C inherits the virtual member and has the option to override it, regardless of whether class B declared an override for that member. public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } } public class C : B { public override void DoWork() { } }

Using the sealed Keyword A derived class can stop virtual inheritance by declaring an override as sealed In the code samples below, the method DoWork is no longer virtual to any class derived from C. It is still virtual for instances of C, even if they are cast to type B or type A. public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } } public class C : B { public sealed override void DoWork() { } }

Using the sealed Keyword (continued) Sealed methods can be replaced by derived classes using the new keyword. If DoWork is called on D using a variable of type D, the new DoWork is called. If a variable of type C, B, or A is used to access an instance of D, a call to DoWork will follow the rules of virtual inheritance, routing those calls to the implementation of DoWork on class C. public class C : B { public sealed override void DoWork() { } } public class D : C { public new void DoWork() { } }

Lesson Review Describe situations in which the following keywords are used. Provide code to support your answer. virtual override base new sealed

References Kendall, K.E. and Kendall, J.E. (2011) Systems Analysis and Design. 8. ed., global ed. Harlow: Pearson Education Microsoft (2015) C# Programming Guide [online] available from <https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx> [27 June 2016] Robertson, L.A., Course Technology: Cengage Learning, and Cengage Learning (2003) Simple Program Design: A Step-by-Step Approach. Australia: Course Technology : Cengage Learning Watson, K. (ed.) (2013) Beginning Visual C# 2012 Programming. Wrox beginning guides. Indianapolis, IN: John Wiley & Sons Wiley (2012) Software Development Fundamentals: Exam 98-361. Hoboken, NJ: John Wiley & Sons Inc