Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Understanding Casting, Function equals, and Array Concepts, Slides of Object Oriented Programming

An overview of casting between classes, static and dynamic types, and the use of the function equals in java. It also covers arrays, including declaration, assignment, and accessing elements. Slides and text from a java programming course.

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(10)

112 documents

1 / 21

Toggle sidebar

Related documents


Partial preview of the text

Download Java Programming: Understanding Casting, Function equals, and Array Concepts and more Slides Object Oriented Programming in PDF only on Docsity! Casting; function equals docsity.com Overview ref in text and JavaSummary.pptx • Quick look at arrays slide 50-55 • Casting among classes C.33-C.36 (not good) slide 34-41 • Static/Dynamic types (apparent/real types) slide 34-41 • Operator instanceof slide 40 • Function equals slide 37-41 Homework. Learn about while/ for loops in Java. Look in text. while ( <bool expr> ) { … } // syntax for (int k= 0; k < 200; k= k+1) { … } // example docsity.com Which function is called by v[0].toString() ? Remember, partition Object contains toString() Which function is called? a0 null a1v 0 1 2 a0 Animal CatCat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal DogDog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 docsity.com Each element v[k] is of type Animal. Its declared type: static type —known at compile-time apparent type Static/apparent type a0 null a1v 0 1 2 a0 Animal CatCat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal DogDog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 Should this call be allowed? Should program compile? v[0].getWeight() docsity.com Each element v[k] is of (static) type Animal. From v[k], see only what is in partition Animal and partitions above it. View of object from static type a0 null a1v 0 1 2 a0 Animal CatCat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 a1 Animal DogDog(String, int) getNoise() toString() age Animal(String, int) isOlder(Animal) 6 getWeight() not in class Animal or Object. Calls are illegal, program does not compile: v[0].getWeight() v[k].getWeight() Components still in lower partitions, but can’t see them Animal docsity.com age Animal(String, int) isOlder(Animal) Explicit casts: unary prefix operators a0 Animal CatCat(String, int) getNoise() toString() getWeight() 5 c a0 Cat Object equals() … You may cast an object to the name of any partition that occurs within it —and to nothing else. a0 maybe cast to Object, Animal, Cat. An attempt to cast it to anything else causes an exception (Cat) c (Object) c (Animal) (Animal) (Cat) (Object) c These casts don’t take any time. The object does not change. It’s a change of perception docsity.com Static/dynamic types a1 Animal DogDog(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 age > h.age; } h a1 Animal Static or apparent type of h is Animal. Syntactic property Determines at compile-time what components can be used: those available in Animal Dynamic or real type of h is Dog. Semantic/runtime property If a method call is legal, dynamic type determines which one is called (overriding one) docsity.com Components used from h a1 Animal DogDog(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 age > h.age; } h a1 Animal h.toString() OK —it’s in class Object partition h.isOlder(…) OK —it’s in Animal partition h.getWeight() ILLEGAL —not in Animal partition or Object partition By overriding rule, calls toString() in Cat partition docsity.com Function equals public class Object { /** Return true iff this object is the same as ob */ public boolean equals(Object b) { return this == b; } } x.equals(y) is same as x == y except when x is null! y ? Object x ? Object This gives a null-pointer exception: null.equals(y) docsity.com Overriding function equals Override function equals in a class to give meaning to: “these two (possibly different) objects of the class have the same values in some of their fields” For those who are mathematically inclined, like any equality function, equals should be reflexive, symmetric, and transitive. Reflexive: b.equals(b) Symmetric: b.equals(c) = c.equals(b) Transitive: if b.equals(c) and c.equals(d), then b.equals(d) docsity.com Function equals in class Animal Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) 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; } toString() 1. Because of h is an Animal in spec, need the test h instanceof Animal docsity.com Why can’t the parameter type be Animal? Animal a0 name age Animal(String, int) equals() toString() … Object equals(Object) public class Animal { /** = “h is an Animal with the same values in its fields as this Animal” */ public boolean equals (Animal h) { if (!(h instanceof Animal)) return false; Animal ob= (Animal) h; return name.equals(ob.name) && age == ob.age; } toString() What is wrong with this? docsity.com Recitation this week: VERY important Recitation this week is about abstract classes interfaces Learn: • Why we may want to make a class abstract • Why we may want to make a method abstract • An interface is like a very restricted abstract class, with different syntax for using it. Don’t miss recitation docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved