Thursday 21 July 2011

anonymous class & object in java

java is typical , and show its tipicalness many times , typical means for user they can do ever thing , even they can kill you for user's happiness .

take a example of anonymous class .
here you can see that , why java is famous , for great user expirience , and assassin of a programmer .

anonymous means without name .
so anonymous class is , class without name . so now the image , in your mind is that , how to store it , thats why java make this class as a inner class .

but why we use it .
think you have a playstation , and in a playstation 10 players will come and play the game , so you have to display their information , for this if you are from  inheritance background , than you will make 10 classess , not a bad thinking , but in these classes their are everything is same , only different thing is a player , so here you make 10 classes for a method . thats why we use anonymous class .

now its declaration & working .

play_station o  = new  play_station()
{  // class starts at run time
void theory()
{
System.out.println("1st person");
}
/*class ends*/ }.player();
                o.screen();
                o.button();

play_station o  = new  play_station()
{  // class starts at run time
void theory()
{
System.out.println("2nd person");
}
/*class ends*/ }.player();
                o.screen()
                o.button();


when class starts , and wheu can it ends after it it will create a reference n this reference is assigned to 'o' and after it you can call the method player( ); for each different person , and overrides it.


----------------------------------------------------------------------------------------------------------------------------------

after it i have anonymous object . so same meaning but concept is that , remove name

like  " new play_station( );  " here you create a object and this have a reference , so if

"new play_station.theory() ;"; than ok it will call  , but the problem with this is that . this object is built for only one method , for using other method you have to create a block initializer or create a new object
for another method .

No comments: