1

How can I access this anonymous class method in Java and I getting error on below. What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of java?

class Test1
{
    public Test1()
    {
        System.out.println("Yes");
    }
}

class Test extends Test1
{  
    public static void main(String []args) {
        Test1 obj1= new Test1()
        {
            public void test1()
            {
                System.out.println("Yes A");
            }
        };
        obj1.test1(); // here i am getting error 
    }
}
Perneel
  • 3,317
  • 7
  • 45
  • 66
Rajadurai K
  • 47
  • 10
  • Post the error trace you are getting, it will help to solve the problem. – Mandar Pandit Jul 03 '14 at 09:14
  • It is possible to access that method. But I don't know why you would ever want to do this. public static void main(String[] args) { new Test1(){ public void test1(){ System.out.println("Yes A"); } }.test1(); } An alternative: public static void main(String[] args) { abstract class Test2 extends Test1 { public abstract void test1(); } final Test2 test2 = new Test2(){ @Override public void test1(){ System.out.println("Yes A"); } }; test2.test1(); } – lexicalscope Jul 03 '14 at 09:56

5 Answers5

0

You have not anonymous class there.

You got error, because you try to invoke method test1() thats not declared in class Test1 and your object obj1 instance of Test1 class.

About anonymous classes try to read this tutorial.

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
0

You can't access it this way. obj1 is of type Test1 and this class doesn't have test1 method.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • Sure, but you can't access it via superclass type. Anyway, I marked it as duplicate of: http://stackoverflow.com/questions/18656107/using-an-arbitrarily-defined-method-of-an-anonymous-interface – Jan Zyka Jul 03 '14 at 09:21
0

When I am trying to compile your program, I am getting error at line obj1.test1(); as The method test1() is undefined for the type Test1.

You can solve this by creating a test1() method in Test1 class.

class Test1 {
  public Test1() {
     System.out.println("Yes");
  }
  public void test1() {

  }
}
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
0

With a parent class reference you can only access methods declared in parent class.
when you do

Test1 obj1= new Test1(){
      public void test1(){
        System.out.println("Yes A");
      }
};
obj1.test1(); // here i am getting error 

you are creating an anonymous instance of a class extending Test1 which is referred by the parent type reference.

so the method test1() actually is a new method which belongs to the extending class and not an overridden method of Test1, so it cant be called using obj1.

Shailesh Aswal
  • 6,632
  • 1
  • 20
  • 27
0

your code is an example of inheritance it is not an example of anonymous class. Here Test1 obj1= new Test1() you are creating object of Test1 class and trying to invoke method test1() on that object it means test1() method should be present in Test1 class.

And anonymous class you can see here

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
How are Anonymous (inner) classes used in Java?

Community
  • 1
  • 1
Dharmraj
  • 164
  • 1
  • 15