• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

anonymous inner class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) An anonymous inner class constructor can take argument in some situation
2) can anonymous inner class implement an interface ?
can someone explain me point 1 with an example and answer point 2
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first pont

anonymous inner classes don't have any name,so they can't have constructors.

For the second pont
Yes

interface Animal
{
public void behaviour();
}
class Bird
{
public static void main(String... args)
{
Animal a=new Animal(){
public void behaviour()
{
System.out.println("Fly");
}
};
a.behaviour();
}
}
Thanks

Anil Kumar
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Anonymous inner class can not have a constructor. The inner class is "anonymous", so you wouldn't be able to provide the constructor with a name.

2. Anonymous inner classes can implement an interface. Most common one you'll see in mock exams is the implementation of the Runnable interface:

Runnable rn = new Runnable() {
public void run() {
System.out.println("Hello there");
}
};
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic