Object-Oriented ProgrammingInheritance & Polymorphism

Similar documents
Object-Oriented Programming More Inheritance

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Java Session. Day 2. Reference: Head First Java

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Inheritance & Polymorphism. Object-Oriented Programming

Software Development (cs2500)

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

Practice for Chapter 11

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

CSA 1019 Imperative and OO Programming

Inheritance & Polymorphism

Create a Java project named week10

Teacher Training Solutions

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

COE318 Lecture Notes Week 8 (Oct 24, 2011)

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

C18a: Abstract Class and Method

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Object Orientated Programming Details COMP360

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Binghamton University. CS-140 Fall Dynamic Types

SWEN221: Software Development. 3: Inheritance I. David J. Pearce & Marco Servetto Computer Science, Victoria University

First IS-A Relationship: Inheritance

type conversion polymorphism (intro only) Class class

Polymorphism. Arizona State University 1

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Introduction to Object-Oriented Programming

Inheritance and Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

More Relationships Between Classes

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

index.pdf January 21,

CMSC 202. Generics II

CPSC 324 Topics in Java Programming

Java is an Object Oriented Programming language i.e. it supports four major pillars of OOPS.

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

ITI Introduction to Computing II

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

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

ITI Introduction to Computing II

What is Inheritance?

Object Oriented Issues in VDM++

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Derived and abstract data types. TDT4205 Lecture 15

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and object compatibility

Inheritance (Outsource: )

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Inheritance (an intuitive description)

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

C10: Garbage Collection and Constructors

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

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

CMSC 341. Nilanjan Banerjee

15CS45 : OBJECT ORIENTED CONCEPTS

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

CIS133J. Working with Numbers in Java

Object-Oriented Concepts

OVERRIDING. 7/11/2015 Budditha Hettige 82

Generalized Code. Fall 2011 (Honors) 2

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Language Features. 1. The primitive types int, double, and boolean are part of the AP

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

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

Object-Oriented Programming Classes, Objects, Variables

Classes and Inheritance Extending Classes, Chapter 5.2

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

The class Object. Lecture CS1122 Summer 2008

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Rules and syntax for inheritance. The boring stuff

Spring 2019 Discussion 4: February 11, 2019

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

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

COMP 110/L Lecture 20. Kyle Dewey

Chapter 5 Object-Oriented Programming

CS 61B Discussion 4: Inheritance Fall 2018

Exercise: Singleton 1

Questions Answer Key Questions Answer Key Questions Answer Key

C09: Interface and Abstract Class and Method

C11: Garbage Collection and Constructors

Transcription:

Inheritance Polymorphism Overriding and Overloading Object-Oriented Programming Inheritance & Polymorphism Inf1 :: 2008/09

Inheritance Polymorphism Overriding and Overloading 1 Inheritance Flat Hierarchy Layered Hierarchy 2 Polymorphism 3 Overriding and Overloading

Flat Animal Hierarchy Animal sleep() makenoise() roam() Lion Wolf makenoise() Cat makenoise() Dog makenoise() makenoise()

Animals Example, 1 Our base class: Animal Animal public class Animal { public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); public void makenoise() { Systemoutprintln( Noises ); public void roam() { Systemoutprintln( Roamin on the plain );

Animals Example, 2 1 Lion IS-A Animal 2 Override the makenoise() method Lion public class Lion extends Animal { public void makenoise() { Systemoutprintln( Roaring: Rrrrrr! );

Animals Example, 3 1 Cat IS-A Animal 2 Override the makenoise() method Cat public class Cat extends Animal { public void makenoise() { Systemoutprintln( Miaowing: Miaooo! );

Animals Example, 4 1 Wolf IS-A Animal 2 Override the makenoise() method Wolf public class Wolf extends Animal { public void makenoise() { Systemoutprintln( Howling: Ouooooo! );

Animals Example, 5 1 Dog IS-A Animal 2 Override the makenoise() method Dog public class Dog extends Animal { public void makenoise() { Systemoutprintln( Barking: Woof Woof! );

Animals Example, 6 The Launcher public class AnimalLauncher { public static void main(string[] args) { Systemoutprintln( \nwolf\n===== ); Wolf wolfie = new Wolf(); wolfiemakenoise() ; // from Wolf wolfieroam(); // from Animal wolfiesleep(); // from Animal Systemoutprintln( \nlion\n===== ); Lion leo = new Lion(); leomakenoise(); // from Lion leoroam(); // from Animal leosleep(); // from Animal

Animals Example, 7 Output Wolf ===== Howling: Ouooooo! Roamin on the plain Sleeping: Zzzzz Lion ===== Roaring: Rrrrrr! Roamin on the plain Sleeping: Zzzzz

Nested Animal Hierarchy Lions and canines can be grouped together into Felines, with common roam() behaviours Dogs and wolves can be grouped together into Canines, with common roam() behaviours

Nested Animal Hierarchy Animal sleep() makenoise() roam() Feline roam() Canine roam() Lion makenoise() Cat makenoise() Wolf makenoise() Dog makenoise()

Animals Example, 1 Same as before Animal public class Animal { public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); public void makenoise() { Systemoutprintln( Noises ); public void roam() { Systemoutprintln( Roamin on the plain );

Animals Example, 2 The new class Feline Feline public class Feline extends Animal { public void roam() { // Override roam() Systemoutprintln( Roaming: I m roaming alone );

Animals Example, 3 The new class Canine Canine public class Canine extends Animal { public void roam() { // Override roam() Systemoutprintln( Roaming: I m with my pack );

Animals Example, 4 1 Lion IS-A Feline 2 Override the makenoise() method Lion public class Lion extends Feline { public void makenoise() { Systemoutprintln( Roaring: Rrrrrr! ); Similarly for Cat

Animals Example, 5 1 Wolf IS-A Canine 2 Override the makenoise() method Wolf public class Wolf extends Canine { public void makenoise() { Systemoutprintln( Howling: Ouooooo! ); Similarly for Dog

Which method gets called? Animal sleep() makenoise() roam() 1 Wolf wolfie = new Wolf(); 2 wolfiemakenoise(); 3 wolfieroam(); Canine roam() 4 wolfiesleep(); Wolf makenoise()

Animals Example, 6 The Launcher public class AnimalLauncher { public static void main(string[] args) { Systemoutprintln( \nwolf\n===== ); Wolf wolfie = new Wolf(); wolfiemakenoise(); // from Wolf wolfieroam(); // from Canine wolfiesleep(); // from Animal Systemoutprintln( \nlion\n===== ); Lion leo = new Lion(); leomakenoise(); // from Lion leoroam(); // from Feline leosleep(); // from Animal

Animals Example, 7 Output Wolf ===== Howling: Ouooooo! Roaming: I m with my pack Sleeping: Zzzzz Lion ===== Roaring: Rrrrrr! Roaming: I m roaming alone Sleeping: Zzzzz

Inheritance Polymorphism Overriding and Overloading Typing and Polymorphism polymorphism (= many shapes ): the same piece of code can be assigned multiple types A class defines a type, namely the signatures of its methods S is a subtype of T, written S <: T, if a value of type S can be used in any context where a value of type T is expected The relation <: is reflexive: T <: T The relation <: is transitive: if S <: T and T <: U, then S <: U (NB: We say T is a supertype of S if S is a subtype of T) Inclusion polymorphism: objects of different types S1, S2, may be treated uniformly as instances of a common supertype T

Inheritance Polymorphism Overriding and Overloading Declaring and Initializing a Reference Variable create a Wolf object Wolf wolfie = new Wolf();

Inheritance Polymorphism Overriding and Overloading Declaring and Initializing a Reference Variable declare a reference variable Wolf wolfie = new Wolf();

Inheritance Polymorphism Overriding and Overloading Declaring and Initializing a Reference Variable link the object to the reference Wolf wolfie = new Wolf();

Inheritance Polymorphism Overriding and Overloading Declaring and Initializing a Reference Variable supertype object of subtype Animal wolfie = new Wolf(); Reference type can be supertype of the object type Eg, Wolf <: Animal

Inheritance Polymorphism Overriding and Overloading Polymorphic ArrayList The Launcher public class AnimalLauncher2 { public static void main(string[] args) { Wolf wolfie = new Wolf(); Lion leo = new Lion(); Cat felix = new Cat(); Dog rover = new Dog(); ArrayList< Animal > animals = new ArrayList<Animal>(); animalsadd(wolfie); animalsadd(leo); animalsadd(felix); animalsadd(rover); for ( Animal a : animals) { amakenoise();

Inheritance Polymorphism Overriding and Overloading Polymorphic Arrays ArrayList<Animal> is polymorphic animalsadd(wolfie) add an object of type Wolf OK since Wolf <: Animal for (Animal a : animals) for each object a of type T such that T <: Animal amakenoise() if a is of type T, use T s makenoise() method

Inheritance Polymorphism Overriding and Overloading Method Overriding, 1 If a class C overrides a method m of superclass D, then: Parameter lists must be same and return type must be compatible: 1 signature of m in C must be same as signature of m in D; ie same name, same parameter list, and 2 return type S of m in C must such that S <: T, where T is return type of m in D m must be at least as accessible in C as m is in D

Inheritance Polymorphism Overriding and Overloading Method Overriding, 2 method in Animal public void makenoise() { Wrong: method in Wolf public void makenoise( int volume ) { Wrong: method in Wolf private void makenoise() {

Inheritance Polymorphism Overriding and Overloading Method Overloading, 1 Overloading: two methods with same name but different parameter lists Overloaded makenoise public void makenoise() { public void makenoise(int volume) { Overloaded println Systemoutprintln(3); // int Systemoutprintln(30); // double Systemoutprintln((float) 30); // cast to float Systemoutprintln( 30 ); // String

Inheritance Polymorphism Overriding and Overloading Reading, etc This week, read Chapters 7 & 8 of Head First Java We ll look at Chapter 8 on Friday Try using Eclipse this week (see links from OOP Webpage)