More about inheritance

Similar documents
Inheritance. A key OOP concept

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

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

Java Classes, Inheritance, and Interfaces

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

Practice for Chapter 11

Inheritance (Part 5) Odds and ends

Improving structure with inheritance. Main concepts to be covered. The DoME example. DoME objects. DoME classes. DoME object model

Object Oriented Programming. Java-Lecture 11 Polymorphism

Improving structure with inheritance

Java Magistère BFA

More On inheritance. What you can do in subclass regarding methods:

C12a: The Object Superclass and Selected Methods

CLASS DESIGN. Objectives MODULE 4

CS-202 Introduction to Object Oriented Programming

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

OOSE2 Vererbung und Polymorphie mit BlueJ

CIS 110: Introduction to computer programming

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 5 Object-Oriented Programming

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

Inheritance -- Introduction

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Inheritance

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Java Fundamentals (II)

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

The class Object. Lecture CS1122 Summer 2008

Inheritance and Polymorphism

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

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

Abstract Classes and Interfaces

What is Inheritance?

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

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Inheritance (continued) Inheritance

More on Objects in JAVA TM

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

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

Big software. code reuse: The practice of writing program code once and using it in many contexts.

PROGRAMMING LANGUAGE 2

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Rules and syntax for inheritance. The boring stuff

Inheritance and Polymorphism

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Chapter 4 Defining Classes I

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

Inheritance and Polymorphism. CS180 Fall 2007

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

The major elements of the object-oriented model

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Lecture 18 CSE11 Fall 2013 Inheritance

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

25. Generic Programming

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

CS 251 Intermediate Programming Inheritance

Programming II (CS300)

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CMSC 132: Object-Oriented Programming II

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

CS/ENGRD 2110 SPRING 2018

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

TeenCoder : Java Programming (ISBN )

Super-Classes and sub-classes

Inheritance. Transitivity

CS260 Intro to Java & Android 03.Java Language Basics

Object-Oriented Concepts

Methods Common to all Classes

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Questions Answer Key Questions Answer Key Questions Answer Key

Making New instances of Classes

Inheritance Motivation

JAVA MOCK TEST JAVA MOCK TEST II

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

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

AP CS Unit 6: Inheritance Notes

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

Distributed Systems Recitation 1. Tamim Jabban

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Questions Answer Key Questions Answer Key Questions Answer Key

Domain-Driven Design Activity

Final Examination Semester 3 / Year 2012

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Lecture 4: Extending Classes. Concept

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Transcription:

Main concepts to be covered More about inheritance Exploring polymorphism method polymorphism static and dynamic type overriding dynamic method lookup protected access 4.1 The inheritance hierarchy Conflicting output What we want CD: A Swingin' Affair (64 mins)* Frank Sinatra tracks: 16 my favourite Sinatra album DVD: O Brother, Where Art Thou? (106 mins) Joel & Ethan Coen The Coen brothers best movie! What we have title: A Swingin' Affair (64 mins)* my favourite Sinatra album title: O Brother, Where Art Thou? (106 mins) The Coen brothers best movie!

The problem Attempting to solve the problem The print method in Item only prints the common fields. Inheritance is a one-way street: A subclass inherits the superclass fields. The superclass knows nothing about its subclass s fields. Place print where it has access to the information it needs. Each subclass has its own version. But Item s fields are private. Database cannot find a print method in Item. Static type and dynamic type Static and dynamic type A more complex type hierarchy requires further concepts to describe it. Some new terminology: static type dynamic type method dispatch/lookup What is the type of c1? What is the type of v1? Car c1 = new Car(); Vehicle v1 = new Car();

Static and dynamic type The declared type of a variable is its static type. The type of the object a variable refers to is its dynamic type. The compiler s job is to check for static-type violations. for(item item : items) item.print(); // Compile-time error. Overriding: the solution print method in both superand subclasses. Satisfies both static and dynamic type checking. Overriding Method lookup Superclass and subclass define methods with the same signature. Each has access to the fields of its class. Superclass satisfies static type check. Subclass method is called at runtime it overrides the superclass version. What becomes of the superclass version? No inheritance or polymorphism. The obvious method is selected.

Method lookup Method lookup Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match. Polymorphism and overriding. The first version found is used. Method lookup summary The variable is accessed. The object stored in the variable is found. The class of the object is found. The class is searched for a method match. If no match is found, the superclass is searched. This is repeated until a match is found, or the class hierarchy is exhausted. Overriding methods take precedence. Super call in methods Overridden methods are hidden but we often still want to be able to call them. An overridden method can be called from the method that overrides it. super.method() Compare with the use of super in constructors.

Calling an overridden method public class CD public void print() super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberoftracks); Method polymorphism We have been discussing polymorphic method dispatch. A polymorphic variable can store objects of varying types. Method calls are polymorphic. The actual method called depends on the dynamic object type. The Object class s methods Methods in Object are inherited by all classes. Any of these may be overridden. The tostring method is commonly overridden: public String tostring() Returns a string representation of the object. public class Item Overriding tostring public String tostring() String line1 = title + " (" + playingtime + " mins)"); if(gotit) else return line1 + "*\n" + " return line1 + "\n" + " " + comment + "\n"); " + comment + "\n");

Overriding tostring Explicit print methods can often be omitted from a class: System.out.println(item.toString()); Calls to println with just an object automatically result in tostring being called: System.out.println(item); Object equality What does it mean for two objects to be the same? Reference equality. Content equality. Compare the use of == with.equals() Overriding the equals method public boolean equals(object obj) if(this == obj) return true; if(!(obj instanceof ThisType)) return false; ThisType other = (ThisType) obj; compare fields of this and other Overriding equals in Student public boolean equals(object obj) if(this == obj) return true; if(!(obj instanceof Student)) return false; Student other = (Student) obj; return name.equals(other.name) && id.equals(other.id) && credits == other.credits;

Overriding hashcode in Student Protected access /** * Hashcode technique taken from * Effective Java by Joshua Bloch. */ public int hashcode() int result = 17; result = 37 * result + name.hashcode(); result = 37 * result + id.hashcode(); result = 37 * result + credits; return result; Private access in the superclass may be too restrictive for a subclass. The closer inheritance relationship is supported by protected access. Protected access is more restricted than public access. We still recommend keeping fields private. Define protected accessors and mutators. Access levels Review The declared type of a variable is its static type. Compilers check static types. The type of an object is its dynamic type. Dynamic types are used at runtime. Methods may be overridden in a subclass. Method lookup starts with the dynamic type. Protected access supports inheritance.