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

Slides:



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

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.
Inheritance Writing and using Classes effectively.
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;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
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.
CS503: Fourth Lecture, Fall 2008 Advanced OOP and Lab. Michael Barnathan.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
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,
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
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.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
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.
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.
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.
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
Chapter 11 Inheritance and Polymorphism
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
Variables as Remote Control
Overloading and Constructors
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 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 February 2006 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: After today, you have learned ALL the basics of classes, and done extremely well. Be proud of yourselves.

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 QUESTION: Which method is called by v.get(0).toString() ? 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 a0 null a1 Vector v A.the one in the hidden partition for Object of a0 B.the one in partition Animal of a0 C.the one in partition Cat of a0 D.the one in partition Dog of a1 E.None of these

5 QUESTION: Should a call v.get(k).getWeight() be allowed (should the program compile)? 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 a0 null a1 Vector v A.Yes, because v[0] has that method. B.No, because v[2] doesn’t have that method. C.No, because that method isn’t available in Animal. D. None of these

6 QUESTION: Should a call v.get(k).getWeight() be allowed (should the program compile)? 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 a0 null a1 Vector v A.Yes, because v[0] has that method. B.No because v[2] doesn’t have that method. C.No, because that method isn’t available in Animal. Apparently, v[k] is an Animal!

7 The call v.get(k).getWeight() is illegal, and the program won’t compile, because: The apparent type of v[k], which is Animal, does not have (or inherit) a method getWeight. 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 a0 null a1 Vector v Apparently, v[k] is an Animal!

8 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;

9 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

10 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.

11 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.

12 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.

13 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)

14 The correct way to write method equals public class Animal { /** = “h is an Animal with the same values in its fields as this Animal */ public boolean equals (Object h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return this.name.equals(ob.name) && this.age == ob.age; } 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)