Presentation is loading. Please wait.

Presentation is loading. Please wait.

NESTED CLASSES REFLECTION PROXIES.

Similar presentations


Presentation on theme: "NESTED CLASSES REFLECTION PROXIES."— Presentation transcript:

1 NESTED CLASSES REFLECTION PROXIES

2 Nested Class A class declared inside a class is known as nested class.
It can access all the members of outer class including private data members and methods.

3 Nested classes There are two types of nested classes
non-static and static nested classes. The non-static nested classes are also known as inner classes. non-static nested class(inner class) a)Member inner class b)Anonmynous inner class c)Local inner class static nested class

4 Nested Class Types

5 Non- Static /Inner Classes
An inner class is a class that is defined inside another class. Inner classes let you make one class a member of another class. Just as classes have member variables and methods, a class can also have member classes.

6 Inner Class Example Code in Java
class OuterClass { class Innerclass //Members of Inner Class } // Members of Outer Class

7 Compile javac OuterClass.java Two class files created as shown below OuterClass.class OuterClass$InnerClass.class

8 All the classes so far have been “top level”
It is possible (and useful) to define a class inside another class The usual access modifiers (public, protected, private) can be used Inner classes were not in Java 1.0 They had to be added in later As a result, inner classes are not as well done as some other aspects of the language

9 Instantiating an Inner Class from Within Code in the Outer Class
class MyOuter { private int x = 7; MyInner mi = new MyInner(); class MyInner public void seeOuter() System.out.println("Outer x is " + x); } public static void main(String arg[]) MyOuter mo=new MyOuter(); mo.mi.seeOuter(); Output: Outer x is 7

10 1. Member inner class A class that is declared inside a class but outside a method is known as member inner class. Invocation of Member Inner class From within the class From outside the class

11 Example of member inner class that is invoked inside a class
class Outer{    private int data=30;    class Inner{     void msg(){ System.out.println("data is "+data);}    }        void display(){     Inner in=new Inner();     in.msg();    public static void main(String args[]){     Outer obj=new Outer();     obj.display();   }    Output: data is 30

12 Example of member inner class that is invoked outside a class
class Outer    private int data=30;     class Inner{      void msg() { System.out.println("data is"+data); }     }  }     class Test   public static void main(String args[]) {     Outer obj=new Outer();     Outer.Inner in=obj.new Inner();     in.msg();     }     Output:data is 30

13 2)Anonymous inner class
A class that have no name is known as anonymous inner class. Anonymous class can be created by: Class (may be abstract class also) Interface

14 Program of anonymous inner class by abstract class
import java.io.*; abstract class Person{ abstract void eat(); } class Emp{ public static void main(String args[]) { Person p=new Person() void eat() System.out.println("nice fruits"); }; p.eat();

15 What happens behind this code?
Person p=new Person() { void eat() System.out.println("nice fruits"); } }; } }

16 A class is created but its name is decided by the compiler which extends the Person class and provides the implementation of the eat() method. An object of Annonymous class is created that is reffered by p reference variable of Person type. As you know well that Parent class reference variable can refer the object of Child class.

17 Program of anonymous inner class by interface
interface Eatable{ void eat(); } class Emp{ public static void main(String args[]){ Eatable e=new Eatable(){ public void eat(){System.out.println("nice fruits");} }; e.eat(); }

18 It performs two main tasks behind this code:
A class is created but its name is decided by the compiler which implements the Eatable interface and provides the implementation of the eat() method. An object of Annonymous class is created that is reffered by p reference variable of Eatable type. As you know well that Parent class reference variable can refer the object of Child class.

19 3)Local inner class A class that is created inside a method is known as local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside the method. Program of local inner class

20 class Simple{    private int data=30;//instance variable    void display(){     class Local{     void msg(){System.out.println(data);}     }    Local l=new Local();     l.msg();    }    public static void main(String args[]){     Simple obj=new Simple();     obj.display();   }  

21 static nested class A static class that is created inside a class is known as static nested class. It cannot access the non-static members. It can access static data members of outer class including private. static nested class cannot access non-static (instance) data member or method.

22 Program of static nested class that have instance method
class Outer{ static int data=30; static class Inner{ void msg(){System.out.println("data is "+data);} } public static void main(String args[]){ Outer.Inner obj=new Outer.Inner(); obj.msg(); } Output:data is 30

23 In Above example, you need to create the instance of static nested class because it has instance method msg(). But you don't need to create the object of Outer class because nested class is static and static properties, methods or classes can be accessed without object.

24 Program of static nested class that have static method
class Outer{     static int data=30;        static class Inner{      static void msg(){System.out.println("data is "+data);}     }         public static void main(String args[]){     Outer.Inner.msg();//no need to create the instance of  static nested class   }  

25 Reflection in Java Reflection is an advanced feature of the Java environment. It gives runtime information about objects, classes, and interfaces. java.lang.reflect

26 Reflection answers questions like:
Which class does an object belong to? What is the description of a given class name? What are the fields in a given class? What is the type of a field? What are the methods in a class? What are the parameters of a method? What are the constructors of a given class?

27 Reflection also lets you operate on objects and do things like:
Constructing an object using a given constructor Invoking an object's method using such-and-such parameters Assigning a value to an object's field Dynamically creating and manipulating arrays

28 Reflection Class – contd…
Java.lang.reflect String getName() – return the name of the item. int getModifiers()- return an integer, that describes the modifier. FIELDS METHODS CONSTRUCTORS

29 Fields Provide information about fields.
The getFields() method returns an array containing Field objects for the public fields. The getDeclaredField() method returns an array of Field objects for all fields. The methods return an array of length 0 if there are no such fields.

30 class Test { public int var1; private int var2; protected int var3;
public class FieldDemo { public static void main(String args[])throws Exception Class c=Class.forName("Test"); Field f[]=c.getFields(); Field fdec[]=c.getDeclaredFields(); System.out.println("public Fields:"); for(int i=0;i<f.length;i++) System.out.println(f[i]); System.out.println("All Fields:"); for(int i=0;i<fdec.length;i++) System.out.println(fdec[i]); } class Test { public int var1; private int var2; protected int var3; int var4; }

31 Output: public Fields: public int Test.var1 All Fields: public int Test.var1 private int Test.var2 protected int Test.var3 int Test.var4

32 Method Provides information about method
The getMethods() method return an array containing Method objects that give you all the public methods. The getDeclaredMethods () return all methods of the class or interface. This includes those inherited from classes or interfaces above it in the inheritance chain.

33 import java.lang.reflect.*;
class Test { public void method1() {} protected void method2() {} private void method3() {} void method4() {} }

34 public class MethodDemo
{ public static void main(String args[])throws Exception Class c=Class.forName("Test"); Method m[]=c.getMethods(); Method mdec[]=c.getDeclaredMethods(); System.out.println("public Methods of class Test & its Super class:"); for(int i=0;i<m.length;i++) System.out.println(m[i].getName()); System.out.println("All Methods:"); for(int i=0;i<mdec.length;i++) System.out.println(mdec[i].getName()); }

35 Output public Methods of class Test & its Super class: Method1 hashCode get Class wait equals notify notify All toString All Methods: method1 Method2 method3 method4

36 Constructor Provide information about constructors
getConstructors () method return an array containing Constructor objects that give you all the public constructors getDeclaredConstructors () method return all constructors of the class represented by the Class object.

37 Using Reflection to Analyze Objects at Run Time
f.set(obj, value) sets the field represented by f of the object obj to the new value. f.get(obj) returns an object whose value is the current value of the field of obj.

38 public class ConstructorDemo
{ public static void main(Stringargs[]) throws Exception A obj=new A(10,20); System.out.println("Before \n var1= +obj.var1); Field f1 = obj.getClass().getField("var1"); int v1 = f1.getInt(obj) + 1; f1.setInt(obj, v1); System.out.println("After \n var1= "+v1); System.out.println("Before \n var2= "+obj.var2); Field f2 = obj.getClass().getField("var2"); f2.set(obj,21); System.out.println("After \n var2= "+f2.get(obj)); } import java.lang.reflect.*; class A { public int var1,var2; A(int i, int j) var1=i; var2=j; }

39 Output: Before var1= 10 After var1= 11 var2= 20 var2= 21

40 PROXIES Proxy used to create new classes at runtime that implement a given set of interfaces The proxy class can create brand-new classes at runtime. In particular, the proxy class has the following methods:

41 Proxies – contd… All methods required by the specified interfaces;
All methods defined in the Object class (toString, equals, and so on). To create a proxy object, you use the newProxyInstance method of the Proxy class

42 Proxies can be used for many purposes, such as:
Routing method calls to remote servers; Associating user interface events with actions in a running program; Tracing method calls for debugging purposes.


Download ppt "NESTED CLASSES REFLECTION PROXIES."

Similar presentations


Ads by Google