C12a: The Object Superclass and Selected Methods

Similar documents
CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

C20a: Selected Interfaces in Java API

The class Object. Lecture CS1122 Summer 2008

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

C18a: Abstract Class and Method

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

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

C09: Interface and Abstract Class and Method

Abstract Classes and Interfaces

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

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

Java Fundamentals (II)

Java Magistère BFA

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

More about inheritance

Domain-Driven Design Activity

Inheritance and Polymorphism

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

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Introduction to Inheritance

C09: Interface, and Abstract Class and Method

Introduction to Object-Oriented Programming

Inheritance (Part 5) Odds and ends

Super-Classes and sub-classes

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

java.lang.object: Equality

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

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

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

CLASS DESIGN. Objectives MODULE 4

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Inheritance (Part 2) Notes Chapter 6

Distributed Systems Recitation 1. Tamim Jabban

CIS 110: Introduction to computer programming

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

CMSC 132: Object-Oriented Programming II

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

INHERITANCE. Spring 2019

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

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

C08: Inheritance and Polymorphism

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

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

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

Inheritance. Transitivity

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

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

C08: Inheritance and Polymorphism

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

MIT AITI Lecture 18 Collections - Part 1

C11: Garbage Collection and Constructors

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

Comparing Objects 3/14/16

Rules and syntax for inheritance. The boring stuff

CS 251 Intermediate Programming Inheritance

C27a: Stack and Queue

Methods Common to all Classes

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

Chapter 11 Inheritance and Polymorphism

Distributed Systems Recitation 1. Tamim Jabban

C22a: Problem Solving using Recursion

The Java Type System (continued)

Java Object Oriented Design. CSC207 Fall 2014

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

Java Enum Java, winter semester ,2018 1

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

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

Java: advanced object-oriented features

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

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

Inheritance (continued) Inheritance

C30b: Inner Class, Anonymous Class, and Lambda Expression

Inheritance and Interfaces

CS 520 Theory and Practice of Software Engineering Fall 2018

Comparing Objects 3/28/16

CS/ENGRD 2110 SPRING 2018

C10: Garbage Collection and Constructors

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

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

Questions Answer Key Questions Answer Key Questions Answer Key

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

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

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

Java Classes, Inheritance, and Interfaces

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Lecture 4. Types, Memory, Exceptions

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

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

25. Generic Programming

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Check out Polymorphism from SVN. Object & Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

Java Persistence API (JPA) Entities

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

Transcription:

CISC 3115 TY3 C12a: The Object Superclass and Selected Methods Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/4/2018 CUNY Brooklyn College 1

Outline The Object class and its methods tostring() equals and its contract The protected visibility modifier The final modifier ArrayList, arrays, and Collections 10/4/2018 CUNY Brooklyn College 2

The Object Class At the top of the Java class hierarchy tree is the java.lang.object class Every class in Java is descended from the java.lang.object class. Even if no inheritance is specified when a class is defined, the superclass of the class is actually Object. public class Circle {... } Equivalent public class Circle extends Object {... } 10/4/2018 CUNY Brooklyn College 3

The Superclass: The Object Class The Object class is a superclass of all Java classes Every class you use or write inherits the instance methods of the Object class You may override the methods with an implementation that is specific to your class. 10/4/2018 CUNY Brooklyn College 4

The tostring() Method in Object The tostring() method returns a string representation of the object. The default implementation public String tostring() { } return getclass().getname() + "@" + Integer.toHexString(hashCode()); returns a string consisting of a class name of which the object is an instance, the at sign (@), and a number representing this object. 10/4/2018 CUNY Brooklyn College 5

Example: The tostring() Method Example: try these statements Loan loan = new Loan(); System.out.println(loan.toString()); 10/4/2018 CUNY Brooklyn College 6

Overriding the tostring() Method The tostring() method is often overridden. 10/4/2018 CUNY Brooklyn College 7

Questions? The Object class and its tostring() method 10/4/2018 CUNY Brooklyn College 8

Comparing Objects Given two reference variables v1 and v2, you may do comparison as follows, v1 == v2 v1.equals(v2) where the equals method is defined in the Object class 10/4/2018 CUNY Brooklyn College 9

v1 == v2 compares the references held in v1 and v2 and determine whether they are identical. 10/4/2018 CUNY Brooklyn College 10

Remark: The == Operator It is used for comparing two primitive data type values or for determining whether two objects have the same references. 10/4/2018 CUNY Brooklyn College 11

v1.equals(v2) It depends on the implementation of the equals method The equals method is defined in the Object class with the following implementation public boolean equals(object obj) { } return (this == obj); 10/4/2018 CUNY Brooklyn College 12

Example: Comparing Students Consider a Student class public class Student { private int studentid; private String name; public Student(int sid, String name) { } } What do think you should get? Student s1 = new Student(100, John Doe ); Student s2 = new Student(100, John Doe ); System.out.println(s1.equals(s2)); 10/4/2018 CUNY Brooklyn College 13

Overriding the Equals Method We override the equals method in the Student class public boolean equals(object theother) { if (theother instanceof Student) { return id == ((Student)theOther).id && name.equals(((student)theother).name); } } else { } return false; 10/4/2018 CUNY Brooklyn College 14

Am I Overriding it? How about this? public boolean equals(student theother) { if (student!= null) { return id == theother.id && name.equals(theother.name); } } else { } return false; 10/4/2018 CUNY Brooklyn College 15

No. You Aren t These are two different methods boolean equals(student theother) { } boolean equals(object theother) { } 10/4/2018 CUNY Brooklyn College 16

Remark: The equals Method It is intended to test whether two objects have the same contents, provided that the method is overridden in a class, a subclass of Object. The == operator is stronger than the equals method, in that the == operator checks whether the two reference variables refer to exactly the same object in the memory (the heap). 10/4/2018 CUNY Brooklyn College 17

Enforcing the Contract The API documentation states, Note that it is generally necessary to override the hashcode method whenever this method is overridden, so as to maintain the general contract for the hashcode method, which states that equal objects must have equal hash codes. 10/4/2018 CUNY Brooklyn College 18

Questions? Every class in Java is a descendent of the Object class To compare two objects, we generally need to override the equals method What is the intended difference between the == operator and the equals method? How do we properly override the equals method? 10/4/2018 CUNY Brooklyn College 19