Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Anonymous Classes, Interface Based, Class Based Anonymous Class
Previous Home Next

Anonymous classes in the Java programming language are the classes without names thus, we can say, an anonymous class is essentially a local class without a name. The main advantage of using an anonymous class is Encapsulation.

An anonymous class is, in a way, the ultimate in private object oriented encapsulation. It is defined exactly where it is needed, it can never be used anywhere else, and it is having a total local scope. Anonymous classes provide a very clear syntax for implementing event listeners in GUI programming, and are very much useful for implementing method parameters and returning objects.

One of the key advantages of using anonymous classes is that it relieves the programmer from defining large numbers of special purpose classes.

Such classes are used only once and are defined right where the action is taking place, and never used again. Also, anonymous classes have access to all data and methods of their containing classes, including private members which means that for small highly localized tasks, they generally need less initialization.

The syntax of anonymous classes have one of the two forms which are shown below :

  1. Interface Based : The syntax for declaring interface based anonymous class is given as,

  2. 
    new InterfaceName()
      {
        // ClassBody
      }
    
    

    An interface based anonymous class has to implement the entire interface on which it is based upon, to understand it better, take a look at the following code snippet :

    Example

    
    Java Tutorialslass AnonymousClassExample
    {
     public static void main(String args[])
      {
       Runnable obj = new Runnable()            
    	   // anonymous class is defined here
      {
       private static final int ARRAY_LIMIT = 50;
       private int[] arr = new int[ARRAY_LIMIT];
       // instance intialization code
        {
         for(int i=0; i< arr.length; i++)
         arr[i] = i;
        }
       public void run()
        {
         int sum = 0;
         for(int i=0; i<ARRAY_LIMIT; i++)
         sum = sum + arr[i];
         System.out.println("The sum of the  array elements is :"+sum);
    	 // printing the sum of array
        }  
    
      };           // anonymous class ends here
         obj.run();
       }
    }
    
    

     

  3. Class Based Anonymous Class : The syntax for class based anonoymous class is,

  4. 
    new ClassName (optional Argument_List)
    {
    // Class Body
    }
    
    

    A class based anonymous class has access to its base class methods and members and it can override itís members, just like any other subclass. To understand more take a look at this code snippet,

    Example

    
    Java Tutorialslass AnonymousClassExample2
    {
        ShowMessage anonymous_obj = new ShowMessage("R4R Example")
                                                       {
    	 public void display()
    	  {
     System.out.println("anonymous override"+"member display ShowMessage Output :"+msg);
    	  }
    						};	
        public AnonymousClassExample2()
    {
     ShowMessage obj = new ShowMessage("hi!!!!!!");
     obj.display();
    ShowMessage anonymous_obj2 =new ShowMessage("hi!!!!!!!")
    {
     public void display()
    {
     System.out.println("anonymous override"+"local display ShowMessage output :"+msg);
    }
    };
    anonymous_obj2.display();
    this.anonymous_obj.display();
    }
    public static void main(String args[])
    {
    AnonymousClassExample2 example = new AnonymousClassExample2();
    }
    }
    Java Tutorialslass ShowMessage
    {
    String msg="";
    public ShowMessage(String msg)
    {
    this.msg=msg;
    }
    public void display()
    {
    System.out.println("SimpleClass output :"+msg);
    }
    }
    
    
Previous Home Next