• 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

Doubt: Anonymous InnerClasses

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,



Does the above code the new Parent("Ravi"){} would call the Construtor(with String) of the subclass extending Parent. Now my question is doesn't compiler insert super() as the first line of the constructor of the subclass (Just as it do asusual ). If it does, then does not it throw compiler error as no No-Arg constructor defined in Parent class.

actually it compiled fine and run too....I didn't understand why was there no compiler error?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question!

A constructor for the anonymous class is provided by the compiler, but it doesn't simply use super with no arguments. Instead, the call to super basically matches the arguments provided in the instance creation expression.

The details are under JLS 15.9.5.1 Anonymous Constructors.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class get
{
get()
{

System.out.println("A");
}
get(String s)
{
this();
System.out.println(s);
}
}
class shiva
{

public static void main(String args[])
{
get g=new get("B");

}
}
output:
A
B
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by praveen oruganti:
...output:
A
B


Yes, but this is just a call to an overloaded version of the constructor (using "this"). There is no anonymous inner class here.

 
reply
    Bookmark Topic Watch Topic
  • New Topic