Chapter 10: Detecting types ● Sometimes we need to check types of objects inside program: – A typical example is in shape hierarchy where you want to know.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Java Review Interface, Casting, Generics, Iterator.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Algorithm Programming Java Reflection Mechanism Bar-Ilan University תשס"ז Moshe Fresko.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Based on OOP with Java, by D.J. Barnes 1 Review 4 View classes as modules Encapsulate operations 4 View classes as struct types Encapsulate data 4 View.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 4: Initialization and cleanup ● How to initializa an object ● Method overloading ● Cleanup: Finalization and gaurbage collection.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Lecture 2 Exception handling Advanced Java Programming 1 dr inż. Wojciech Bieniecki
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Programming in Java CSCI-2220 Object Oriented Programming.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces and Polymorphism CS 162 (Summer 2009).
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Classes Revisited Chapter 8.
Reflection Mehdi Einali Advanced Programming in Java 1.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
BY:- TOPS Technologies
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
7: Polymorphism Upcasting revisited Forgetting the object type
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
null, true, and false are also reserved.
Advanced Programming in Java
Java Programming Language
Advanced Programming in Java
Lecture 18: Polymorphism (Part II)
Chapter 11 Inheritance and Polymorphism Part 2
Java Programming: From the Ground Up
Presentation transcript:

Chapter 10: Detecting types ● Sometimes we need to check types of objects inside program: – A typical example is in shape hierarchy where you want to know exact type of a shape (e.g. Circle, square, etc.) and do some processing based on the exact type – Another typical situation is when you want to fill an object with some data but you don't want to define all the possible classes in your program ● Two ways to do this in Java: – Traditional RTTI: all types are available at compile time – Reflection: finding class information at run-time

The need for RTTI ● In a class hierarchy we often refer to the derived class objects using a base type reference ● As discussed before this with the polymorphism helps us to extend programs more easy ● However, we may sometime need to check the exact type of an object to perform specific procssesing

A basic form of RTTI (downcasting) public class Shapes { private static Test monitor = new Test(); public static void main(String[] args) { // Array of Object, not Shape: Object[] shapeList = { new Circle(), new Square(), new Triangle() }; for(int i = 0; i < shapeList.length; i++) ((Shape)shapeList[i]).draw(); // Must cast monitor.expect(new String[] { "Circle.draw()", "Square.draw()", "Triangle.draw()" }); } } ///:~ class Shape { void draw() { System.out.println(this + ".draw()"); } } class Circle extends Shape { public String toString() { return "Circle"; } } class Square extends Shape { public String toString() { return "Square"; } } class Triangle extends Shape { public String toString() { return "Triangle"; } }

Class object ● Class object contains information about the class ● For each class in a program a class object is created ● When in a program we create an object (using new), JVM checks to see if the class object for that class is in memory. If it is not, the corresponding.class file is loaded and a class object is created.

Example of class loading class Candy { static { System.out.println("Loading Candy"); } class Gum { static { System.out.println("Loading Gum"); } class Cookie { static { System.out.println("Loading Cookie"); } public class SweetShop { private static Test monitor = new Test(); public static void main(String[] args) { System.out.println("inside main"); new Candy(); System.out.println("After creating Candy"); try { Class.forName("Gum"); } catch(ClassNotFoundException e) { System.out.println("Couldn't find Gum"); } System.out.println("After Class.forName(\"Gum\")"); new Cookie(); System.out.println("After creating Cookie"); monitor.expect(new String[] { "inside main", "Loading Candy", "After creating Candy", "Loading Gum", "After Class.forName(\"Gum\")", "Loading Cookie", "After creating Cookie" }); }

Checking before a cast ● With instanceOf() we can check if an object is an instance of a specific class ● This code checks to see if x is of type Dog before casting it to a Dog class. ● If we don't have such a check before casting, there might be a possibility to have a ClassCastException. if(x instanceof Dog) ((Dog)x).bark();

An example of using instanceOf ● Suppose we want to write a program that counts the number of each pet type ● First we define some pet types package c10; public class Pet {} ///:~ //: c10:Dog.java package c10; public class Dog extends Pet {} ///:~ //: c10:Pug.java package c10; public class Pug extends Dog {} ///:~ //: c10:Cat.java package c10; public class Cat extends Pet {} ///:~ //: c10:Rodent.java package c10; public class Rodent extends Pet {} ///:~ //: c10:Gerbil.java package c10; public class Gerbil extends Rodent {} ///:~ //: c10:Hamster.java package c10; public class Hamster extends Rodent {} ///:~

An example of using instanceOf (cont.) ● Now we define a utility to store counts for each type public class AssociativeArray { private static Test monitor = new Test(); private Object[][] pairs; private int index; public AssociativeArray(int length) { pairs = new Object[length][2]; } public void put(Object key, Object value) { if(index >= pairs.length) throw new ArrayIndexOutOfBoundsException(); pairs[index++] = new Object[] { key, value }; } public Object get(Object key) { for(int i = 0; i < index; i++) if(key.equals(pairs[i][0])) return pairs[i][1]; throw new RuntimeException("Failed to find key"); } public String toString() { String result = ""; for(int i = 0; i < index; i++) { result += pairs[i][0] + " : " + pairs[i][1]; if(i < index - 1) result += "\n"; } return result; } public static void main(String[] args) { AssociativeArray map = new AssociativeArray(6); map.put("sky", "blue"); map.put("grass", "green"); map.put("ocean", "dancing"); map.put("tree", "tall"); map.put("earth", "brown"); map.put("sun", "warm"); try { map.put("extra", "object"); // Past the end } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Too many objects!"); } System.out.println(map); System.out.println(map.get("ocean")); monitor.expect(new String[] { "Too many objects!", "sky : blue", "grass : green", "ocean : dancing", "tree : tall", "earth : brown", "sun : warm", "dancing" }); }

An example of using instanceOf (cont.) ● The main class public class Counter { int i; public String toString() { return Integer.toString(i); } } ///:~ public class PetCount { private static Test monitor = new Test(); private static Random rand = new Random(); static String[] typenames = { "Pet", "Dog", "Pug", "Cat", "Rodent", "Gerbil", "Hamster", }; // Exceptions thrown to console: public static void main(String[] args) { Object[] pets = new Object[15]; try { Class[] petTypes = { Class.forName("c10.Dog"), Class.forName("c10.Pug"), Class.forName("c10.Cat"), Class.forName("c10.Rodent"), Class.forName("c10.Gerbil"), Class.forName("c10.Hamster"), }; for(int i = 0; i < pets.length; i++) pets[i] = petTypes[rand.nextInt(petTypes.length)].newInstance(); } catch(InstantiationException e) { System.out.println("Cannot instantiate"); System.exit(1); } catch(IllegalAccessException e) { System.out.println("Cannot access"); System.exit(1); } catch(ClassNotFoundException e) { System.out.println("Cannot find class"); System.exit(1); }

An example of using instanceOf (cont.) ● The main class AssociativeArray map = new AssociativeArray(typenames.length); for(int i = 0; i < typenames.length; i++) map.put(typenames[i], new Counter()); for(int i = 0; i < pets.length; i++) { Object o = pets[i]; if(o instanceof Pet) ((Counter)map.get("Pet")).i++; if(o instanceof Dog) ((Counter)map.get("Dog")).i++; if(o instanceof Pug) ((Counter)map.get("Pug")).i++; if(o instanceof Cat) ((Counter)map.get("Cat")).i++; if(o instanceof Rodent) ((Counter)map.get("Rodent")).i++; if(o instanceof Gerbil) ((Counter)map.get("Gerbil")).i++; if(o instanceof Hamster) ((Counter)map.get("Hamster")).i++; } // List each individual pet: for(int i = 0; i < pets.length; i++) System.out.println(pets[i].getClass()); // Show the counts: System.out.println(map); monitor.expect(new Object[] { new TestExpression("% class c10\\."+ "(Dog|Pug|Cat|Rodent|Gerbil|Hamster)", pets.length), new TestExpression( "% (Pet|Dog|Pug|Cat|Rodent|Gerbil|Hamster)" + " : \\d+", typenames.length) }); }

Using class literals public class PetCount2 { private static Random rand = new Random(); public static void main(String[] args) { Object[] pets = new Object[15]; Class[] petTypes = { // Class literals: Pet.class, Dog.class, Pug.class, Cat.class, Rodent.class, Gerbil.class, Hamster.class };

for(int i = 0; i < pets.length; i++) { Object o = pets[i]; // Using Class.isInstance() to eliminate // individual instanceof expressions: for(int j = 0; j < petTypes.length; ++j) if(petTypes[j].isInstance(o)) ((Counter)map.get(petTypes[j].toString())).i++; } Class.isInstance – dynamic instance of

InstanceOf vs. Class equivalence ● instanceof says “are you this class, or a class derived from this class?” On the other hand, if you compare the actual Class objects using ==, there is no concern with inheritance—it’s either the exact type or it isn’t. public static void main(String[] args) { test(new Base()); test(new Derived()); monitor.expect(new String[] { "Testing x of type class c10.Base", "x instanceof Base true", "x instanceof Derived false", "Base.isInstance(x) true", "Derived.isInstance(x) false", "x.getClass() == Base.class true", "x.getClass() == Derived.class false", "x.getClass().equals(Base.class)) true", "x.getClass().equals(Derived.class)) false", "Testing x of type class c10.Derived", "x instanceof Base true", "x instanceof Derived true", "Base.isInstance(x) true", "Derived.isInstance(x) true", "x.getClass() == Base.class false", "x.getClass() == Derived.class true", "x.getClass().equals(Base.class)) false", "x.getClass().equals(Derived.class)) true" }); } } ///:~ public class FamilyVsExactType { private static Test monitor = new Test(); static void test(Object x) { System.out.println("Testing x of type " + x.getClass()); System.out.println("x instanceof Base " + (x instanceof Base)); System.out.println("x instanceof Derived " + (x instanceof Derived)); System.out.println("Base.isInstance(x) " + Base.class.isInstance(x)); System.out.println("Derived.isInstance(x) " + Derived.class.isInstance(x)); System.out.println("x.getClass() == Base.class " + (x.getClass() == Base.class)); System.out.println("x.getClass() == Derived.class " + (x.getClass() == Derived.class)); System.out.println("x.getClass().equals(Base.class)) "+ (x.getClass().equals(Base.class))); System.out.println( "x.getClass().equals(Derived.class)) " + (x.getClass().equals(Derived.class))); }

Some usefull methods of Class interface HasBatteries {} interface Waterproof {} interface Shoots {} class Toy { // Comment out the following default constructor // to see NoSuchMethodError from (*1*) Toy() {} Toy(int i) {} } class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots { FancyToy() { super(1); } } public class ToyTest { private static Test monitor = new Test(); static void printInfo(Class cc) { System.out.println("Class name: " + cc.getName() + " is interface? [" + cc.isInterface() + "]"); } public static void main(String[] args) { Class c = null; try { c = Class.forName("FancyToy"); } catch(ClassNotFoundException e) { System.out.println("Can't find FancyToy"); System.exit(1); } printInfo(c); Class[] faces = c.getInterfaces(); for(int i = 0; i < faces.length; i++) printInfo(faces[i]); Class cy = c.getSuperclass(); Object o = null; try { // Requires default constructor: o = cy.newInstance(); // (*1*) } catch(InstantiationException e) { System.out.println("Cannot instantiate"); System.exit(1); } catch(IllegalAccessException e) { System.out.println("Cannot access"); System.exit(1); } printInfo(o.getClass()); monitor.expect(new String[] { "Class name: FancyToy is interface? [false]", "Class name: HasBatteries is interface? [true]", "Class name: Waterproof is interface? [true]", "Class name: Shoots is interface? [true]", "Class name: Toy is interface? [false]" }); }

Reflection: runtime class information ● RTTI works when we now types of classes we want to work with ● Sometimes we need to work with objects that we don't have any information about their class ● Java provides a mechanism for such a situation which is named as reflection ● One sample example is component-based programming, with the use of Rapid Application Development (RAD) ● For such tools we need to know what attributes, methods and events is defined for an object. These are shown to the developer and (s)he can set the values or overide the event handles ● Other typical usage is in Remote Method Invocation (RMI)

Reflection example for(int i = 0; i < ctor.length; i++) System.out.println( p.matcher(ctor[i].toString()).replaceAll("")); lines = m.length + ctor.length; } else { for(int i = 0; i < m.length; i++) if(m[i].toString().indexOf(args[1]) != -1) { System.out.println( p.matcher(m[i].toString()).replaceAll("")); lines++; } for(int i = 0; i < ctor.length; i++) if(ctor[i].toString().indexOf(args[1]) != -1) { System.out.println(p.matcher( ctor[i].toString()).replaceAll("")); lines++; } } catch(ClassNotFoundException e) { System.out.println("No such class: " + e); } } ///:~ import java.lang.reflect.*; import java.util.regex.*; public class ShowMethods { private static final String usage = "usage: \n" + "ShowMethods qualified.class.name\n" + "To show all methods in class or: \n" + "ShowMethods qualified.class.name word\n" + "To search for methods involving 'word'"; private static Pattern p = Pattern.compile("\\w+\\."); public static void main(String[] args) { if(args.length < 1) { System.out.println(usage); System.exit(0); } int lines = 0; try { Class c = Class.forName(args[0]); Method[] m = c.getMethods(); Constructor[] ctor = c.getConstructors(); if(args.length == 1) { for(int i = 0; i < m.length; i++) System.out.println( p.matcher(m[i].toString()).replaceAll(""));