1 CS1110 9 October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.

Slides:



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

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.
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 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
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.
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.
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.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
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.
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.
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.
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.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
A: A: double “4” A: “34” 4.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
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.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
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.
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Java Unit 11: Inheritance I
CS100J 21 February 2005 Casting About
CS1110 Classes, stepwise refinement 23 Sep 2009
Lecture 17: Polymorphism (Part II)
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
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
Variables as Remote Control
Overloading and Constructors
Review Operation Bingo
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.
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Building Java Programs
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
slides created by Ethan Apter
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
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
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 CS October 2008 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 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 QUESTION: Which method is called by v.get(0).toString() ? 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 Object Animal DogCat the class hierarchy:

3 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

4 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!

5 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 declare 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!

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

7 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

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

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

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

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

12 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 name.equals(ob.name) && 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)