CET203 SOFTWARE DEVELOPMENT Session 3A Abstract classes and Polymorphism.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Object-oriented Programming Concepts
Chapter 10 Classes Continued
Abstract Classes and Interfaces
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
CET203 SOFTWARE DEVELOPMENT Session 1B Modelling and the Theory of Inheritance.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Object Oriented Programming
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 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.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
One class is an extension of another.
One class is an extension of another.
Object-oriented programming
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Overview of C++ Polymorphism
CIS 199 Final Review.
Presentation transcript:

CET203 SOFTWARE DEVELOPMENT Session 3A Abstract classes and Polymorphism

Objectives Understand the concept of abstract classes Use polymorphism to handle related classes in a generalized way Understand the implications of polymorphism with overridden methods

Abstract classes Publication will never be instantiated – we will never create objects of this type The class exists to gather together the generalized features of its subclasses in one place for them to inherit. We can enforce the fact that Publication is non- instantiable by declaring it ‘abstract’: abstract class Publication { // etc.

Abstract Publication Book author OrderCopies() Magazine orderQty currIssue AdjustQty() RecvNewIssue() > Publication title price copies SellCopy() DiscMag RecvNewIssue() Abstract Class

Exercise 1 Publications2A.sln

Exercise 1 If Publication is an abstract class which of the following statements would be legal: Book book1 = new Book("Software Development“, 10.99, 3, "Liz Gandy"); DiscMag dmag1 = new DiscMag("C# programming", 2.5, 10, 10, "September"); Publication pub1 = new Publication("Programming in C#", 20.5, 1); Magazine mag1 = new Magazine("C# monthly", 2.5, 10, 10, "September");

Recap: Biological classification Kingdom(e.g. animals) Phylum(e.g. vertebrates) Class(e.g. mammal) Order(e.g. artiodactyla) Family(e.g. suidae) Genus(e.g. sus) Species(e.g. sus scrofa) Pinky the pig is a…

“is a” Within hierarchical classification of animals – Pinky is a pig (species sus scrofa) – Pinky is (also, more generally) a mammal – Pinky is (also, even more generally) a vertebrate We can specify the type of thing an organism is at different levels of detail: – higher level = less specific – lower level = more specific Pinky has a backbone, and suckles her young, and goes “oink oink”.

Exercise 2 1.How would we fit together the following in a hierarchy (using “is a” statements): – “guitar” – “acoustic guitar” – “musical instrument” – “electric guitar” – “stringed instrument” – “my guitar” [electrically amplified] 2.Draw a class diagram for this hierarchy (just show class names and relationships, no attributes or operations!) 3.Show on your class diagram which classes might be abstract?

Pigs (Three Different Ones) An object in a classification hierarchy has an ‘is a’ relationship with every class from which it is descended. If I ask you to give me a pig – you could give me Pinky – or any other pig If I ask you to give me a mammal – you could give me Perky – or any other pig – or any other mammal (e.g. any lion, or any mouse, or any human being!) If I ask you to give me an animal – you could give me Porky – or… (etc!)

Class Types & Roles Every time we define a class we create a new ‘type’ Types determine compatibility between variables, parameters etc. A subclass type is a subtype of the superclass type We can substitute a subtype wherever a ‘supertype’ is expected If the type is a superclass, any subclass can fill the role because they are each subtypes The reverse is not true!

Exercise 3 Consider the following objects: Book myBook; Magazine myMag; DiscMag myDiscMag; Fill in the gaps: If I asked you to give me a book you could give me ___ or any other object of type ____ If I asked you to give me a discMag you could give me ___ or any other object of type ____ If I ask you to give me a magazine you could give me ___ or ____ or any other object of type ____ If I asked you to give me a publication you could give me ___ or ___ or ___ or any other object of type ____ Book Publication Magazine DiscMag

Substitutability When designing class/type hierarchies, the type mechanism allows us to place a subclass object where a superclass is specified This has implications for the design of subclasses – we need to make sure they are genuinely substitutable for the superclass For example, a method should not be overridden with functionality which performs an inherently different operation

Example RecvNewIssue() in DiscMag overrides RecvNewIssue() from Magazine but does the same basic job (“fulfils the contract”) as the inherited version with respect to updating the number of copies and the current issue. However, it extends that functionality in a way specifically relevant to DiscMags by displaying a reminder to check the cover discs.

Q: What do we know about a ‘Publication’? A: It’s an object which supports (at least) the operations: – SellCopy() – ToString() A: It’s an object which has (at least) the properties: – Title – Price – Copies Inheritance guarantees that objects of any subclass of Publications provides at least these. A subclass can never remove an operation inherited from its superclass(es) – this would break the guarantee. Because subclasses extend the capabilities of their superclasses, the superclass functionality can be assumed.

Polymorphism Because an instance of a subclass is an instance of its superclass we can handle subclass objects as if they were superclass objects. Because a superclass guarantees certain operations in its subclasses we can invoke those operations without caring of which subclass the actual object is an instance. This characteristic is termed ‘polymorphism’, originally meaning ‘having multiple shapes’.

Operations If p ‘is a’ Publication, it might be a Book or a Magazine or a DiscMag. Whichever, it has a SellCopy() method. So we can invoke p.SellCopy() without worrying about what exactly p is. This can make life a lot simpler when we are manipulating objects within an inheritance hierarchy.

CashTill We want to develop a class CashTill which processes a sequence of items being sold. Without polymorphism we would need separate methods for each type of item: SellBook (Book book) SellMagazine (Magazine magazine) SellDiscMag (DiscMag discMag) With polymorphism we need only SellItem (Publication pub) An object of any of the subclasses of Publication can be passed as a Publication parameter

Publications sell themselves! Without polymorphism we would need to check for each item p so we were calling the right method to sell a copy of that subtype if p is a Book call SellCopy() method for Book else if p is a Magazine call SellCopy() method for Magazine else if p is a DiscMag call SellCopy() method for DiscMag Instead we trust p to know its own type and its own method for selling itself call p.SellCopy()

Class Diagram with CashTill Book Author : String OrderCopies(copies : int) Magazine orderQty : int currIssue : String AdjustQty(quantity : int) RecvNewIssue(newIssue : String) DiscMag RecvNewIssue(newIssue : String) CashTill runningTotal : double SellItem(pub : Publication) ShowTotal() > Publication title : String price : double copies : int SellCopy()

Implementing CashTill (1) class CashTill { // INSTANCE VARIABLES ********************* private double runningTotal; // Constructor CashTill () { runningTotal = 0; }

Implementing CashTill (2) public void SellItem (Publication pub) { String msg; runningTotal = runningTotal + pub.Price; pub.SellCopy(); msg = "Sold " + pub + “ + pub.Price + "\nSubtotal = " + runningTotal; Console.WriteLine(msg); } The version of SellCopy() called depends on which type of Publication has been passed into pub Calls appropriate class’s overridden ToString()

Implementing CashTill (3) public void ShowTotal() { Console.WriteLine("GRAND TOTAL: " + runningTotal); runningTotal = 0; }

Exercise 4 Publications2B.sln

Exercise 4 Assuming the following objects have been declared: CashTill myTill = new CashTill(); Book book1 = new Book("Software Development", 10.99, 3, "Liz Gandy"); Book book2 = new Book("Programming in C#", 20.5, 1, "Linda White"); Magazine mag1 = new Magazine("C# monthly", 2.5, 10, 10, "September"); DiscMag dmag1 = new DiscMag("C# programming", 2.5, 10, 10, "September"); Write the code statements to sell one copy of each item through the CashTill and then show the overall total

Summary Polymorphism allows us to refer to objects according to a superclass rather than their actual class. Inheritance guarantees that operations available for the superclass are available in its subclasses. We can manipulate objects by invoking operations defined for the superclass without worrying about which subclass is involved in any specific case. C# ensures that the appropriate method for the actual class of the object is invoked at run-time.