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

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
1 CS October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
More about inheritance Exploring polymorphism 5.0.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
More about inheritance Exploring polymorphism 5.0.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
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.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Advanced Programming in Java
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
CS100J 21 February 2005 Casting About
More about inheritance
Methods Attributes Method Modifiers ‘static’
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
Sorry these slides weren’t available last evening!
CS100J 28 February 2008 Casting About
The fattest knight at King Arthur's round table was Sir Cumference
COS 260 DAY 19 Tony Gauvin.
Continuing Chapter 11 Inheritance and Polymorphism
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
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.
Abstract classes, Interfaces
Building Java Programs
CS/ENGRD 2110 FALL 2016 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Encapsulation and Polymorphism
Advanced Programming in Java
Presentation transcript:

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

Overview ref in JavaHyperText Quick look at arrays array Casting among classes cast Operator instanceof Function getClass Function equals compile-time reference rule Homework. JavaHyperText while-loop for-loop while ( <bool expr> ) { … } // syntax for (int k= 0; k < 200; k= k+1) { … } // example

Announcements Search Piazza for your question (before posting)! Partner-finding event: Tuesday, September 12 at 5:30pm Phillips 203 There will be snacks!

Before Next Lecture… Follow the tutorial on abstract classes and interfaces, and watch the videos. Click these

Classes we work with today Work with a class Animal and subclasses like Cat and Dog Put components common to animals in Animal a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 a1 Dog getNoise() toString() 6 Object partition is there but not shown Object Animal Dog Cat class hierarchy:

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:

Which function is called? a0 null a1 v 0 1 2 Which function is called by v[0].toString() ? (Remember, the hidden Object partition contains toString().) a0 Animal Cat toString() getNoise() getPurrs() age isOlder(Animal) 5 a1 Animal Dog toString() getNoise() age isOlder(Animal) 6 Bottom-up or overriding rule says function toString in Cat partition

Consequences of a class type Animal[] v; declaration of v. Also means that each variable v[k] is of type Animal The type of v is Animal[] The type of each v[k] is Animal The type is part of the syntax/grammar of the language. Known at compile time. a0 null a1 v 0 1 2 Animal objects • For example, a variable of type Animal can hold any object with an Animal segment. So a plain Animal, a Dog, or a Cat. • (On next slide.) A variable’s type: • Restricts what values it can contain. • Determines which methods are legal to call on it.

From an Animal variable, can use only methods available in class Animal a.getPurrs() is obviously illegal. The compiler will give you an error. a0 a Animal a0 Animal age isOlder(Animal) 5 Dog When checking legality of a call like a.getPurrs(…) since the type of a is Animal, method getPurrs must be declared in Animal or one of its superclasses. see JavaHyperText: compile-time reference rule 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! getNoise() toString()

From an Animal variable, can use only methods available in class Animal Suppose a0 contains an object of a subclass Cat of Animal. By the rule below, a.getPurrs(…) is still illegal. Remember, the test for legality is done at compile time, not while the program is running. a0 a Animal a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 When checking legality of a call like a.getPurrs(…) since the type of a is Animal, method getPurrs must be declared in Animal or one of its superclasses. Types exist at compile time. Objects exist at run time. The compiler has NO IDEA what object you will put in what variable, except that it obeys the type rule: Animal variables hold objects with *some* Animal segment. see JavaHyperText: compile-time reference rule

From an Animal variable, can use only methods available in class Animal The same object a0, from the viewpoint of a Cat variable and an Animal variable a0 c Cat a0 a Animal c.getPurrs() is legal a.getPurrs() is illegal because getPurrs is not available in class Animal a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5

Rule for determining legality of method call Rule: c.m(…) is legal and the program will compile ONLY if method m is declared in C or one of its superclasses. (JavaHyperText entry: compile-time reference rule.) a0 Object C m(…) must be declared in one of these classes a0 c C … … …

Another example Type of v[0]: Animal Should this call be allowed? Should program compile? v[0].getPurrs() Should this call be allowed? Should program compile? v[k].getPurrs() a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 Again, the value of k only exists at run time! Not when you're compiling your code. So the compiler cannot *possibly* know which object you're referring to. And it just wants to keep you safe! a0 null a1 v 0 1 2

View of object based on the type Each element v[k] is of type Animal. From v[k], see only what is in partition Animal and partitions above it. getPurrs() not in class Animal or Object. Calls are illegal, program does not compile: v[0].getPurrs() v[k].getPurrs() a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 Components are in lower partitions, but can’t see them a0 null a1 v 0 1 2 Animal

Casting objects a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 Object Animal Dog Cat You know about casts like: (int) (5.0 / 7.5) (double) 6 double d= 5; // automatic cast a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 You can also use casts with class types: Animal h= new Cat("N", 5); Cat c= (Cat) h; A class cast doesn’t change the object. It just changes the perspective: how it is viewed!

Explicit casts: unary prefix operators Rule: At run time, 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 an exception a0 Object equals() … Animal age isOlder(Animal) 5 (Cat) c (Object) c (Animal) (Animal) (Cat) (Object) c getNoise() toString() getPurrs() Cat And they cannot fail at run time! These casts don’t take any time. The object does not change. It’s a change of perception. c a0 Cat

Implicit upward cast a0 Animal Cat getNoise() toString() getPurrs() age isOlder(Animal) 5 public class Animal { /** = "this Animal is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } Call c.isOlder(d) Variable h is created. a1 is cast up to class Animal and stored in h a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 Upward casts done automatically when needed h a1 Animal c a0 Cat d a1 Dog

Explicit downward cast Animal Cat purrs ____ getNoise() toString() getPurrs() age isOlder(Animal) 5 public class Cat extends Animal { private int purrs; /** return true iff ob is a Cat and its * fields have same values as this */ public boolean equals(Object ob) { ? // { h is a Cat } if ( ! super.equals(ob) ) return false; Cat c= (Cat) ob ; // downward cast return purrs == c.getPurrs(); } h a0 Animal (Dog) ob leads to runtime error. Don’t try to cast an object to something that it is not!

Method getClass, explicit down cast Animal Cat purrs _____ getNoise() toString() getPurrs() age isOlder(Animal) 5 public class Cat extends Animal { private int purrs; /** return true iff ob is a Cat and its * fields have same values as this */ public boolean equals(Object ob) { if ( ob.getClass() != getClass() ) return false; // { h is a Cat } if ( ! super.equals(ob) ) return false; Cat c= (Cat) ob ; // downward cast return purrs == c.getPurrs(); } h a0 Animal <object>.getClass() == <class-name>.class true iff <object>’s bottom partition is <class-name>

A complete implementation of equals Animal Cat purrs _____ getNoise() toString() getPurrs() age isOlder(Animal) 5 public class Cat extends Animal { private int purrs; /** return true iff ob is a Cat and its * fields have same values as this */ public boolean equals(Object ob) { if ( ob == null || ob.getClass() != getClass() ) return false; // { h is a Cat } if ( ! super.equals(ob) ) return false; Cat c= (Cat) ob ; // downward cast return purrs == c.getPurrs(); } h a0 Animal Check whether ob is null before calling getClass.

Operator instanceof a0 Animal Cat purrs _____ getNoise() toString() getPurrs() age isOlder(Animal) 5 // Both are true. if ( a0 instanceof Cat ) … if ( a0 instanceof Animal ) … // Only the first is true. if ( a0.getClass() == Cat.class ) … if ( a0.getClass() == Animal.class ) … h a0 Animal <object> instanceof <class-name> true iff <object> has a partition for <class-name>

Opinions about casting Use of instanceof and downcasts can indicate bad design DON’T: if (x instanceof C1) do thing with (C1) x else if (x instanceof C2) do thing with (C2) x else if (x instanceof C3) do thing with (C3) x DO: x.do() … where do is overridden in the classes C1, C2, C3 But how do I implement equals() ? That requires casting!