Methods Common to all Classes

Similar documents
The class Object. Lecture CS1122 Summer 2008

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

Super-Classes and sub-classes

java.lang.object: Equality

equals() in the class Object

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

Making New instances of Classes

Creating an Immutable Class. Based on slides by Prof. Burton Ma

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

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

ITI Introduction to Computing II

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

ITI Introduction to Computing II

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Programming Languages and Techniques (CIS120)

Introduction to Inheritance

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

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Expected properties of equality

CSE 331 Software Design & Implementation

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 11: Collections and Maps

CSE 331 Software Design & Implementation

For this section, we will implement a class with only non-static features, that represents a rectangle

INHERITANCE. Spring 2019

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

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Java Fundamentals (II)

Programming 2. Inheritance & Polymorphism

Inheritance (continued) Inheritance

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

Inheritance and Polymorphism

CLASS DESIGN. Objectives MODULE 4

Programming Languages and Techniques (CIS120)

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

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java Object Oriented Design. CSC207 Fall 2014

JAVA MOCK TEST JAVA MOCK TEST II

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

TeenCoder : Java Programming (ISBN )

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Abstract Classes and Interfaces

MET08-J. Preserve the equality contract when overriding the equals() method

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

CompuScholar, Inc. 9th - 12th grades

Inheritance (Part 5) Odds and ends

C12a: The Object Superclass and Selected Methods

Computer Science II (20073) Week 1: Review and Inheritance

OBJECT ORİENTATİON ENCAPSULATİON

COMP 250 Fall inheritance Nov. 17, 2017

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Programming Languages and Techniques (CIS120e)

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Equality. Michael Ernst. CSE 331 University of Washington

Implementing Object Equivalence in Java Using the Template Method Design Pattern

Equality. Michael Ernst. CSE 331 University of Washington

Programming Languages and Techniques (CIS120)

CS-202 Introduction to Object Oriented Programming

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

Practice for Chapter 11

Inheritance, Polymorphism, and Interfaces

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance and Polymorphism

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

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

Programming Languages and Techniques (CIS120)

Intro to Computer Science 2. Inheritance

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

More about inheritance

Chapter 11 Inheritance and Polymorphism

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

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

Advanced Placement Computer Science. Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

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

Object-Oriented Programming in the Java language

CISC370: Inheritance

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

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

Inheritance. Inheritance

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Programming II (CS300)

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

The Liskov Substitution Principle

Chapter 14 Abstract Classes and Interfaces

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Transcription:

Methods Common to all Classes 9-2-2013

OOP concepts Overloading vs. Overriding Use of this. and this(); use of super. and super() Methods common to all classes: tostring(), equals(), hashcode() HW#1 posted; due Wednesday, 9/4/13 Reading Assignment: Effective Java 2 (EJ2) Chapter 3 Methods Common to All Objects, Items 8-10

public class Circle { private float radius; private Point center; A Circle has a Point UML Class Diagram Circle FilledCircle HAS-A (composition) 1 Point

Object UML Class Diagram IS-A (inheritance) A Circle is an Object Circle public class Circle { /* stuff */ (everything is an Object, except for a primitive value)

public class Circle { private float radius; private Point center; public Circle(int x, int y) public Circle(float r,int x,int y) public float getradius() public float computearea() // end class Circle IS-A Circle FilledCircle public class FilledCircle extends Circle { private Color fillcolor; public FilledCircle(float r, int x,int y, Color acolor) public float getcolor() // end class FilledCircle

Object IS-A IS-A Circle HAS-A (composition) 1 Point IS-A (inheritance) FilledCircle

Object methods: getclass(), tostring(), equals(), hashcode(), etc. Circle methods: constructors, get/setcenter(), get/setradius(), computearea() FilledCircle methods: constructors, get/setcolor()

Overloading multiple versions of a method, distinguished by their signatures (name and argument list) Overriding when a subclass completely replaces a method that it inherits from its superclass (same signature)

multiple versions of a method, distinguished by their signatures (name & argument list) it is very common to overload constructors example: public Circle(int x, int y) // creates unit circle public Circle(float radius, int x, inty)

can override an inherited method completely (that is, replace it) can specialize an inherited method by overriding the method in the superclass, but reusing code from the superclass It s common to do both

this reference to current object this.radius this() way to call another constructor in the same class

public Circle(float radius, int x, int y) { this.radius = radius; this.center = new Point(x,y); public Circle(int x, int y) { this(1.0f, x, y); /* reuses code */ advantage: can change the implementation without changing all the other constructors

public Circle(int x, int y) { this.radius = 1.0f; this.center = new Point(x,y); public Circle(float radius, int x, int y) { this(x, y); this.radius = radius;

this. see a dot => reference to the current object this( see opening parenthesis => calling another constructor in the same class

FilledCircle fc1 = new FilledCircle(1.5f, 100, 200, Color.blue); fc1 FilledCircle FilledCircle Circle center radius 1.5 fillcolor blue Point x y 100 200

public class Circle { private float radius; private Point center; public Circle(float r,int x,int y) { radius = r; center = new Point(x,y); public Circle(int x, int y) { this(1.0f, x, y); public float getradius() public float computearea() // end class Circle IS-A Circle FilledCircle public class FilledCircle extends Circle { private Color fillcolor; public FilledCircle(float r, int x, int y, Color acolor) { super(r, x, y); fillcolor = acolor; public float getcolor() // end class FilledCircle

super. see a dot => reference to a field or method in the embedded superclass super( see opening parenthesis => calling a superclass constructor

cf. Effective Java, Chapter 3 superclass java.lang.object methods: tostring(), equals(), hashcode(), finalize(), clone(), getclass(), wait(), notify(), notifyall() Which of these typically need to be specialized in subclasses? tostring(), equals(), hashcode() What about comparing objects? compareto()

System.out.println( Answer = + 42 ); String int in this context, + means String concatenation (polymorphic) So the int value 42 is converted to a string with the method Integer.toString()

Consider the following code: Circle c1 = new Circle(1.5f, 25, 80); System.out.println (c1); What gets printed? Why? Problem: c1.tostring() is automatically called. Search the inheritance hierarchy for a method tostring(), starting at Circle. Find and invoke the method in Object. Prints Circle@1be2d65 Solution: override tostring()

Object methods: getclass(), tostring(), equals(), getclass(), etc. Circle methods: constructors, get/setcenter(), get/setradius(), computearea() tostring(), equals(), hashcode() FilledCircle methods: constructors, get/setcolor() tostring(), equals(), hashcode()

cf. Effective Java 2, pp. 51-53 tostring returns a meaningful description of the object Can specify a format example: phone numbers, p. 52

public class Circle { // other stuff public String tostring() { return (this.getclass() + \n\t Center + this.center + \n\t Radius = + this.radius ); // note: Point should also override tostring()

public class Circle { // other stuff @Override public String tostring() { return (this.getclass() + \n\t Center + this.center + \n\t Radius = + this.radius ); // note: Point should also override tostring()

public class FilledCircle { // other stuff @Override public String tostring() { return (super.tostring() + \n\t Color + this.fillcolor() );

Just about every class needs its own tostring() method, but not every class needs its own equals() method that tests for equal values example: Random equals() doesn t apply the equals() inherited from your superclass already tests what you want the class is private (or package-private) and you are certain that equals() will never be invoked; in that case, an accidental call to equals() should be an error (more on exceptions later)

Override Object.equals() when objects in your class can be equal in value in some sense, different from equivalent circles, lines, dice? other? value classes Bloch, Effective Java 2, p. 34

flawed public class Circle { equals() must return boolean doesn t override Object.equals() the argument must be Object public Boolean equals(circle o) { return (this.radius == o.radius) && (this.center == o.center) ; to override a method, must have the same signature & return type

public class Circle { public boolean equals(object o) { return (this.radius == o.radius) && (this.center == o.center) ; flawed radius & center are private, it s OK here, but may need accessor functions to avoid breaking encapsulation of other classes center is a Point; can t test for equality w/ ==

public class Circle { public boolean equals(object o) { return ( this.radius == o.radius ) && ( (this.center).equals(o.center) ); flawed what if o is null? get a NullPointerException what if object o refers to something other than a circle, like a die for instance?

reflexive x.equals(x) is true symmetric x.equals(y) iff y.equals(x) transitive x.equals(y) && y.equals(z) => x.equals(z) consistent multiple calls to x.equals(y) all give the same result cautious x.equals(null) is false

public class Circle { public boolean equals(object o) { if (o == null) return false; if (o == this) return true; if (!(o instanceof Circle)) return false; Circle other = (Circle) o; /* required, but will be detected by the instanceof operator anyway */ /* not strictly required, but efficient */ /* this cast cannot fail */ return (this.radius == other.radius) && (this.center).equals(other.center) );

public boolean equals(object o) { if (this == o) return true; if (!(o instanceof Circle)) return false; Circle other = (Circle) o; /* include this performance optimization when the equals test is complex */ return (this.radius == other.radius) && ( (this.center).equals(other.center) ); /* cf. Effective Java pp. 33-34 for float, double, array */

Suppose you are the class designer for a new class T T has 3 fields: field1, field2, field3 of types A, B and C respectively (can generalize to n fields). A field1 suppose that A is a primitive type (like int) B field2 suppose that you will ignore this when testing for equality C field3 suppose that C is an object

public boolean equals(object o) { /* compares fields 1 and 3 for equality */ if (!(o instanceof T)) return false; T other = (T) o; return ( (this.field1 == other.field1) && (this.field3).equals(other.field3) );