Inheritance (Part 2) Notes Chapter 6

Similar documents
Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance (Part 5) Odds and ends

Inheritance, cont. Notes Chapter 6 and AJ Chapters 7 and 8

Interfaces. consider an interface for mathematical functions of the form

Inheritance (cont) Abstract Classes

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

INHERITANCE. Spring 2019

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Instance Members and Static Members

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

Inheritance. Transitivity

Java Fundamentals (II)

C12a: The Object Superclass and Selected Methods

Inheritance and Polymorphism

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

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

What is Inheritance?

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

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

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

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

Object-Oriented Concepts

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Rules and syntax for inheritance. The boring stuff

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

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

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

More Relationships Between Classes

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

Java Object Oriented Design. CSC207 Fall 2014

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

Example: Count of Points

Chapter 5 Object-Oriented Programming

Programming overview

EECS2030 Week 7 worksheet Tue Feb 28, 2017

1 Shyam sir JAVA Notes

First IS-A Relationship: Inheritance

Computer Science 210: Data Structures

CMSC 132: Object-Oriented Programming II

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

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

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

Super-Classes and sub-classes

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

CS-202 Introduction to Object Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Magistère BFA

Introduction to Inheritance

Inheritance CSC 123 Fall 2018 Howard Rosenthal

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

CLASS DESIGN. Objectives MODULE 4

C++ Important Questions with Answers

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Collections, Maps and Generics

COMP 250 Fall inheritance Nov. 17, 2017

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Polymorphism. return a.doublevalue() + b.doublevalue();

Inheritance and Interfaces

Inheritance and Polymorphism

Exercise: Singleton 1

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Example: Count of Points

Inheritance, Polymorphism, and Interfaces

When the above code runs, which type s execute method will be run? When the above code runs, which type s execute method will be run?

Inheritance and Polymorphism

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

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

CSCE3193: Programming Paradigms

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS/ENGRD 2110 SPRING 2018

CSC9T4: Object Modelling, principles of OO design and implementation

Class, Variable, Constructor, Object, Method Questions

Distributed Systems Recitation 1. Tamim Jabban

Chapter 6 Introduction to Defining Classes

Chapter 14 Abstract Classes and Interfaces

Practice for Chapter 11

FAQ: Classes & Objects

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Arrays Classes & Methods, Inheritance

Introductory Programming Inheritance, sections

public static boolean isoutside(int min, int max, int value)

COP 3330 Final Exam Review

More about inheritance

Everything is an object. Almost, but all objects are of type Object!

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

CS1150 Principles of Computer Science Objects and Classes

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

The class Object. Lecture CS1122 Summer 2008

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Lecture 18 CSE11 Fall 2013 Inheritance

PROGRAMMING LANGUAGE 2

CIS 110: Introduction to computer programming

Abstract Classes and Interfaces

COMP 250. inheritance (cont.) interfaces abstract classes

Object Orientated Programming Details COMP360

Polymorphism: Inheritance Interfaces

CS Programming I: Inheritance

Transcription:

Inheritance (Part 2) Notes Chapter 6 1

Object Dog extends Object Dog PureBreed extends Dog PureBreed Mix BloodHound Komondor... Komondor extends PureBreed 2

Implementing Inheritance suppose you want to implement an inheritance hierarchy that represents breeds of dogs for the purpose of helping people decide what kind of dog would be appropriate for them many possible fields: appearance, size, energy, grooming requirements, amount of exercise needed, protectiveness, compatibility with children, etc. we will assume two fields measured on a 10 point scale size from 1 (small) to 10 (giant) energy from 1 (lazy) to 10 (high energy) 3

Dog public class Dog extends Object { private int size; private int energy; // creates an "average" dog Dog() { this(5, 5); } Dog(int size, int energy) { this.setsize(size); this.setenergy(energy); } 4

public int getsize() { return this.size; } public int getenergy() { return this.energy; } public final void setsize(int size) { this.size = size; } } public final void setenergy(int energy) { this.energy = energy; } why final? stay tuned 5

What is a Subclass? a subclass looks like a new class that has the same API as its superclass with perhaps some additional methods and fields the new class has direct access to the public and protected* fields and methods without having to redeclare or re-implement them the new class can introduce new fields and methods the new class can re-define (override) its superclass methods 6 * the notes does not discuss protected access

Mix UML Diagram a mixed breed dog is a dog whose ancestry is unknown or includes more than one pure breed Dog Mix 1 breeds ArrayList<String> 7

Dog - size : int - energy : int + setsize() + setenergy() + equals(object) : boolean + hashcode() : int + tostring() : String... Mix - breeds : ArrayList<String> + getbreeds() : List<String> + equals(object) : boolean + hashcode() : int + tostring() : String... subclass can add new fields subclass can add new methods subclass can change the implementation of inherited methods 8

What is a Subclass? a subclass looks like a new class that has the same API as its superclass with perhaps some additional methods and fields inheritance does more than copy the API of the superclass the derived class contains a subobject of the parent class the superclass subobject needs to be constructed (just like a regular object) the mechanism to perform the construction of the superclass subobject is to call the superclass constructor 9

What is a Subclass? another model of inheritance is to imagine that the subclass contains all of the fields of the parent class (including the private fields), but cannot directly use the private fields 10

Mix Memory Diagram belongs to superclass private in superclass not accessible by name to Mix 500 Mix object size 1 energy 10 breeds 1000 11

Constructors of Subclasses the purpose of a constructor is to set the values of the fields of this object how can a constructor set the value of a field that belongs to the superclass? by calling the superclass constructor and passing this as an implicit argument 12

Constructors of Subclasses 1. the first line in the body of every constructor must be a call to another constructor if it is not then Java will insert a call to the superclass default constructor if the superclass default constructor does not exist or is private then a compilation error occurs 2. a call to another constructor can only occur on the first line in the body of a constructor 3. the superclass constructor must be called during construction of the derived class 13

Mix (version 1) public final class Mix extends Dog { // no declaration of size or energy; part of Dog private ArrayList<String> breeds; public Mix () { // call to a Dog constructor super(); this.breeds = new ArrayList<String>(); } public Mix(int size, int energy) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList<String>(); } 14

public Mix(int size, int energy, ArrayList<String> breeds) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList<String>(breeds); } 15

Mix (version 2 using chaining) public final class Mix extends Dog { // no declaration of size or energy; part of Dog private ArrayList<String> breeds; public Mix () { // call to a Mix constructor this(5, 5); } public Mix(int size, int energy) { // call to a Mix constructor this(size, energy, new ArrayList<String>()); } 16

public Mix(int size, int energy, ArrayList<String> breeds) { // call to a Dog constructor super(size, energy); this.breeds = new ArrayList<String>(breeds); } 17

why is the constructor call to the superclass needed? because Mix is-a Dog and the Dog part of Mix needs to be constructed 18

Mix mutt = new Mix(1, 10); Mix object Dog object 1. Mix constructor starts running creates new Dog subobject by invoking the Dog constructor 2. Dog constructor starts running creates new Object subobject by (silently) invoking the Object constructor 3. Object constructor runs sets size and energy creates a new empty ArrayList and assigns it to breeds Object object size 1 energy 10 breeds 1000 19

Mix Memory Diagram 500 Mix object size 1 energy 10 breeds 1000 1000 ArrayList<String> object... 20

Invoking the Superclass Ctor why is the constructor call to the superclass needed? because Mix is-a Dog and the Dog part of Mix needs to be constructed similarly, the Object part of Dog needs to be constructed 21

Invoking the Superclass Ctor a derived class can only call its own constructors or the constructors of its immediate superclass Mix can call Mix constructors or Dog constructors Mix cannot call the Object constructor Object is not the immediate superclass of Mix Mix cannot call PureBreed constructors cannot call constructors across the inheritance hierarchy PureBreed cannot call Komondor constructors cannot call subclass constructors 22

Constructors & Overridable Methods if a class is intended to be extended then its constructor must not call an overridable method Java does not enforce this guideline why? recall that a derived class object has inside of it an object of the superclass the superclass object is always constructed first, then the subclass constructor completes construction of the subclass object the superclass constructor will call the overridden version of the method (the subclass version) even though the subclass object has not yet been constructed 23

Superclass Ctor & Overridable Method public class SuperDuper { public SuperDuper() { // call to an over-ridable method; bad this.overrideme(); } } public void overrideme() { } System.out.println("SuperDuper overrideme"); 24

Subclass Overrides Method public class SubbyDubby extends SuperDuper { private final Date date; public SubbyDubby() { super(); this.date = new Date(); } @Override public void overrideme() { System.out.println("SubbyDubby overrideme : " + this.date); } } public static void main(string[] args) { SubbyDubby sub = new SubbyDubby(); sub.overrideme(); } 25

the programmer's intent was probably to have the program print: SuperDuper overrideme SubbyDubby overrideme : <the date> or, if the call to the overridden method was intentional SubbyDubby overrideme : <the date> SubbyDubby overrideme : <the date> but the program prints: SubbyDubby overrideme : null SubbyDubby overrideme : <the date> final attribute in two different states! 26

What's Going On? 1. new SubbyDubby() calls the SubbyDubby constructor 2. the SubbyDubby constructor calls the SuperDuper constructor 3. the SuperDuper constructor calls the method overrideme which is overridden by SubbyDubby 4. the SubbyDubby version of overrideme prints the SubbyDubby date field which has not yet been assigned to by the SubbyDubby constructor (so date is null) 5. the SubbyDubby constructor assigns date 6. SubbyDubby overrideme is called by the client 27

remember to make sure that your base class constructors only call final methods or private methods if a base class constructor calls an overridden method, the method will run in an unconstructed derived class 28