• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
Given the following,


the anonymous inner class created in line Boo f = new Bar() { }; is the sub-class of which class ? of Boo or Bar
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a subclass of Boo because it is created using the syntax new Boo(){}
As Boo is a subclass of Bar, it also becomes a subclass of Bar.

It can be assinged to a variable of type Bar because again Bar is the superclass of its superclass that is Boo.

If it would have been created using the syntax new Bar(){}, it would have been subclass of only Bar and not Boo.

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


Output:
class Bar$1
true
false
class Bar$2
true
true
class Bar$3
true
true

So in all the cases, a class created inside the Bar class, would be subclass
of Bar only.

Case #1: f1 is a reference variable of class Foo, that refers to the
object of the anonymous class that extends class Foo.

Case #2: f2 is a reference variable of class Foo, that refers to object
of the anonymous class that is subclass of class Bar.

Case #3: f3 is a reference variable of class Bar, that refers to object
of the anonymous class that is subclass of class Bar.

Note: Because Foo is parent class of class Bar, its reference variable
can hold the anonymous implementer or extending class of class Bar or its
subclasses.


Thanks,
[ May 12, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Chandra is Right.All are inner classes of Bar.

I got runtime class and superclass mixed up.Sorry for that.

Thanks,
Megha
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the anonymous class is a sub-class of the class which is used with new keyword before the curly braces of the anonymous class.

a class created inside the Bar class, would be subclass
of Bar only.
this statement is not clear. inside the bar class means what?
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


a class created inside the Bar class, would be subclass
of Bar only.




This statement should be

a class created inside the Bar class, would have runtime
class as Bar, it can be subclass of any class, not necessarily Bar only.

Thanks,
Megha
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> a class created inside the Bar class, would have runtime
class as Bar

What do you mean by runtime class? (I know only about java.lang.Runtime class)
I couldn't find any reference in JLS 3.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes can't stand independently like top level classes, even if it
is nested class (static class). There is association between enclosing class
and inner class. If you see the the classes created after compilation, you
will see like:


Outer$Inner1.class
Outer$Inner2.class
Outer$Inner3.class
...

This dollar sign indicate that Inner1 2 3 are inner classes of the Outer
class. In the case of Anonymous class, as we know there is no name of
anonymous class so they stand like

Outer$1.class
Outer$2.class
Outer$3.class
...
and so on.

Inner classes can be subclass of the enclosing class. As

class Outer {
class Inner extends Outer{
}
}

Still you will find the Inner class as
Outer$Inner1.class
But this time the instance of the Inner class will pass the instanceof
test with the Outer class, otherwise if it was not subclass,
there would be compiler error while doing

(innerobject instanceof Outer) //error, incompatibility error

In case of nested class, although you need not to have instance of Outer
to instantiate the nested class, but outside the Outer class, you must
say like

Outer.Nested nested = new Outer.Nested(); //OK

if you want to instantiate the Nested class.


Thanks,
[ May 13, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


a class created inside the Bar class, would be subclass
of Bar only.
this statement is not clear. inside the bar class means what?



Let me modify that line a bit:

"a class created inside the Bar class, would be inner class of Bar only" even if it is anonymous implementer class. It will come after Outer class name followed by dollar sign followed by Inner class name (1,2 ... in
case of anonymous class).

Actually we are talking about inner classes, classes created inside the outer class would be inner classes of the enclosing class.



After compiling Bar class, if you see what classes are created, you will
find
Bar.class
Bar$1.class

So the second class file is nothing but the anonymous class, that was
created inside the Bar class's main() method.

I think there is a confusion around inner class and
subclass.
"We will say that a class class B is a subclass of class A only if it
passes IS-A test."


Thanks,
[ May 13, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the original question asked if it was a sub class of which class. Actualy I think it is an inner class and not a subclass at all.

Inner class and subclass are two different things.

The only class that knows about the inner class is the enclosing class.

It would not be a subclass of any class.

Also as discussed the second class is already a subclass of the first then any succeeding sublclasses would also be a subclass of the first.

In OO the children, grandchildren and great grandchildren, g. g. grandchildren are all considered children.

But this is an inner class.
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Boo {
Boo(String s) { }
Boo() { }
}
class Bar extends Boo {
Bar() { }
Bar(String s) {super(s);}
void zoo() {
Boo f = new Bar() { };
}
}

--------------------------------------------------------------------------------

here I asked:

the anonymous inner class created in line Boo f = new Bar() { }; is the sub-class of which class ? of Boo or Bar

the anonymous inner class is always a sub-class (or implementer class) of the object or instance type not the reference type.

and the problem arose by chandra's statement of a class declared inside a outer-class is always a subclass of outer-class which is wrong But it is always an inner class of outer-class.

Thanks,
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> the anonymous inner class created in line Boo f = new Bar() { }; is the sub-class of which class ? of Boo or Bar

Both. (direct subclass of Bar, indirect of Boo)

>> inner class and subclass

It is possible to derive from inner classes, so I rather prefer not to use "sub-class" term (I consider it as wrong) if you just want to say, that every inner class instance is connected to instance of outer class.

>> "runtime class"
I can guess, what he meant, but I don't think this is some official term used in JLS. (If it is, please post a link)
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is possible to derive from inner classes, so I rather prefer not to use "sub-class" term (I consider it as wrong) if you just want to say, that every inner class instance is connected to instance of outer class.



I agree, I would be wrong to refer it "subclass". We should say it inner
class, sub-class is only true when inner class extends Outer too obviously.



Originally posted By Shyam,
the anonymous inner class created in line Boo f = new Bar() { }; is the sub-class of which class ? of Boo or Bar



I think this question is itself ambiguous. Bar is subclass of Foo too.
f is subclass of Bar and Foo too, but inner class of Bar only.


My above post, described a Bar class that extends Foo class that is not
enclosing class.


Anyways it was all about inner class.

Thanks,
[ May 13, 2007: Message edited by: Chandra Bhatt ]
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What do you mean by runtime class? (I know only about java.lang.Runtime class)
I couldn't find any reference in JLS 3.



If you look at defination of getClass() method, you will see it gives the runtime class of the reference on which it is invoked.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> a class created inside the Bar class, would have runtime class as Bar
All right, thanks, I can see now, what you meant.

But as we can see from Chandra's example the runtime class of those objects are not as runtime class of Bar.



gives "false"
 
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
I think some concepts are being confused here. Let's put aside the question of inner/outer for a moment, and consider the basic syntax for an anonymous class:

new TypeA(){};

Here, an instance of an anonymous class is being created. The anonymous definition (inside the braces) extends or implements TypeA, depending on whether TypeA is a class or an interface. So this anonymous instance IS-A TypeA (in other words, a subclass of TypeA).

Now consider an assignment: TypeB b = new TypeA(){};

Here, we are taking the reference to the anonymous instance (which IS-A TypeA), and assigning it to a variable that is TypeB. Because there is no explicit cast, this is only possible if it's an upcast -- that is, if TypeA extends TypeB. So we now know that the anonymous instance IS-A TypeB in addition to being a TypeA.

So what really matters in this question has nothing to do with inner/outer. What matters is simply...
  • Bar extends Boo, and...
  • An anonymous class is instantiated using Boo f = new Bar(){};
  • It doesn't matter where this is being instantiated, so let's just put it in an entirely separate class called Test.

    From the output, we see that f IS-A Bar and f IS-A Boo. (Indeed, since Bar extends Boo, any instance of Bar will also be a Boo.)

    In the above code, this anonymous instance happens to be an inner class of Test (so its class name is Test$1), but this has nothing to do with it being a subclass of both Bar and Boo. Just as in the original example, the anonymous instance happens to be an inner class of Bar (so its class name is Bar$1), but this has nothing to do with it being a subclass of both Bar and Boo.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic