A class that have no name is called anonymous inner class;
They can be created by using:
1. class
2. abstract class
3. interfaces
Example 1:
lets consider a abstract class
package abstractconcept;
public abstract class MyAbstract
{
public String name;
public int age;
/**Abstract Method*/
public abstract int sum(int a,int b);
/**
* Concrete Method
*/
public int sub(int c,int d)
{
return c-d;
}
}
Implementing Class
package abstractconcept;
public class Annonymous
{
public static void main(String[] args)
{
MyAbstract obj=new MyAbstract() {
@Override
public int sum(int a, int b) {
// TODO Auto-generated method stub
return a+b;
}
};
System.out.println(obj.sum(10,10));
}
}
Output: 20
Code of Anonymous Class:Generated by the compiler at compile time
import java.io.PrintStream.*;
public class Annonymous$1 extends MyAbstract
{
Annonymous$1(){} //Default Constructor
public int sum(int a, int b)
{
return a+b;
}
}
Example 2:
Anonymous inner class using interface
package abstractconcept;
interface MyAbstract
{
/**Abstract Method*/
public abstract int sum(int a,int b);
}
Implementing Class
package abstractconcept;
public class Annonymous implements MyAbstract
{
public static void main(String[] args)
{
MyAbstract obj=new MyAbstract() {
@Override
public int sum(int a, int b) {
// TODO Auto-generated method stub
return a+b;
}
};
System.out.println(obj.sum(10,10));
}
}
Output: 20
Code of Anonymous Class:Generated by the compiler at compile time
import java.io.PrintStream.*;
public class Annonymous$1 extends MyAbstract
{
Annonymous$1(){} //Default Constructor
public int sum(int a, int b)
{
return a+b;
}
}
They can be created by using:
1. class
2. abstract class
3. interfaces
Example 1:
lets consider a abstract class
package abstractconcept;
public abstract class MyAbstract
{
public String name;
public int age;
/**Abstract Method*/
public abstract int sum(int a,int b);
/**
* Concrete Method
*/
public int sub(int c,int d)
{
return c-d;
}
}
Implementing Class
package abstractconcept;
public class Annonymous
{
public static void main(String[] args)
{
MyAbstract obj=new MyAbstract() {
@Override
public int sum(int a, int b) {
// TODO Auto-generated method stub
return a+b;
}
};
System.out.println(obj.sum(10,10));
}
}
Output: 20
Code of Anonymous Class:Generated by the compiler at compile time
import java.io.PrintStream.*;
public class Annonymous$1 extends MyAbstract
{
Annonymous$1(){} //Default Constructor
public int sum(int a, int b)
{
return a+b;
}
}
Example 2:
Anonymous inner class using interface
package abstractconcept;
interface MyAbstract
{
/**Abstract Method*/
public abstract int sum(int a,int b);
}
Implementing Class
package abstractconcept;
public class Annonymous implements MyAbstract
{
public static void main(String[] args)
{
MyAbstract obj=new MyAbstract() {
@Override
public int sum(int a, int b) {
// TODO Auto-generated method stub
return a+b;
}
};
System.out.println(obj.sum(10,10));
}
}
Output: 20
Code of Anonymous Class:Generated by the compiler at compile time
import java.io.PrintStream.*;
public class Annonymous$1 extends MyAbstract
{
Annonymous$1(){} //Default Constructor
public int sum(int a, int b)
{
return a+b;
}
}
0 comments:
Post a Comment