• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java general

 
Ranch Hand
Posts: 32
Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..?

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic