Topics

6/recent/ticker-posts

Different types of classes in java part 4

Types of classes in  java part 4 



anonymous class/unnamed class:

  • Writing class body without class declaration is called as anonymous class. Writing class body during class object creation. "Implementing all the methods of interface while instantiating interface". 
  • If interface contains abstract methods, to interface we will write one or many sub classes. We can instantiate and use sub class anywhere in our system. But anonymous class aim is zero re-usability of class because at the time of instantiation itself we will implement all the interface methods so that those method implementations are available in only one occasion/instance. 
  • If interface implementation do not  want to be reusable, but want to be usable only once then we will implement anonymous class.


Zeroth Anonymous example:

interface Intf1 {

v m1();

v m2();

}


class Intf1ACExample {

public static void main(String rags[]) {

// we are not writing sub class to interface

// we are instantiating interface

// in the interface instantiation body, implement all interface methods in that body

// such a interface instantiation body is called as anonymous class

Intf1 i1=new Intf1() {

public void m1() {}

public void m2() {}

};

i1.m1();

i1.m2();

}


First Anonymous example:

// AnonymousInterface.java

interface AnonymousInterface {

   void method1();

   void method2();

}


// STDOUTPrinterClass.java

public class STDOUTPrinterClass implements AnonymousInterface {

    public void method1() {

   System.out.println("method1 is printing on STDOUT");

}

    public void method2() {

   System.out.println("method2 is printing on STDOUT");

}

}

// FilePrinterClass.java

public class FilePrinterClass implements AnonymousInterface {

    public void method1() {

   System.out.println("method1 is printing on File");

}

    public void method2() {

   System.out.println("method2 is printing on File");

}

}


// AnonymousInterfaceUsedClass.java

public class AnonymousInterfaceUsedClass {

   public void printMessage(AnonymousInterface ai) {

       ai.method1();

       ai.method2();

   }

}


// AnonymousInterfaceClient1.java

public class AnonymousInterfaceClient1 {


   public static void main(String rags[]) {

       // STDOUTPrinterClass spc=new STDOUTPrinterClass();

       FilePrinterClass spc=new FilePrinterClass();

   AnonymousInterfaceUsedClass aiuc=new AnonymousInterfaceUsedClass();

   aiuc.printMessage(spc);

   }

}

 

// AnonymousInterfaceClient2.java

public class AnonymousInterfaceClient2 {


   public static void main(String rags[]) {

       // STDOUTPrinterClass spc=new STDOUTPrinterClass();

       // FilePrinterClass spc=new FilePrinterClass();

       // AnonymousInterfaceUsedClass aiuc=new AnonymousInterfaceUsedClass();

   // aiuc.printMessage(spc);


   // instead of instantiating interface sub classes and pass that object as argument into printMessage() method of AnonymousInterfaceUsedClass. We will directly instantiate interface in printMessage() method and implement both the interface methods in instantiation body.

   

       AnonymousInterfaceUsedClass aiuc=new AnonymousInterfaceUsedClass();

   aiuc.printMessage(new AnonymousInterface() {

      public void method1() {

     System.out.println("method1 is printing output from anonymous class/interface object creation body");

  }

      public void method2() {

     System.out.println("method2 is printing output from anonymous class/interface object creation body");

  }

   });

   }

   

}

 

/*

anonymous class:

new AnonymousInterface() {

      void method1() {

     System.out.println("method1 is printing output from anonymous class/interface object creation body");

  }

      void method2() {

     System.out.println("method2 is printing output from anonymous class/interface object creation body");

  }

}


or

AnonymousInterface ai=new AnonymousInterface() {

      void method1() {

     System.out.println("method1 is printing output from anonymous class/interface object creation body");

  }

      void method2() {

     System.out.println("method2 is printing output from anonymous class/interface object creation body");

  }

};


AnonymousInterfaceUsedClass aiuc=new AnonymousInterfaceUsedClass();

aiuc.printMessage(ai);

*/ 

 

  •  Wherever re-usability is not required there we will implement and use anonymous class. Anonymous class can be passed only as argument into method. 
  • It cannot be written or saved anywhere outside and reused.


 Inner class and Anonymous class are together called as Nested Classes.


Second anonymous class example:

public interface ActionListener {

public void actionPerformed(ActionEvent ae);

}


public class AddActionListener implements ActionListener {

public void actionPerformed(ActionEvent ae) {

add two operands

}

}


public class SubActionListener implements ActionListener {

public void actionPerformed(ActionEvent ae) {

sub two operands

}

}


public class CalcFrame extends Frame {

int firstOperand;

int secondOperand;

int result;

Button zeroB, firstB,..........;

Button addB, subB, mulB;

public CalcFrame() {

create button objects

add them to layout

// adding event hander classes to buttons

// addB.addActionListener(new AddActionListener());

// subB.addActionListener(new SubActionListener()); 


addB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

result=firstOperand + secondOperand;

}

});

subB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

result=firstOperand - secondOperand;

}

});

}

}


For more details on java please contact Ph.No : 9848111288

ActiveNet Hyderabad, 


Post a Comment

0 Comments