Overriding methods. Changing what we have inherited from e.g. Object

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

Inheritance. Transitivity

Rules and syntax for inheritance. The boring stuff

CLASS DESIGN. Objectives MODULE 4

C12a: The Object Superclass and Selected Methods

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

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

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

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

INHERITANCE. Spring 2019

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

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Part 5) Odds and ends

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Super-Classes and sub-classes

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

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

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

More about inheritance

CS 251 Intermediate Programming Inheritance

COMP 250 Fall inheritance Nov. 17, 2017

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

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

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Inheritance, Polymorphism, and Interfaces

Inheritance (Part 2) Notes Chapter 6

CS313D: ADVANCED PROGRAMMING LANGUAGE

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

Java Magistère BFA

Overriding Variables: Shadowing

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Software Development (cs2500)

Abstract Classes and Interfaces

Programming Languages and Techniques (CIS120)

Subclasses, Superclasses, and Inheritance

INHERITANCE AND EXTENDING CLASSES

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

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

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

Object-Oriented Concepts

Programming 2. Inheritance & Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Making New instances of Classes

Week 7 Inheritance. Written by Alexandros Evangelidis. Adapted from Joseph Gardiner. 10 November 2015

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

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

Introduction to Inheritance

Java Fundamentals (II)

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Instance Members and Static Members

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

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

The class Object. Lecture CS1122 Summer 2008

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Advanced Placement Computer Science. Inheritance and Polymorphism

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Chapter 5. Inheritance

Chapter 6: Inheritance

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

CS61B Lecture #8: Object-Oriented Mechanisms

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Lecture 18 CSE11 Fall 2013 Inheritance

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lecture 4: Extending Classes. Concept

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

CS 320 Introduction to Software Engineering Spring 2017

CSC9T4: Object Modelling, principles of OO design and implementation

Inheritance, and Polymorphism.

Inheritance Motivation

Declarations and Access Control SCJP tips

Arrays Classes & Methods, Inheritance

Java Enum Java, winter semester ,2018 1

UCLA PIC 20A Java Programming

CST141--Inheritance 2

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance and Polymorphism

Exceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions

AP CS Unit 6: Inheritance Notes

AP CS Unit 8: Inheritance Exercises

OBJECT ORİENTATİON ENCAPSULATİON

COMP 110/L Lecture 20. Kyle Dewey

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Programming II (CS300)

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

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Transcription:

Overriding methods Changing what we have inherited from e.g. Object

We inherit e.g. tostring(), equals() and hashcode() But as we said before, the versions of these methods as defined in java.lang.object are pretty simplistic. Don t worry. There is a solution to this problem! Since we are happy to have a tostring() method but feel the need to refine it a little to better serve our own class, we can simply override it. Overriding a method is the way to change the behavior of one method, but preferably without changing the meaning of that method.

Overriding tostring() The version of tostring() we inherited from Object simply returned a String with the class name and an @ and a hexadecimal number. This is probably not a very good summary of instances of our own objects as a String. Let s say we have declared a class Member with simply two instance variables, name and email. A better representation as a String of a Member instance would be the name and some delimiter and the email, e.g.: Peter Jackson, peter@jurassic.org

How to override tostring() To override a method means declaring it like it was declared in the superclass but changing the actual code in the method body. The tostring() implementation for the Member class would then look like this: public String tostring(){ return name + ", " + email; } Because the version in java.lang.object is defined with public access, String return type, the name tostring and no parameters. All these part must match (with some exceptions we skip here) in order to override the method.

Now, we ve overridden it. What s the use? The usable thing about overriding it, is that code which deals with any type of object, safely can call tostring() on any instance (via a reference). If the actual class (type) of the object has its own version of the method tostring(), that code is the code which will be executed at runtime! In fact, if we now did this: Object o = new Member("Adam", "adam@email.com"); System.out.println(o); The println method would eventually (in the end) call the version of tostring() which we have overridden in the Member class. Even though the reference o is declared as of type Object.

Overriding allows for flexibility Going back to our file manager example, overriding the thumbnail() method in the class File (if we pretend it has such a method) in the classes AudioFile and VideoFile and ImageFile would allow our application to loop through a list of File objects (viewed as being of type File) and call the thumbnail() method on each of them with a different result depending on the implementation in the overridden version in the different subclasses respectively.

Overriding equals - a few words In short, there is a rule you should obey when overriding equals(), and it is to also override hashcode(). This is because two objects which are considered equals (the equals method on one object with the other as parameter returns true), should also return the same int number when hashcode() is called. hashcode() should be a unique number (preferably) for an object. The reason for this rule, is that many methods expect this rule to be followed, and may use hashcode() for some reason, like when checking if an equal object exists in some colletion, like the key set of a Properties object.

There are excellent tutorials on hashcode etc Use a search engine to find one!

A method can get a parameter as Object One additional (potential) benefit of extending Object and overriding for instance tostring() in a subclass, is that there might exist a method somewhere which we can call with any instance regardless of its class type. Such a method could accept a parameter of type Object, and then make use of the fact that it knows it can call tostring() on the parameter and at least get a String back (perhaps the boring string with @ but still a String). In fact, println() in PrintStream has exactly this situation! Let s look a little more on that...

What does println do? It calls String.valueOf // in class PrintStream: public void println(object x) { // can print any object! String s = String.valueOf(x); synchronized (this) { print(s); newline(); } } // in class String: public static String valueof(object obj) { return (obj == null)? "null" : obj.tostring(); }

Accessing the inherited version You can use the keyword super to access stuff in the parent class. Let s say we want to use the inherited version of tostring() as part of our own tostring(): public String tostring(){ return super.tostring() + " with name: " + name + " and email: " + email; } // Member@2608fb65 with name: Ben Afflec and email: a@b.c