Inheritance and Virtual Functions 11-26-2001. Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class?

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Reusable Classes.  Motivation: Write less code!
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
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.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
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.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? Don’t let problems hang.
MIPS Machine Assembly Language Opening Discussion zWhat did we talk about last class? zHave you seen anything interesting in the news? zDo you.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
OOP: Encapsulation &Abstraction
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Inherited Classes in Java
Programming II Polymorphism A.AlOsaimi.
Presentation transcript:

Inheritance and Virtual Functions

Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class?

Two Roles of Inheritance zInheritance plays two roles in programming languages like C++. We have discussed these over the last two classes. ySubtyping - When one type inherits from another it becomes a subtype of it. yReuse - Methods defined for a superclass can be called for instances of a subclass without being rewritten.

When Not to Reuse zAt the end of last class I asked you if you always want to reuse code from a superclass when you inherit from it. As you can now guess from the title of this slide, the answer is no. zSometimes subclasses need to do the same type of thing, but they need to do different specific actions. That is, they need methods with the same names but different implementations.

Virtual Functions zWhen you want different subclasses to have different implementations of a method, that method needs to be declared virtual. zThis is done simply by putting the virtual keyword before the return type. virtual int decideMove(); virtual void calcValue();

Virtual vs. Non-virtual zWhen a function takes as an argument a reference or pointer to a supertype when a non-virtual function is called it always calls the method of the supertype, even if the subtype overloads it. zWith a virtual function it calls the “closest” declared method. zWe aren’t going to discuss the issue of scoping non-virtual functions.

Once Virtual, Always Virtual zOnce a function has been declared virtual in a superclass it is virtual in all the subclasses in C++. As a result, the subclasses don’t have to explicitly say it is virtual. zHowever, I would recommend that if a function is virtual you call it such everywhere so that people don’t have to look to superclasses to find out.

Pure Virtual zA virtual function declaration in a class can be made pure virtual which implies that the method has no implementation of the superclass. Classes with outstanding pure virtual functions can’t be instantiated. virtual int decideMove()=0; virtual void calcValue()=0;

Look at Code zLet’s now go add a virtual function to the Pac-Man code that we started working on in the last class. zLast class we put in a function that had the entity move one step in the direction that it was set to move in. They also need to decide what direction to move in at a given time. This should not be the same for all of them and hence should be virtual.

Minute Essay zTry to think of an example of an instance where inheritance helps describe a logical ordering of types. What behaviors I this case might be virtual? zWe will not be having class the rest of this week. Feel free to use the time to work on assignment #7 and maybe even to try writing a little piece of code that uses inheritance.