CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110.

Slides:



Advertisements
Similar presentations
1 CS October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.
Advertisements

Programo Issues Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009.
Effective Java, Chapter 3: Methods Common to All Objects.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Java Type System and Object Model Horstmann ch , 7.7.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public.
Modern Programming Tools And Techniques-I
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Searching.
Chapter 11 Inheritance and Polymorphism
CS100J 21 February 2005 Casting About
CSC 205 Java Programming II
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
Sorry these slides weren’t available last evening!
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
CS100J 28 February 2008 Casting About
The fattest knight at King Arthur's round table was Sir Cumference
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
COMP 110 Some notes on inheritance, review
Non-static classes.
Overloading and Constructors
CSE 143 Lecture 22 The Object class; Polymorphism read
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
Overriding methods from the Object class: equals, toString
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
Searching.
Polymorphism 21-Apr-19.
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Chapter 7 Java Object Model
Building Java Programs
Presentation transcript:

CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110

Reminder: A1 due tonight

Today’s topics Casting, object-casting rule Compile-time reference rule Quick look at arrays Implementing equals, method getClass Review on your own if you need to: while and for loop While and for are what you would expect from other languages; we won’t spend time on them in class.

Classes we work with today class Animal subclasses Cat and Dog Put components common to animals in Animal Cat pet1= new Cat(5); Dog pet2= new Dog(6); a0 pet1 Cat a1 pet2 Dog a0 Animal Cat toString() purrs() age isOlder(Animal) 5 a1 Animal Dog toString() age isOlder(Animal) 6 Object Animal Dog Cat class hierarchy: Demo: Animal, Cat, Dog source code (Object partition is there but not shown) DEMO

Casting

Casting objects Object Animal Dog Cat You know about casts like: (int) (5.0 / 7.5) (double) 6 double d= 5; // cast implicit pet1 null Animal a0 pet2 a0 Cat You can also use casts with class types: Animal pet1= new Cat(5); // cast implicit Cat pet2= (Cat) pet1; a0 Animal Cat toString() purrs() age isOlder(Animal) 5 pet1 “blinders” A class cast doesn’t change the object. It just changes the perspective: how it is viewed!

Explicit casts: unary prefix operators Object-casting rule: At runtime, an object can be cast to the name of any partition that occurs within it —and to nothing else. a0 can be cast to Object, Animal, Cat. An attempt to cast it to anything else causes a ClassCastException. a0 Object equals() … Animal age isOlder(Animal) 5 (Cat) c (Object) c (Cat) (Animal) (Cat) (Object) c toString() purrs() Cat The object does not change. The perception of it changes. c a0 Cat

Implicit upward cast a0 Animal Cat toString() purrs() age isOlder(Animal) 5 public class Animal { /** = "this Animal is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } h “blinders” a1 Animal Dog toString() age isOlder(Animal) 6 Cat pet1= new Cat(5); Dog pet2= new Dog(6); if (pet2.isOlder(pet1)) {…} Those images are Grumpy Cat and Marnie the Dog. Demo: Demo.java, method casting() // pet1 is cast up to class // Animal and stored in h h a0 Animal pet1 a0 Cat pet2 a1 Dog DEMO

Compile-time reference rule

Compile-time reference rule (v1) see From a variable of type C, can reference only methods/fields that are available in class C. Animal pet1= new Animal(5); int m = pet1.purrs(); a0 pet1 Animal illegal The compiler will give you an error. a0 Animal age isOlder(Animal) 5 The Java compiler wants to keep you *safe*. So this rule requires there to be *some* method with the right name. But it doesn't have to know *which* method will be called! Checking the legality of pet1.purrs(…): Since pet1 is an Animal, purrs is legal only if it is declared in Animal or one of its superclasses. From an Animal variable, can use only methods available in class Animal

Quiz: Which references are legal? Animal Cat toString() purrs() age isOlder(Animal) 5 h a0 Animal A. h.toString() OK —it’s in class Object partition B. h.isOlder(…) OK —it’s in Animal partition C. h.purrs() ILLEGAL —not in Animal partition or Object partition h “blinders”

Arrays

Animal[] v= new Animal[3]; declaration of array v Create array of 3 elements a6 v null Assign value of new-exp to v a6 Animal[] 1 2 null Assign and refer to elements as usual: v[0]= new Animal(…); … a= v[0].getAge(); null null null 0 1 2 v Sometimes use horizontal picture of an array:

Array elements may be subclass objects Animal[] v; // declaration of v v= new Animal[3]; // initialization of v v[0]= new Cat(5); // initialization of 1st elem v[2]= new Dog(6); // initialization of 2nd elem a0 null a1 v 0 1 2 The type of v is Animal[] The type of each v[k] is Animal Animal objects

Compile-time reference rule (CTRR), applied null null null v 0 1 2 Animal[] v; // declaration of v v= new Animal[3]; // initialization of v Cat pet1= new Cat(5); // initialization of pet1 v[0]= pet1; // initialization of 1st elem int m= v[0].purrs(); // is this allowed? a0 a0 Animal Cat toString() purrs() age isOlder(Animal) 5 pet1 Not allowed! Type of v[0] is Animal. CTRR: May reference only methods available in Animal. purrs is not declared in Animal or one of its superclasses. Demo: Demo.java, method types() “v[0] blinders” DEMO

Contrast: Bottom-up rule, applied a0 null a1 v 0 1 2 Animal[] v= new Animal[3]; v[0]= new Cat(5); v[2]= new Dog(6); v[0].toString(); Which toString() gets called? a0 Animal Cat toString() purrs() age isOlder(Animal) 5 a1 Animal Dog toString() age isOlder(Animal) 6 Object Object toString() toString() Bottom-up / Overriding rule says function toString in Cat partition

Equals

How Object defines equals(o) p1 Point public boolean equals(Object o) { return this == o; } a0 p2 Point a1 p3 Point Point p1= new Point(5,4); Point p2= p1; if (p1 == p2) {...} // true? if (p1.equals(p2)) {...} // true? Point p3= new Point(5,4); if (p1 == p3) {...} // true? if (p1.equals(p3)) {...} // true? a0 Point x y 5 4 a1 Point x y 5 4

Defining equality for your own class Specification: Object.equals has a specification you must obey: reflexive, symmetric, transistive https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object- Reflexive x.equals(x) Symmetric x.equals(y) iff y.equals(x) Transitive if x.equals(y) and y.equals(z) then x.equals(z) (Provided x and y are not null) equals should say that x and y are equal iff they are indistinguishable

Are any of these equal? Assume that Cat and Dog have no fields. a0 Animal age equals(…) 6 a1 Animal Cat equals() age equals(…) 6 a2 Animal Dog equals() age equals(…) 6 Can objects a1 and a2 be considered equal? Can objects a0 and a1 be considered equal? If the two objects are not of the same class (e.g. Cat, or Animal) they shouldn’t be considered equal

Function getClass and static field class Animal Cat toString() purrs() age isOlder(Animal) 5 Instance method getClass() returns the class of the lowest partition in the object Object getClass() equals(Object) h.getClass() == Cat.class h.getClass() != Animal.class h.getClass() != Object.class h a0 Animal

Equals in Animal a0 Animal age 5 equals(Object) public class Animal { private int age; /** return true iff this and obj are of the same class * and their age fields have same values */ public boolean equals(Object obj) { } if (obj == null || getClass() != obj.getClass()) return false; Animal an= (Animal) obj; Orwell: all animals are equal, but some are more equal than others Demo: Cat.equals() return age == an.age; Almost every method equals that you write will have these three pieces DEMO

Equals in Animal a0 Animal age equals(Object) purr 5 public class Animal { /** return true iff this and obj are of the * same class, age fields have same values */ public boolean equals(Object obj) { … } equals(Object) Cat public class Cat extends Animal { /** return true iff this and obj are of the * same class and age and purr fields have same values */ public boolean equals(Object obj) { } if (!super.equals(obj)) return false; Cat cob= (Cat) obj; return purr.equals(cob.purr); DEMO

Object.equals public class Point { public int x; public int y; public Point(int x, int y) { this.x= x; this.y= y; } Point@01fb Object Point x y toString() equals(Object o)

Equality for Points public class Point { /** return “this and obj are of the same class, and this and obj have the same x and y fields” */ @Override public boolean equals(Object obj) { How can we tell whether this and obj are of the same class? }

Equality for Points /** return “this and obj are of the same class and this and obj have the same x and y fields” */ @Override public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) return false; } Point p= (Point)obj; // downcast to reference Point fields return x == p.x && y == p.y;

Casting advice function equals() requires casting But, use of explicit down-casts can indicate bad design DON’T: if ( … ) do something with (C1) x else if ( … ) do something with (C2) x else if (…) do something with (C3) x DO: x.do() … where do() is overridden in classes C1, C2, C3

Operator instanceof obj instanceof C Is true if object obj has a partition named C. if (s[k] instanceof Circle) { Circle cir= Circle(s[k]; }