The fattest knight at King Arthur's round table was Sir Cumference

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

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 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
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.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
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.
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
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.
What is inheritance? It is the ability to create a new class from an existing class.
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.
Programming in Java CSCI-2220 Object Oriented Programming.
More about inheritance Exploring polymorphism 5.0.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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,
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.
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.
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.
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.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
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 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
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
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
Object-Oriented Programming: Polymorphism
Java Programming Language
CS/ENGRD 2110 Spring 2016 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.
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
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
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
Presentation transcript:

The fattest knight at King Arthur's round table was Sir Cumference The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. CS/ENGRD 2110 Spring 2018 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110

Overview references in JavaHyperText Quick look at arrays array Casting among classes cast, object-casting rule 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

A2 is due Thursday Everyone should get 100/100 since we gave you all the test cases you need. Please look at the pinned Piazza note “Assignment A2” for information that is not in the handout and answers to questions.

Before Next Lecture… Abstract classes and interfaces Follow the tutorial on abstract classes and interfaces, and watch less than 13 minutes of videos. Visit JavaHyperText and click on Abstract classes and interfaces This will make Thursday’s lecture far more understandable. 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:

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.

Dog and Cat objects stored in Animal variable a0 null a1 v 0 1 2 Which function is called by v[0].toString() ? (Remember, the hidden Object partition contains toString().) Can store (pointers to) subclass objects in superclass variable 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

a.getPurrs() is obviously illegal. Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. a.getPurrs() is obviously illegal. The compiler will give you an error. a0 a Animal From an Animal variable, can use only methods available in class 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()

getNoise() toString() getPurrs() age isOlder(Animal) 5 Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. Suppose a0 contains an object of a subclass Cat of Animal. By the copmle-time reference 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

Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. 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

(JavaHyperText entry: compile-time reference rule.) Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. 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 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 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

Function h.equals(ob) Function h.equals(ob) returns true if objects h and ob are equal, where equality depends on the class. Here, we mean all corresponding fields are equal. h a0 k a1 h.equals(h): true h.equals(k): true h.equals(j): false a0.equals(a0): true a0.equals(a1): true a0.equals(a2) false j a2 Not Java 3 age equals(Object) a2 An 5 age equals(Object) a1 An 5 age equals(Object) a0 An

Function h.equals(ob) Function h.equals(ob) returns true if objects h and ob are equal, where equality depends on the class. Here, we mean all corresponding fields are equal. a0.equals(a0): true a0.equals(a1): false a0.equals(a2): false age _5_ equals(Object) a2 An Cat noise _”q”_ age _2_ equals(Object) a1 An Cat noise _”p”_ age _5_ equals(Object) a0 An Cat noise _”p”_

Function h.equals(ob) This function checks equality of age Function h.equals(ob) returns true if objects h and ob are equal, where equality depends on the class. Here, we mean all corresponding fields are equal. age _5_ equals(Object) a0 An Cat noise _”p”_ This function checks equality of age This function Calls superclass equality checks equality of noise

Function h.equals(ob) This function checks equality of age Function h.equals(ob) returns true if objects h and ob are equal, where equality depends on the class. Here, we mean all corresponding fields are equal. 5 age equals(Object) a1 An What is value of a1.equals(a0)? a0.equals(a1)? age _5_ equals(Object) a0 An Cat noise _”p”_ Obviously, h.equals(0b) has to check that the classes of h and ob are the same This function checks equality of age This function Calls super-class equality checks equality of noise

Function h.equals(ob) (1) Check classes of this and parameter age _5_ equals(Object) a0 An Cat noise _”p”_ (1) Check classes of this and parameter (2) Check age of this and parameter Call super-class equality (3) Check equality of noise Siamese f _8_ equals(Object) Call super-class equality (3) Check equality of f

Use function getClass h.getClass() Let Cat be the lowest partition of object h Then h.getClass() == Cat.class h.getClass() != Animal.class a0 Animal Cat purrs _____ getNoise() toString() getPurrs() age isOlder(Animal) 5 h a0 Animal

Equals in Animal a0 Animal age 5 equals(Object) public class Animal { private int age; /** return true iff this and ob are of the same class * and their age fields have same values */ public boolean equals(Object ob) { } if (ob == null || getClass() != ob.getClass()) return false; Animal an= (Animal) ob; // cast ob to Animal!!!! return age == an.age; // downcast was needed to reference age

Equals in Cat a0 Animal age 5 equals(Object) Cat noise _”p”_ public class Animal { private int age; /** return true iff this and ob are of same class * and their age fields have same values */ public boolean equals(Object ob) {} public class Cat extends Animal { private int age; /** return true iff this and ob are of same class * and their age and noise fields have same values */ public boolean equals(Object ob) {} } if (!super.equals(ob) return false; Cat ca= (Cat) ob; // downcast is necessary! return noise == ca.noise; // needed to reference noise

Use operator instanceof ob instanceof C true iff ob has a partition named C a0 Animal Cat purrs _____ getNoise() toString() getPurrs() age isOlder(Animal) 5 h instanceof Object true h instanceof Animal true h instanceof Cat true h instanceof JFrame false h a0 Animal

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!