Inheritance and Polymorphism

Similar documents
Inheritance and Polymorphism

OVERRIDING. 7/11/2015 Budditha Hettige 82

Polymorphism. Arizona State University 1

Review and Recursion

Chapter 14 Abstract Classes and Interfaces

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Programming II (CS300)

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

What is Inheritance?

Lecture Notes on Programming Languages

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

What are the characteristics of Object Oriented programming language?

CLASSES AND OBJECTS IN JAVA

ECE 3574: Dynamic Polymorphism using Inheritance

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Inheritance, and Polymorphism.

Pointers and Terminal Control

Inheritance, Polymorphism, and Interfaces

Inheritance and object compatibility

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Polymorphism. Zimmer CSCI 330

Object-Oriented Programming (OOP) Fundamental Principles of OOP

1 Shyam sir JAVA Notes

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

PROGRAMMING LANGUAGE 2

CMSC 132: Object-Oriented Programming II

Chapter 10 Classes Continued. Fundamentals of Java

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

25. Generic Programming

G Programming Languages - Fall 2012

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

C++ Important Questions with Answers

Object-oriented Programming. Object-oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

CS111: PROGRAMMING LANGUAGE II

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Polymorphism and Interfaces. CGS 3416 Spring 2018

Programming II (CS300)

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Java How to Program, 8/e

8. Polymorphism and Inheritance

Object Oriented Issues in VDM++

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Object Oriented Programming. Solved MCQs - Part 2

C++ Programming: Polymorphism

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Programming in C# Inheritance and Polymorphism

CS-202 Introduction to Object Oriented Programming

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Introduction to Object-Oriented Programming

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance. Transitivity

C++ Inheritance and Encapsulation

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Extending Classes (contd.) (Chapter 15) Questions:

Data Abstraction. Hwansoo Han

Binghamton University. CS-140 Fall Dynamic Types

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CPS 506 Comparative Programming Languages. Programming Language

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CSI33 Data Structures

Object-Oriented Programming

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance -- Introduction

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Inheritance and Substitution (Budd chapter 8, 10)

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

First IS-A Relationship: Inheritance

A <Basic> C++ Course

OOAD - OBJECT MODEL. The concepts of objects and classes are intrinsically linked with each other and form the foundation of object oriented paradigm.

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Information System Design (IT60105)

Data Structures and Programming with C++

COMP 110/L Lecture 19. Kyle Dewey

PROGRAMMING IN C++ COURSE CONTENT


Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

INHERITANCE. Spring 2019

The Essence of OOP using Java, Nested Top-Level Classes. Preface

VIRTUAL FUNCTIONS Chapter 10

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Chapter 6 Introduction to Defining Classes

Transcription:

Division of Mathematics and Computer Science Maryville College

Outline Inheritance 1 Inheritance 2 3

Outline Inheritance 1 Inheritance 2 3

The "is-a" Relationship Object classification is typically hierarchical. An object can be said to be either its type or another. For example it is correct to say "An alpaca is a mammal." When the "is-a" relationship is present, that implies inheritance.

Overview of Inheritance Inheritance is when a subclass is created from a base class. In our example "Mammal" is the base class and "Alpaca" is the subclass. A subclass takes on all of the attributes and methods of its base class. A subclass can override base class functions. A subclass has access to the base class s public and protected members. A subclass is a more specific class than its base class.

Syntax Inheritance class Alpaca : public Mammal { //class stuff here }; Visibility Specifiers public Public members of the base class become public members of the subclass protected Public members of the base class become protected members of the subclass private All members of the base class become private members of the subclass.

Overriding Functions Inheritance If a function in the base class and subclass both have the same function signature (name and parameter types), then the function is overridden. Invoking the function on an object of the subclass type will call the subclass version of the function. This is the beginning of the real power of inheritance. Let s play with this concept for a moment. (inheritance.cpp example)

Outline Inheritance 1 Inheritance 2 3

Overview of is a Greek-derived term which means "many forms". A reference to a subclass can be treated as a reference to a base class. The base class reference still behaves like the subclass! Allows us to program to the "general case", and further modularize software development. Allows us to program to the "general case", and further modularize software development. This is the most powerful aspect of OOP, but it is the one of the most misunderstood and incorrectly used feature of C++.

The virtual Specifier A function can be specified as virtual during its initial definition. A virtual function is a function which has been marked for dynamic binding. Invoking a virtual function on a base class pointer or reference will call the most-specific version of the function. This is what allows subclasses to change behaviors in code they never see! Unless you are really really really really certain you should do otherwise, member functions should be marked virtual. Let s try it out. (polymorphism.cpp example)

and Pointers only works with pointers and references. A statically defined object cannot participate in polymorphism! This is why we generally use pointers when referring to objects.

Up-casting and Down-casting Consider the following: Mammal *m; Alpaca *a1, *a2; a1 = new Alpaca; Up-casting - When a reference or pointer to a subclass is assigned to a reference or pointer of a base class. In C++, up-casting is implicit. m = a1; Down-casting - When a reference or pointer to a base class is converted to a reference or pointer of a subclass. This must be done explicitly. a2 = (Alpaca*) m;

Pure Virtual Functions and Abstract Classes A pure virtual function has no implementation in the base class. Such a function is created by assigning it the value of nullptr at definition time: virtual void display()=0; A class containing a pure virtual function is said to be an "abstract class". An abstract class cannot be instantiated. Subclasses are required to override all pure virtual functions.

Outline Inheritance 1 Inheritance 2 3

Program Specifications Allow the user to move a cursor around on the canvas. Pressing the first letter of a shape begins drawing it. Move the cursor and press enter to set points in the shape. When the shape is complete, it will appear on the canvas.

The Shapes Program Classes