Sorry these slides weren’t available last evening!

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
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 Oct 2009 Another classy lecture Casting about (secs 4.2, 4.3) 1.the class hierarchy 2.apparent and real classes 3.casting between classes 4.operator.
1 CS October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
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.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
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.
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.
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.
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Java Unit 11: Inheritance I
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
CS100J 21 February 2005 Casting About
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
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/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Overloading and Constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Announcements A3 will available on Piazza tomorrow. Refer often to the Piazza FAQ Note for A3. Please read the assignment A3 FAQ Notes on the Piazza.
Writing Methods AP Computer Science A.
slides created by Ethan Apter
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
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 2016 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
slides created by Ethan Apter
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
A3 is due after the break, but start on it NOW
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 2
Review: libraries and packages
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 21 Advanced List Implementation
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Presentation transcript:

Sorry these slides weren’t available last evening! CS/ENGRD 2110 Spring 2017 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110

Sorry these slides weren’t available last evening! Announcements A3 will available on Piazza tomorrow. Refer often to the Piazza FAQ Note for A3 Please read the assignment A3 FAQ Notes on the Piazza before asking a question. It might already be answered. Sorry these slides weren’t available last evening! Can keep a list in an array, using also an int variable: e.g. The list is in b[0..n-1]

Assignment A3: Linked Lists Idea: maintain a list (2, 5, 7) like this: 2 a1 a6 v succ 5 a6 a8 v succ 7 a8 null v succ h a1 This is a singly linked list To save space we write names like a6 instead of N@35abcd00 3

How to insert a node at the beginning 2 a1 a6 v succ 5 a6 a8 v succ 7 a8 null v succ h a1 (2, 5, 7) h a3 2 a1 a6 v succ 5 a8 7 null 8 (8, 2, 5, 7) 4

How to remove successor of a node in the middle 2 a1 a6 v succ 5 a6 a2 v succ 8 a2 a8 v succ 7 a8 null v succ h a1 k a6 (2, 5, 8, 7) h a1 2 a6 v succ 5 a8 7 null 8 a2 k (2, 5, 7) 5

Assignment A3: Use an inner class In addition: methods of C can reference private components of Cin public class C { private int x; public void m(int y) { } } … ob= new Cin(…); ob.b= 5; private class Cin { private int b; } public void mm() { … x= b; … } Inside-out rule: Objects of Cin can reference components of the object of C in which they live.

Assignment A3: Generics public class LinkedList { void add(Object elem) {…} Object get(int index) {…} } Values of linked list are probably of class Object public class LinkedList<E> { void add(E elem) {…} E get(int index) {…} } You can specify what type of values ns = new LinkedList<Integer>(); ns.add(“Hello”); // error ns.add(5); String s = ns.get(0); // error int n = ns.get(0); ss = new LinkedList<String>(); ss.add(“Hello”); ss.add(5); // error String s = ss.get(0); int n = ss.get(0); // error

Overview ref in text and JavaSummary.pptx Quick look at arrays slide 50-55 Casting among classes C.33-C.36 (not good) slide 34-41 Consequences of the class type slide 34-41 Operator instanceof slide 40 Function equals slide 37-41 Homework. Learn about while/ for loops in Java. Look in text. while ( <bool expr> ) { … } // syntax for (int k= 0; k < 200; k= k+1) { … } // example

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

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, partition Object contains toString() a0 Animal Cat toString() toNoise() getWeight() age isOlder(Animal) 5 a1 Animal Dog getString() toNoise() 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 As we see on next slide, the type of a class variable like v[k] determines what methods can be called

From an Animal variable, can use only methods available in class Animal a.getWeight() is obviously illegal. The class won’t compile. When checking legality of a call like a.getWeight(…) since the type of a is Animal, function getWeight must be declared in Animal or one of its superclasses. a0 a Animal a0 Animal age isOlder(Animal) 5

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.getWeight(…) 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() getWeight() age isOlder(Animal) 5 When checking legality of a call like a.getWeight(…) since the type of a is Animal, function getWeight must be declared in Animal or one of its superclasses.

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.getWeight() is legal a.getWeight() is illegal because getWeight is not available in class Animal a0 Animal Cat getNoise() toString() getWeight() age isOlder(Animal) 5 a0 Animal Cat getNoise() toString() getWeight() 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 a0 c C a0 Object C m(…) must be declared in one of these classes … … …

Another example Type of v[0]: Animal Should this call be allowed? Should program compile? v[0].getWeight() Should this call be allowed? Should program compile? v[k].getWeight() a0 Animal Cat getNoise() toString() getWeight() age isOlder(Animal) 5 a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 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. getWeight() not in class Animal or Object. Calls are illegal, program does not compile: v[0].getWeight() v[k].getWeight() a0 Animal Cat getNoise() toString() getWeight() 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() getWeight() 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 Discuss casts up/down class hierarchy. Animal h= new Cat(“N”, 5); Cat c= (Cat) h; A class cast doesn’t change the object. It just changes the perpective –how it is viewed!

Explicit casts: unary prefix operators Rule: 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() getWeight() Cat 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() getWeight() 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

Example a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } Type of h is Animal. Syntactic property. Determines at compile-time what components can be used: those available in Animal If a method call is legal, the overriding rule determines which implementation is called h a1 Animal

Components used from h a1 Animal Dog getNoise() toString() age isOlder(Animal) 6 public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } h.toString() OK —it’s in class Object partition h.isOlder(…) OK —it’s in Animal partition h.getWeight() ILLEGAL —not in Animal partition or Object partition By overriding rule, calls toString() in Dog partition h a1 Animal

Explicit downward cast Animal Cat weight ___ getNoise() toString() getWeight() age isOlder(Animal) 5 public class Cat extends Animal { private float weight; /** 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 weight == c.getWeight(); } h a0 Animal (Dog) ob leads to runtime error. Don’t try to cast an object to something that it is not!

Operator instanceof, explicit down cast Animal Cat weight ___ getNoise() toString() getWeight() age isOlder(Animal) 5 public class Cat extends Animal { private float weight; /** return true iff ob is a Cat and its * fields have same values as this */ public boolean equals(Object ob) { if ( ! (ob instanceof Cat) ) return false; // { h is a Cat } if ( ! super.equals(ob) ) return false; Cat c= (Cat) ob ; // downward cast return weight == c.getWeight(); } 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() (dothing overridden in C1, C2, C3) But how do I implement equals() ? That requires casting