Object-Oriented Programming More Inheritance

Similar documents
Object-Oriented ProgrammingInheritance & Polymorphism

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

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

Inheritance & Polymorphism. Object-Oriented Programming

Java Session. Day 2. Reference: Head First Java

Software Development (cs2500)

Practice for Chapter 11

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

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

Chapter 14 Abstract Classes and Interfaces

Inheritance & Polymorphism

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

First IS-A Relationship: Inheritance

What is Inheritance?

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

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

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

Polymorphism. Arizona State University 1

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

More Relationships Between Classes

Inheritance and Polymorphism

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

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

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

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

Rules and syntax for inheritance. The boring stuff

C18a: Abstract Class and Method

COMP 110/L Lecture 20. Kyle Dewey

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Inheritance (Outsource: )

Object Orientated Programming Details COMP360

ECE 122. Engineering Problem Solving with Java

COE318 Lecture Notes Week 8 (Oct 24, 2011)

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

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

Java Object Oriented Design. CSC207 Fall 2014

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

Create a Java project named week10

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

CSA 1019 Imperative and OO Programming

C10: Garbage Collection and Constructors

Programming II (CS300)

index.pdf January 21,

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

Lecture 4: Extending Classes. Concept

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

CS 61B Discussion 4: Inheritance Fall 2018

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

ITI Introduction to Computing II

ITI Introduction to Computing II

Object-Oriented Programming

Object Oriented Programming: Based on slides from Skrien Chapter 2

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object-Oriented Programming Classes, Objects, Variables

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

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

Questions Answer Key Questions Answer Key Questions Answer Key

CS Programming I: Inheritance

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

ITI Introduction to Computing II

15CS45 : OBJECT ORIENTED CONCEPTS

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Chapter 5. Inheritance

COMP 250 Fall inheritance Nov. 17, 2017

Inheritance, Polymorphism, and Interfaces

C09: Interface and Abstract Class and Method

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

CS 251 Intermediate Programming Inheritance

Inheritance. Transitivity

ITI Introduction to Computing II

Java Fundamentals (II)

Inheritance (continued) Inheritance

C++ Inheritance and Encapsulation

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

Binghamton University. CS-140 Fall Dynamic Types

CMSC 202. Generics II

Class, Variable, Constructor, Object, Method Questions

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

Java Magistère BFA

Generalized Code. Fall 2011 (Honors) 2

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Making New instances of Classes

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

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

Exercise: Singleton 1

Building custom components IAT351

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

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

Example: Count of Points

CS313D: ADVANCED PROGRAMMING LANGUAGE

C11: Garbage Collection and Constructors

Transcription:

Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 1 / 45

1 Inheritance Flat Hierarchy Layered Hierarchy 2 Polymorphism 3 Overriding and Overloading 4 Abstract Classes 5 Recap Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 2 / 45

Flat Animal Hierarchy Animal sleep() makenoise() roam() Lion Wolf makenoise() Cat makenoise() Dog makenoise() makenoise() Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 3 / 45

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 ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 4 / 45

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! ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 5 / 45

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! ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 6 / 45

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! ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 7 / 45

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! ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 8 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 9 / 45

Animals Example, 7 Output Wolf ===== Howling: Ouooooo! Roamin on the plain Sleeping: Zzzzz Lion ===== Roaring: Rrrrrr! Roamin on the plain Sleeping: Zzzzz Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 10 / 45

Nested Animal Hierarchy Lions and cats can be grouped together into Felines, with common roam() behaviours Dogs and wolves can be grouped together into Canines, with common roam() behaviours Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 11 / 45

Nested Animal Hierarchy Animal sleep() makenoise() roam() Feline roam() Canine roam() Lion makenoise() Cat makenoise() Wolf makenoise() Dog makenoise() Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 12 / 45

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 ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 13 / 45

Animals Example, 2 The new class Feline Feline public class Feline extends Animal { public void roam() { // Override roam() Systemoutprintln( Roaming: I m roaming alone ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 14 / 45

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 ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 15 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 16 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 17 / 45

Which method gets called? Animal sleep() makenoise() roam() 1 Wolf wolfie = new Wolf(); 2 wolfiemakenoise(); 3 wolfieroam(); Canine roam() 4 wolfiesleep(); Wolf makenoise() Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 18 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 19 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 20 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 21 / 45

Declaring and Initializing a Reference Variable create a Wolf object Wolf wolfie = new Wolf(); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 22 / 45

Declaring and Initializing a Reference Variable declare a reference variable Wolf wolfie = new Wolf(); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 23 / 45

Declaring and Initializing a Reference Variable link the object to the reference Wolf wolfie = new Wolf(); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 24 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 25 / 45

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(); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 26 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 27 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 28 / 45

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() { Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 29 / 45

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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 30 / 45

Method Overloading, 2 1 Return types can be different 2 You can t just change the return type gets treated as an invalid override 3 Access levels can be varied up or down Incorrect override of makenoise public String makenoise() { String howl = Ouooooo! ; return howl; Exception in thread main javalangerror: Unresolved compilation problem: The return type is incompatible with AnimalmakeNoise() at week06wolfmakenoise(wolfjava:15) at week06animallaunchermain(animallauncherjava:11) Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 31 / 45

Animal Objects? Creating new objects Wolf wolfie = new Wolf(); Animal leo = new Lion(); Animal weird = new Animal(); Animal class is meant to contain information that all animals have in common But this is not enough to define any one specific animal Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 32 / 45

Concrete vs Abstract Concrete Abstract Examples: Cat, Wolf Specific enough to be instantiated Examples: Animal, Feline Not intended to have instances Only useful if extended Any instances will have to be instances of a subclass of the abstract class Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 33 / 45

The Abstract Animal, 1 Animal abstract class Animal { public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); public void makenoise() { Systemoutprintln( Noises ); public void roam() { Systemoutprintln( Roamin on the plain ); Just put the keyword abstract before the class declaration Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 34 / 45

The Abstract Animal, 2 An abstract class can be extended by other abstract classes Canine and Feline can (and should) both be abstract Animal abstract class Animal { public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); public void makenoise() { Systemoutprintln( Noises ); public void roam() { Systemoutprintln( Roamin on the plain ); Just put the keyword abstract before the class declaration Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 35 / 45

The Abstract Animal, 2 Animal abstract class Animal { public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); public abstract void roam(); public abstract void makenoise(); Now has abstract methods! Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 36 / 45

The Abstract Animal, 3 roam() and makenoise() are abstract methods (we ve already seen these in interfaces): no body; must be implemented in any concrete subclass (implemented overriden); don t have to be implemented by an abstract subclass; can only be declared in an abstract class; sleep() is not abstract, so can be straightforwardly inherited Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 37 / 45

Abstract Classes in Animal Hierarchy <<Abstract>> Animal sleep() makenoise() roam() <<Abstract>> roam() Feline <<Abstract>> roam() Canine Lion makenoise() Cat makenoise() Wolf makenoise() Dog makenoise() Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 38 / 45

Using Abstract Classes Use an abstract class when you have several similar classes that: have a lot in common the implemented parts of the abstract class have some differences the abstract methods Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 39 / 45

Digression: Constructor Chaining Constructor of the immediate superclass invoked with super() If you don t explicitly call this, the compiler will Case 1 No constructor explicitly declared Compiler inserts public Foo() { super(); Case 2 You have already declared one or more constructors Compiler inserts the following as first statement in each one: super(); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 40 / 45

Recap: Implementing/Overriding, 1 If a class C implements or 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 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 41 / 45

Method Implementation, 1 Abstract method in Animal public abstract void makenoise(); Wrong: method in Wolf public void makenoise( int volume ) { Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 42 / 45

Method Overloading Overloading: two methods with same name but different parameter lists Overloaded makenoise public void makenoise(int volume) { public void makenoise() { makenoise(int 1); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 43 / 45

Method Implementation, 2 You cannot implement or override a method by providing just an overloaded method Declaration of sleep() in Animal public void sleep() { Systemoutprintln( Sleeping: Zzzzz ); Right: overriding sleep() in Wolf public void sleep() { Systemoutprintln( Sleeping: XXXXX ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 44 / 45

Method Implementation, 3 Wrong: overloading sleep() in Wolf public void sleep(int duration) { String sleepnoise = Sleeping: Zzzzz ; if (duration > 2) sleepnoise = Still Sleeping: XXXXX ; Systemoutprintln(sleepNoise); Right: overloading and overriding public void sleep(int duration) { public void sleep() { Systemoutprintln( Sleeping: XXXXX ); Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 45 / 45