1 CS1110 15 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.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
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.
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.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
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.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
More about inheritance Exploring polymorphism 5.0.
1 Topic 10 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.”
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CS 61B Data Structures and Programming Methodology July David Sun.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
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.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
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.
Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input.
Object Oriented Software Development
More about inheritance Exploring polymorphism 5.0.
Inheritance and Access Control CS 162 (Summer 2009)
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
Java Unit 11: Inheritance I
CS100J 21 February 2005 Casting About
More about inheritance
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
COS 260 DAY 19 Tony Gauvin.
Continuing Chapter 11 Inheritance and Polymorphism
Overloading and Constructors
slides created by Ethan Apter
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.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Building Java Programs
slides created by Ethan Apter
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
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
Java’s Central Casting
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

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 instanceof 5.function equals Reading for next time: Sec and chapter 7 on loops. A4 due Friday (extra consulting hours then) Time management tip #42: schedule deadlines on your calendar; also schedule the time it will take to do the work.

2 Object Acct GmHm the class hierarchy: Setting: maintaining Cornell accounts (Acct). Two kinds so far: GMail (Gm) and Hotmail (Hm). b0 Acct Gm Gm(String) alert(String) newClip(String) nid Acct(String) alert(String) getID() ac1 b1 Acct Hm Hm(String) alert(String) popUp(String) nid Acct(String) alert(String) getID() jp2 Both Gm and Hm override method alert(String), presumably in an application-specific way. ( Gmail might show a new “Web clip”, whereas Hotmail might create a popup). [Note: Acct might best be made an abstract class; see next lab.]

3 b0 Acct Gm Gm(String) alert(String) newClip(String) nid Acct(String) alert(String) getID() ac1 b1 Acct Hm Hm(String) alert(String) popUp(String) nid Acct(String) alert(String) getID() jp2 b0 The apparent (declared) type of a is Acct, and will always be Acct. This is a syntactic property having to do with compiling. g Gm b1b1 h Hm a b0 Acct The real type of a, the real class of the object whose name is currently in a, is Gm, but could change via assignment: a= h; This is a semantic property having to do with the current value of a.

4 Implicit casting up the class hierarchy (good news) b1 Acct Hm Hm(String) alert(String) popUp() nid Acct(String) alert(String) getID() jp2 h has apparent type Hm, but our list v has an apparent type based on Acct. Does this mean we must do an explicit cast to add h to v? v.add( (Acct) h); b0 b14 … Vector v … b1b1 h Hm Nope; luckily, casts up the hierarchy are automatic, allowing this: v.add(h);

5 b0 Acct Gm Gm(String) alert(String) newClip(String) nid Acct(String) alert(String) getID() ac 1 b1 Acct Hm Hm(String) alert(String) popUp(String) nid Acct(String) alert(String) getID() jp2 b0 null b1 Vector v More good news: Overriding (still) has the correct behavior First, the compiler checks that apparent type Acct has an alert method; if that succeeds, then the bottom-up rule is applied. v.get(0).alert() will call the over-riding, Gmail-specific alert() method. v.get(2).alert() will call the over-riding, Hotmail-specific alert() method.

6 b0 Acct Gm Gm(String) alert(String) newClip(String) nid Acct(String) alert(String) getID() ac 1 b1 Acct Hm Hm(String) alert(String) popUp(String) nid Acct(String) alert(String) getID() jp2 b0 null b1 Vector v A sensible policy with an embedded “gotcha”: The apparent type can rule out some available methods. The apparent type of v, based on Acct, does not have a newClip method. Therefore, the compiler rules the call v.get(0).newClip(“FLOOD”) illegal, even though in practice, the real type of v.get(0) might mean that newClip(…) would be available.

7 b0 Acct Gm Gm(String) alert(String) newClip(String) nid Acct(String) alert(String) getID() ac 1 b1 Acct Hm Hm(String) alert(String) popUp(String) nid Acct(String) alert(String) getID() jp2 b0 a Workaround: check the real type. If we insist on calling newClip at all costs, then we need to explicitly downward-cast and/or to declare fresh variables of the right apparent type (Gm, not Acct). To assign correctly to these fresh variables, we need to check the real type: if ( a instanceof Gm) { Gm newG= (Gm) a; … } Acct need this downward cast (can’t just wedge “big” class into small)

8 Example public class Acct { // If Acct is a Gm, apply newClip, o.w, do nothing. (instance method just for lecture) public void tryNewClip(Acct a, String msg) { if ( ! ) return; // a is a Gm return g.newClip(msg); } Apparent type of a: Acct Real type of a: Gm Gm(String) alert(String) newClip(String) b0 Acct Gm nid Acct(String) alert(String) getID() tryNewClip(Acct, String) ac1 tryNewClip: 1b1 a b0 g Acct Gm // downward castGm g= (Gm) a ; Here, (Hm) a would lead to a runtime error. Don’t try to cast an object to something that it is not! (a instanceof Gm)

9 The correct way to write method equals public class Acct { … /** = “h is an Acct with the same values in its fields as this Acct” */ public boolean equals (Object h) { if (!(h instanceof Acct)) return false; Acct a= (Acct) h; // why? b/c Objects don’t // have nids return nid.equals(a.nid); } Acct b0 Gm(String) alert(String) newClip(String) Gm nid Acct(String) alert(String) getID() equals(Object) ac1 Object equals(Object) Note that method equals should take arbitrary Objects as arguments. Method equals helps prevent addition of duplicates to lists, etc. [delete some stray methods  ]