Advanced Programming in Java

Slides:



Advertisements
Similar presentations
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Advertisements

Sadegh Aliakbary Sharif University of Technology Fall 2012.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Programming Languages and Paradigms Object-Oriented Programming.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
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,
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Reflection Mehdi Einali Advanced Programming in Java 1.
BY:- TOPS Technologies
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Chapter 7: Cloning and RTTI
Summary prepared by Kirk Scott
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
Class Structure 15-Jun-18.
Java Primer 1: Types, Classes and Operators
CSE 413, Autumn 2002 Programming Languages
University of Central Florida COP 3330 Object Oriented Programming
Generics, Lambdas, Reflections
Continuing Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 Inheritance and Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
More inheritance, Abstract Classes and Interfaces
Advanced Programming in Java
Java Programming Language
Advanced Programming in Java
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CSCE 314: Programming Languages Dr. Dylan Shell
CMPE212 – Reminders Assignment 2 due next Friday.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Presentation transcript:

Advanced Programming in Java Sharif University of Technology Summer 2015

Agenda What is RTTI? Why we need it? Type information Java and Reflection Fall 2013 Sharif University of Technology

A Challenge Suppose you want to implement RPC or RMI What do you need? Socket Programming Serialization How do you invoke methods in other side? Fall 2013 Sharif University of Technology

Problem Suppose you should write a program It reads the name of a class And the name of one of its methods And all of its parameters The program creates an object from the specified class and invokes the specified method with specified parameters Fall 2013 Sharif University of Technology

Problem (2) How can you implement it? What is its application? RPC and RMI Object transfer to a web service Fall 2013 Sharif University of Technology

RTTI Runtime type information (RTTI) Allows you to discover and use type information while a program is running This feature is also called Reflection in java Fall 2013 Sharif University of Technology

RTTI (2) With RTTI, you can ask an object reference the exact type that it’s referring to. And you can get information about it its characteristics and capabilities Methods, constructors, fields, … And you can call its methods and get its properties Fall 2013 Sharif University of Technology

Solve the Problem How can you implement the requested program with RTTI? How can you simulate RPC and RMI? How can you send an object via web-service? Fall 2013 Sharif University of Technology

The Class Object How type information is represented at run time? This is accomplished through a special kind of object It is called the Class object it contains information about the class Java performs its RTTI using the Class object Fall 2013 Sharif University of Technology

Class Loader There’s one Class object for each class that is part of your program Each time you write and compile a new class, a single Class object is also created and stored, appropriately enough, in an identically named .class file To make an object of that class, JVM uses a subsystem called a class loader Fall 2013 Sharif University of Technology

How Classes are Loaded? A classes is loaded into the JVM dynamically upon the first use of the class When? when the program makes the first reference to a static member of that class The constructor is also a static method of a class! Even though the static keyword is not declared Instantiation: using the new operator  a reference to a static member (constructor) Fall 2013 Sharif University of Technology

Dynamic Loading A Java program isn’t completely loaded before it begins Pieces of it are loaded when necessary This is called Dynamic loading Different from many traditional languages Enables difficult or impossible behavior to duplicate in a statically loaded language like C++. Fall 2013 Sharif University of Technology

Default Class Loader The class loader first checks: Is the Class object for that type loaded? If not, class loader finds the .class file and loads it A customized class loader may load the class from a DB Fall 2013 Sharif University of Technology

First Example Method method = String.class.getMethod( "substring", int.class); Object value = method.invoke("Taghi Taghavi", 6); System.out.println((String)value); Fall 2013 Sharif University of Technology

Example Class c = Class.forName(args[0]); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); Fall 2013 Sharif University of Technology

More Reflection Class clazz = object.getClass(); Annotation[] annotations = clazz.getAnnotations(); Field[] fields = clazz.getFields(); Constructor[] constructors = clazz.getConstructors(); Fall 2013 Sharif University of Technology

Example package drawing; class MyClass{ String name; public MyClass(String name) { this.name = name; } public String getName() { return name; Fall 2013 Sharif University of Technology

Example (contd.) Class c = Class.forName("drawing.MyClass"); Constructor constructor = c.getConstructor(String.class); MyClass instance = (MyClass) constructor.newInstance("Ali Alavi"); Field field = instance.getClass().getDeclaredField("name"); field.set(instance, "Taghi Taghavi"); System.out.println(instance.getName()); Fall 2013 Sharif University of Technology

instanceof Operator if(x instanceof Dog) ((Dog)x).bark(); Tells you if an object is an instance of a particular type if(x instanceof Dog) ((Dog)x).bark(); Use instanceof before a downcast when you don’t have other information that tells you the type of the object; Otherwise, you may end up with a …? ClassCastException Fall 2013 Sharif University of Technology

instanceof void f(Object c){ if(c instanceof Serializable && c instanceof String) System.out.println("YES!"); } instanceof returns false if the reference is null interface class Fall 2013 Sharif University of Technology

instanceof vs. Class equivalence There’s an important difference between instanceof and the direct comparison of the Class objects But instanceof and islnstance() produce equivalent results if(c instanceof String) ... if(c.getClass().equals(String.class))... Fall 2013 Sharif University of Technology

Quiz! Fall 2013 Sharif University of Technology

Quiz public static void wow(ArrayList<String> list) { Method method = list.getClass().getMethod("add", Object.class); method.invoke(list, new Integer(2)); } public static void main(String args[]) { ArrayList<String> s = new ArrayList<String>(); wow(s); for (Object string : s) { System.out.println(string); Fall 2013 Sharif University of Technology

More on Reflection

How to Retrieve Class Object Compile time code (Hard coded) 1. ClassName.class Class clazz = Person.class; Runtime 2. Class.forName Class clazz = Class.forName("edu.sharif.ce.Rectangle"); 3. reference.getClass Object o = new Person(); Class clazz= o.getClass(); Fall 2013 Sharif University of Technology

Class is a Generic Class Example1 Class<Person> clazz = Person.class; Person p = clazz.newInstance(); No cast is needed Example2 Object o = new Person(); Class<? extends Object> c = o.getClass(); Fall 2013 Sharif University of Technology

Class and Generic Types What is wrong with this code? class GenericType<T>{ private T element; public void f(){ Class c2 = element.getClass(); Class c1 = T.class; } No generic type information at runtime Remember erasure OK Syntax Error Fall 2013 Sharif University of Technology

TYPE in Wrapper Classes Fall 2013 Sharif University of Technology

Changing the Accessibility! class MyClass{ private void privateMethod(){ } ... MyClass instance = new MyClass(); Method method = instance.getClass(). getDeclaredMethod("privateMethod"); method.setAccessible(true); method.invoke(instance); Fall 2013 Sharif University of Technology

Swap two integers public static void swap(Integer i, Integer j) { try { Integer lastJ = new Integer(j); Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(j, i); value.set(i, lastJ); value.setAccessible(false); } catch (Exception e) { e.printStackTrace(); } } Thanks to Mr. Soheil Hassas Yeganeh! OO cries on this capability Fall 2013 Sharif University of Technology

Fall 2013 Sharif University of Technology