CS/ENGRD 2110 SPRING 2018

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

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

Lecture 10. Overriding & Casting About

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Rules and syntax for inheritance. The boring stuff

More On inheritance. What you can do in subclass regarding methods:

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

INHERITANCE. Spring 2019

C a; C b; C e; int c;

Introduction to Inheritance

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS/ENGRD 2110 SPRING 2018

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

The class Object. Lecture CS1122 Summer 2008

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Super-Classes and sub-classes

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

CS-202 Introduction to Object Oriented Programming

1 Shyam sir JAVA Notes

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

More about inheritance

Java Magistère BFA

type conversion polymorphism (intro only) Class class

C12a: The Object Superclass and Selected Methods

Inheritance (Part 5) Odds and ends

25. Generic Programming

Inheritance and Polymorphism

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

What is Inheritance?

OBJECT ORİENTATİON ENCAPSULATİON

Chapter 5 Object-Oriented Programming

Polymorphism. return a.doublevalue() + b.doublevalue();

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Location: Planet Laser Interview Skills Workshop

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance (Part 2) Notes Chapter 6

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Polymorphism and Interfaces. CGS 3416 Spring 2018

CIS 110: Introduction to computer programming

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Inheritance and Polymorphism. CS180 Fall 2007

CS111: PROGRAMMING LANGUAGE II

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Check out Polymorphism from SVN. Object & Polymorphism

Abstract Classes and Interfaces

CSE 431S Type Checking. Washington University Spring 2013

CSE 143 Lecture 12 Inheritance

Java Object Oriented Design. CSC207 Fall 2014

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

COE318 Lecture Notes Week 8 (Oct 24, 2011)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lecture 4: The class hierarchy; static components

First IS-A Relationship: Inheritance

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Practice for Chapter 11

Implements vs. Extends When Defining a Class

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CSC9T4: Object Modelling, principles of OO design and implementation

Example: Count of Points

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

CS111: PROGRAMMING LANGUAGE II

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Inheritance. Transitivity

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Operators and Expressions

Making New instances of Classes

Programming Languages and Techniques (CIS120)

Inheritance (continued) Inheritance

CSE 401/M501 Compilers

CS260 Intro to Java & Android 03.Java Language Basics

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

CSE 113 A. Announcements - Lab

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

A Short Summary of Javali

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Basic Object-Oriented Concepts. 5-Oct-17

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Inheritance and Polymorphism

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Informatik II Tutorial 6. Subho Shankar Basu

Software Development (cs2500)

CLASS DESIGN. Objectives MODULE 4

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Selected Java Topics

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Transcription:

1 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 2 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 3 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 4 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 and subclasses like and Dog Put components common to animals in class hierarchy: Object Dog isolder() getpurrs() a1 6 isolder() Dog Object partition is there but not shown

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

Consequences of a class type 7 [] v; declaration of v. Also means that each variable v[k] is of type The type of v is [] The type of each v[k] is The type is part of the syntax/grammar of the langu. Known at compile time. v 0 1 2 null a1 objects A variable s type: Restricts what values it can contain. Determines which methods are legal to call on it.

Dog and objects stored in variable 8 Which function is called by v[0].tostring()? (Remember, the hidden Object partition contains tostring().) v 0 1 2 null a1 Can store (pointers to) subclass objects in superclass variable Bottom-up or overriding rule says function tostring in partition isolder() tostring() getnoise() getpurrs() a1 6 isolder() Dog tostring() getnoise()

Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. 9 a.getpurrs() is obviously illegal. The compiler will give you an error. a From an variable, can use only methods available in class When checking legality of a call like a.getpurrs( ) since the type of a is, method getpurrs must be declared in or one of its superclasses. isolder() Dog 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. 10 Suppose contains an object of a subclass of. By the copmletime reference rule below, a.getpurrs( ) is still illegal. Remember, the test for legality is done at compile time, not while the program is running. a When checking legality of a call like a.getpurrs( ) since the type of a is, method getpurrs must be declared in or one of its superclasses. see JavaHyperText: compile-time reference rule isolder() getpurrs()

Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. 11 c The same object, from the viewpoint of a variable and an variable a c.getpurrs() is legal isolder() getpurrs() a.getpurrs() is illegal because getpurrs is not available in class isolder() getpurrs()

Compile-time reference rule: From a variable of type C, can reference only methods/fields that are available in class C. 12 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.) c C m( ) must be declared in one of these classes Object C

Another example 13 Type of v[0]: Should this call be allowed? Should program compile? v v[k].getpurrs() 0 1 2 null a1 isolder() getpurrs() Should this call be allowed? Should program compile? v[0].getpurrs() a1 6 isolder() Dog

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

1 Casting objects You know about casts like: (int) (.0 / 7.) (double) 6 double d= ; Dog // automatic cast Object You can also use casts with class types: h= new ("N", ); c= () h; A class cast doesn t change the object. It just changes the perspective: how it is viewed! isolder() getpurrs() a1 6 isolder() Dog

Explicit casts: unary prefix operators 16 Object-casting rule: At runtime, an object can be cast to the name of any partition that occurs within it and to nothing else. can be cast to Object,,. An attempt to cast it to anything else causes an exception () c (Object) c () () () (Object) c equals() Object isolder() getpurrs() These casts don t take any time. The object does not change. It s a change of perception. c

17 Implicit upward cast public class { /** = "this is older than h" */ public boolean isolder( h) { return > h.; } Call c.isolder(d) Variable h is created. a1 is cast up to class and stored in h Upward casts done automatically when needed h a1 c d a1 Dog isolder() getpurrs() a1 6 isolder() Dog

Function h.equals(ob) 20 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 k a1 h.equals(h): h.equals(k): h.equals(j): true true false.equals(): true.equals(a1): true.equals(a2) false Not Java j a2 a2 a1 3 An An An

Function h.equals(ob) 21 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..equals(): true.equals(a1): false.equals(a2): false a2 _3_ An noise _ q _ a1 _2_ An noise _ p An noise _ p _

Function h.equals(ob) 22 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. This function checks equality of This function (1) Calls superclass equality (2) checks equality of noise An noise _ p _

Function h.equals(ob) 23 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. What is value of a1.equals()?.equals(a1)? Obviously, h.equals(0b) has to check that the classes of h and ob are the same This function checks equality of This function (1) Calls super-class equality (2) checks equality of noise a1 An An noise _ p _

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

Use function getclass 2 h.getclass() Let be the lowest partition of object h Then h.getclass ==.class h.getclass!=.class isolder() purrs getpurrs() h

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

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

Use operator instanceof 28 ob instanceof C true iff ob has a partition named C h instanceof Object true h instanceof true h instanceof true h instanceof JFrame false isolder() purrs getpurrs() h

Opinions about casting 29 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!