This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes java general Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java general" Watch "java general" New topic
Author

java general

Gangireddy Danam
Ranch Hand

Joined: Aug 08, 2012
Posts: 32

class P{
public void pop()
{
System.out.println("P");
}
}
class Food{
P ob=new P(){
public void method1()
{
System.out.println("hai..");
}
public void pop()
{
System.out.pritnln("sir");
}
};
public void popit()
{
ob.pop();//it will execute
ob.method1();//it will not execute..
}
}
...if i want to call method1()..how could may i call..?
can anonymous classes will replace constructors..?

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

can you edit your post and put code tags around the java code? It is very difficult to read otherwise.

like so:



also post your real code. This will not compile as is (typos, etc.).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18501
    
  40

Gangireddy Danam wrote:...if i want to call method1()..how could may i call..?
can anonymous classes will replace constructors..?


Basically, using a Pop reference, you can't, as the Method1() method is not in scope for the Pop class. The likely / common answer is that you need to cast the variable, but since it was instantiated from an anonymous class, the class definition is out of scope -- and hence, you can't cast it either.... So, your only option is to use the reflection libraries.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java general
 
Similar Threads
Reg. invoking private methods
question about polymorphism
Why private method of parent is called?
super . super
About Overridden II (a Mock Question question...)