1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.

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;
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
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.
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.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
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.
CS 1110 Prelim II: Review Session 1. Exam Info Prelim II: 7:30–9:00PM, Tuesday, 8 November, Baker Lab 200, 219, 119 Look at the previous Prelims Arrive.
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.
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.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
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
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.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
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.
1 Project 15: Animals Using Derived Classes and Inheritance.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
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
Lecture 10 Review of classes
CS100J 21 February 2005 Casting About
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 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
slides created by Ethan Apter
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
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:

1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof 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 4.The class hierarchy 5.function equals Study Secs 4.2 and 4.3 in text Buy a poster with the procrastinator’s creed here:

2 Class Animal 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; } } We put each method on one line to save space on the slide. Don’t do it in your program. a0 Animal name age Animal(String, int) isOlder(Animal) getNoise() getName() toString()

3 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; } } Cat(String, int) getNoise() toString() getWeight() a0 Animal Cat name age Animal(String, int) isOlder(Animal) getNoise() getName() toString()

4 Casting up the class hierarchy 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 ObjectAnimal DogCat 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;

5 Implicit casting up the class hierarchy public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return this.age > h.age; } } c= new Cat(“C”, 5); d= new Dog(“D”, 6); c.isOlder(d) ????? 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 isOlder: 1a0 h AnimalObject Animal DogCat 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

6 Implicit casting up the class hierarchy public class Animal { /** = "this is older than h" */ public boolean isOlder(Animal h) { return this.age > h.age; } } c= new Cat(“C”, 5); d= new Dog(“D”, 6); c.isOlder(d) --what is its value? a1 Animal Dog Dog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 isOlder: 1a0 h a1 Animal Apparent type of h. Syntactic property. The type with which h is defined. Real type of h: Doc (type of object a1). Semantic property. The class-type of the folder whose name is currently in h. Two new terms to learn! Apparently, h is an Animal, but really, it’s a Dog.

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

8 What method is called by h.toString() ? 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) isOlder: 1a1 h a0 Apparent type of h: Animal Real type of h: Cat What method is called by h.toString() ? Cat(String, int) getNoise() toString() getWeight() a0 Animal Cat name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() s Determined by the real type: The overriding toString() in Cat.

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

10 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; } Object Animal DogCat Animal a0 Cat(String, int) getNoise() toString() getWeight() Cat name age Animal(String, int) isOlder(Animal) getNoise() getName() toString() Object equals(Object)