CS100J 21 February 2005 Casting About

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 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.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Java bootcamp Spring Java Bootcamp 2004 Expectations: We assume that you have studied C, C++, or Java and know about the following: Variables and.
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.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
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.
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 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
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.
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,
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
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.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Java Unit 11: Inheritance I
Lecture 10 Review of classes
Class Structure 15-Jun-18.
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
Sorry these slides weren’t available last evening!
CS100J Final Class on Classes 22 September 2005
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
More inheritance, Abstract Classes and Interfaces
Creating Objects in a Few Simple Steps
slides created by Ethan Apter
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
CS18000: Problem Solving and Object-Oriented Programming
slides created by Ethan Apter
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
CS100J Classes, stepwise refinement 21 September 2005
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

CS100J 21 February 2005 Casting About Casting between classes Apparent and real classes. Operator instanceof The class hierarchy function equals Study Secs 4.2 and 4.3 in text Procrastination Leave nothing for to-morrow that can be done to-day. Lincoln How does a project get a year behind schedule? One day at a time. Fred Brooks I don't wait for moods. You accomplish nothing if you do that. Your mind must know it has got to get down to work. Pearl S. Buck When I start a new project, I procrastinate immediately so that I have more time to catch up. Gries Buy a poster with the procrastinator’s creed here: http://www.art.com/asp/sp-asp/_/pd--10001845/Procrastinators_Creed.htm

About Prelim 1 After today, you have learned all the basics of classes, and done extremely well. Be proud of yourselves. Today’s lecture, on casting, is not on prelim 1. The prelim requires that you: Know terminology (that means knowing what things mean). The handout about the prelim summarizes the terms and their meanings. Draw folders (instances, objects) of a class. Be able to execute program segments yourself, drawing objects as necessary. Write a class or subclass definition, including its constructors. Be able to write a testing method in a Junit class (as on A2). Be able to write simple functions that manipulate Strings. Good way to study is to practice: do many exercises on pp. 100-103. Write them, put them into DrJava, and test them!!!

Class Animal We put each method on one line to save space on the slide. Don’t do it in your program. public class Animal { private String name; // name of the animal private int age; // age of animal /** Constructor: an Animal with name n, age a */ public Animal(String n, int a) { name= n; age= a; } /** = "this Animal is older than h" */ public boolean isOlder(Animal h) { return this.age > h.age; } /** = the noise that the animal makes -- "" in class Animal */ public String getNoise () { return ""; } /** = the name of this Animal */ public String getName() { return name; } /** = a description of this Animal */ public String toString() { return "Animal " + name + ", age " + age; } } a0 Animal name age Animal(String, int) isOlder(Animal) getNoise() getName() toString()

Class Cat /** An instance is a cat */ public class Cat extends Animal { /** Constructor: a Cat with name n and age a */ public Cat(String n, int a) { super(n, a); } /** = the noise this cat makes */ public String getNoise() { return "meow"; } /** = a description of this Cat */ public String toString() { return super.toString() + ", noise " + getNoise(); } /** = weight of Cat */ public int getWeight() { return 20; } a0 Animal Cat name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() Cat(String, int) getNoise() toString() getWeight()

Casting up the class hierarchy Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 Object Animal Dog Cat You know about casts like (int) (5.0 / 7.5) (double) 6 double d= 5; // automatic cast We now discuss casts up and down the class hierarchy. Animal h= new Cat(“N”, 5); Cat c= (Cat) h; a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6

Implicit casting up the class hierarchy Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return this.age > h.age; } } Object Animal Dog Cat c= new Cat(“C”, 5); d= new Dog(“D”, 6); c.isOlder(d) ????? a1 is cast from Dog to Animal, automatically Casts up the hierarachy done automatically Upward automatic casts make sense. Here, any Dog is an Animal a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 isOlder: 1 a0 h Animal a1

Implicit casting up the class hierarchy 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 this.age > h.age; } } Two new terms to learn! c= new Cat(“C”, 5); d= new Dog(“D”, 6); c.isOlder(d) --what is its value? Real type of h: Doc (type of object a1). Semantic property. The class-type of the folder whose name is currently in h. isOlder: 1 a0 h a1 Animal Apparent type of h. Syntactic property. The type with which h is defined. Apparently, h is an Animal, but really, it’s a Dog.

What components can h reference? name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() Animal public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return this.age > h.age; } } Cat c= new Cat(“C”, 5); d= new Dog(“D”, 6); d.isOlder(c) Cat(String, int) getNoise() toString() getWeight() isOlder: 1 a1 h a0 Animal What can isOlder reference in object h? Determined by the apparent type: Only components in partition Animal (and above)!!! h.getWeight() is illegal. Syntax error. Apparent type of h: Animal Real type of h: Cat

What method is called by h.toString() ? name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() Animal public class Animal { public boolean isOlder(Animal h) { String s= h.toString(); return this.age > h.age; } } c= new Cat(“C”, 5); d= new Dog(“D”, 6); d.isOlder(c) Cat Cat(String, int) getNoise() toString() getWeight() Determined by the real type: The overriding toString() in Cat. isOlder: 1 a1 h a0 s Apparent type of h: Animal Real type of h: Cat What method is called by h.toString() ?

Explicit cast down the hierarchy name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() 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 return c.getWeight(); } (h instanceof Cat) Cat Cat(String, int) getNoise() toString() getWeight() // downward cast int c= (Cat) h ; Object Animal Dog Cat Here, (Dog) h would lead to a runtime error. Don’t try to cast an object to something that it is not! isOlder: 1 a1 h a0 c a0 Animal Cat Apparent type of h: Animal Real type of h: Cat

The correct way to write method equals public class Animal { /** = “h is a non-null Animal with the same values in its fields as this Animal */ public boolean equals (Object h) { if (h == null) return false; if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return this.name.equals(ob.name) && this.age == ob.name; } Animal a0 Cat(String, int) getNoise() toString() getWeight() Cat name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() Object equals(Object) Object Animal Dog Cat