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

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.
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.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
CS 61B Data Structures and Programming Methodology July David Sun.
Inheritance, Static and Dynamic Typing. Announcements  Project 0 due Fri 2/13 at 11:59pm 24 slip hours (unused hours roll over)  HW3 released tonight,
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Effective Java: Generics Last Updated: Spring 2009.
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.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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 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.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Types in programming languages1 What are types, and why do we need them?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
 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
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
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.
Interfaces & Sub-types Weiss sec Scenario Instructor says: “Implement a class IntegerMath with two methods pow and fact with the following signatures:
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
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 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
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
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.
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.
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
Presentation transcript:

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

Announcements 2  A3 now available on CMS and Piazza. Refer often to the Piazza FAQ Note for A3  Please read the assignment FAQ Notes on the Piazza before asking a question. It might already be answered.

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 This is a singly linked list To save space we write names like a6 instead of

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

5 5 h a1 2 a6 v succ 5 a6 a2 v succ 7 a8 null v succ Easy to remove a node if you have its predecessor! (2, 5, 8, 7) 8 a2 a8 v succ k a6 h a1 2 a6 v succ 5 a6 a8 v succ 7 a8 null v succ 8 a2 a8 v succ k a6 (2, 5, 7)

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

Assignment A3: Generics 7 public class LinkedList { } Values of linked list are probably of class Object public class LinkedList { } You can specify what type of values new LinkedList (…)

Overview ref in text and JavaSummary.pptx 8  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

Big Picture: Type Systems 9 Andrew MyersRoss Tate Object types in Java Arrays Subtypes Method resolution Casts Binary methods Cornell Research Polyglot Compiler Object initialization Information-flow Pattern matching Decidability

Classes we work with today 10 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]; 11 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? 12 a0 null a1 v a0 Animal Cat Cat(String, int) toString() toNoise() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal Dog Dog(String, int) getString() toNoise() 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 13 a0 null a1 v Animal[] v; declaration of v. Also means that each variable v[k] is of type Animal 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 14 a0 Animal age Animal(String, int) isOlder(Animal) 5 a0 a 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.

From an Animal variable, can use only methods available in class Animal 15 a0 a Animal 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 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 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. …

From an Animal variable, can use only methods available in class Animal 16 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 17 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 18 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 19 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 20 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 21 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 22 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 23 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 24 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 Dog partition

Explicit downward cast 25 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 26 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