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

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
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.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
CS 1110 Prelim II: Review Session 1. (Fall’07) Question 1 (15 points). Write the body of the following function recursively. /** = n, but with its digits.
Java bootcamp Spring Java Bootcamp 2004 Expectations: We assume that you have studied C, C++, or Java and know about the following: Variables and.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
CS 1110 Prelim II: Review Session 1. Exam Info Prelim 1: 7:30–9:00PM, Tuesday, Nov 9 th, Olin 155 (Last name starts with A-Lewis) and Olin 255 (Last name.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
CS 61B Data Structures and Programming Methodology July David Sun.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
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.
What is inheritance? It is the ability to create a new class from an existing class.
CS 1110 Prelim II: Review Session 1. Introduction My name: Bruno Abrahao – We have three additional TA’s in the room to help you individually Shuang Nam.
CECS 220 Java Test Review. Variable Names 0 May Contain: a-z, A-Z, 0-9, _, $, characters from other languages. 0 May not start with Legal: 0 MyVariable.
CS/ENGRD 2110 SPRING 2015 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.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Programming in Java CSCI-2220 Object Oriented Programming.
Types in programming languages1 What are types, and why do we need them?
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
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.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
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.
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.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
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
CS100J 21 February 2005 Casting About
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/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
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.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
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
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
Review for Midterm 3.
Presentation transcript:

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

About prelim 1 2  October 2: 5:30PM and at 7:30PM. We will tell you which to attend.  Problems with that one? You go to the other one.  Students with conflicts --having two prelims at 7:30 at that evening Take ours at 5:30 OR (take ours at 7:30 AND take makeup that the other class) New to Cornell? It is standard practice to take 2 prelims one evening  People who HAVE to be out of town should us the particulars --later, not now.  Anyone who misses the prelim will have their grade based on prelim 2 and the final. They will HAVE to take the final (may be optional, in a way to be explained in November).  Please don't us about prelim 1 now. Too early. Too much going on now for us to handle it. We’ll let you know when.

Assignment A3: Doubly linked Lists 3 Idea: maintain a list (2, 5, 7) like this: 3 h a1 2 a6 v succ 5 a6 a8 v succ 7 a8 null v succ Also, if we have a variable that contains a pointer to a node, it’s easy to remove that node or insert another value before or after that node. n a6 Easy to insert a node in the beginning!

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

Classes we work with today 5 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 Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 Object Animal DogCat class hierarchy:

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

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

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. Consequences of a class type 8 a0 null a1 v Animal[] v; declaration of v. Also means that each variable v[k] is of type Animal Animal variables 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 9 a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a0 c Cat a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a0 a Animal The same object a0, from the viewpoint of a Cat variable and an Animal variable c.getWeight() is legala.getWeight() is illegal because getWeight Is not available in class Animal

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

Type of v[0]: Animal Another example 11 a0 null a1 v a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 Should this call be allowed? Should program compile? v[0].getWeight() Should this call be allowed? Should program compile? v[k].getWeight()

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

Casting up class hierarchy 13 a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 You know about casts like (int) (5.0 / 7.5) (double) 6 double d= 5; // automatic cast Object Animal DogCat 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!

age Animal(String, int) isOlder(Animal) Explicit casts: unary prefix operators 14 a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() 5 c a0 Cat Object equals () … Rule: an object can be cast to the name of any partition that occurs within it — and to nothing else. a0 maybe cast to Object, Animal, Cat. An attempt to cast it to anything else causes an exception (Cat) c (Object) c (Animal) (Animal) (Cat) (Object) c These casts don’t take any time. The object does not change. It’s a change of perception

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

Example 16 a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } h a1 Animal 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 method is called

Components used from h 17 a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return age > h.age; } h a1 Animal 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 Cat partition

Explicit downward cast 18 h a0 Animal public class Animal { // If Animal is a Cat, return its weight; otherwise, return 0. public int checkWeight(Animal h) { if ( ! ) return 0; // { h is a Cat } Cat c= (Cat) h ; // downward cast return c.getWeight(); } a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 (Dog) h leads to runtime error. Don’t try to cast an object to something that it is not!

Operator instanceof, explicit downward cast 19 h a0 Animal public class Animal { // If Animal is a cat, return its weight; otherwise, return 0. public int checkWeight(Animal h) { if ( ! (h instanceof Cat) ) return 0; // { h is a Cat } Cat c= (Cat) h ; // downward cast return c.getWeight(); } a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 instanceof true iff object is an instance of the class —if object has a partition for class

Function equals 20 public class Object { /** Return true iff this object is * the same as ob */ public boolean equals(Object b) { return this == b; } x.equals(y) is same as x == y except when x is null! y ? Object x ? This gives a null-pointer exception: null.equals(y) a0 Object equals(Object)

Overriding function equals 21 Override function equals in a class to give meaning to: “these two (possibly different) objects of the class have the same values in some of their fields” For those who are mathematically inclined, like any equality function, equals should be reflexive, symmetric, and transitive. Reflexive: b.equals(b) Symmetric: b.equals(c) = c.equals(b) Transitive: if b.equals(c) and c.equals(d), then b.equals(d)

Function equals in class Animal 22 Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) public class Animal { /** = “h is an Animal with the same values in its fields as this Animal” */ public boolean equals (Object h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return name.equals(ob.name) && age == ob.age; } toString() 1. Because of h is an Animal in spec, need the test h instanceof Animal

Function equals in class Animal 23 Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) public class Animal { /** = “h is an Animal with the same values in its fields as this Animal” */ public boolean equals (Object h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return name.equals(ob.name) && age == ob.age; } toString() 2. In order to be able to reference fields in partition Animal, need to cast h to Animal

Function equals in class Animal 24 Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) public class Animal { /** = “h is an Animal with the same values in its fields as this Animal” */ public boolean equals (Object h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return name.equals(ob.name) && age == ob.age; } toString() 3. Use String equals function to check for equality of String values. Use == for primitive types

Why can’t the parameter type be Animal? 25 Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) public class Animal { /** = “h is an Animal with the same values in its fields as this Animal” */ public boolean equals (Animal h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return name.equals(ob.name) && age == ob.age; } toString() What is wrong with this?