• 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

Anonymous -Q

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract class A {
public int x = 1, y = 1;
public A(int x, int y) {this.x = x; this.y = y;}
public abstract int math();
}
class BB {
A a1 = new A(2,1) {public int math() {return x + y;}};
static A a2 = new A(2,1) {public int math() {return x - y;}};
static A a3 = new A(2,1) {public int math() {return x * y;}};
static A a4 = new A(2,1) {public int math() {return x / y;}};
public static void main(String[] args) {
System.out.print("" + a1.math() + a2.math() +
a3.math() + a4.math());
}}

// I found this question in anonymous section of Dan Chisholm's exam

// 1. How we are able to instantiate an abstract class A inside class BB
// So does this mean a nested class can instantiate a abstract class.
// 2. how we are able to access variables x and y because class BB dosen't
extend class A

If A a1 = new A(2,1) is not a nested class but an anonymous class then how
can a anonymous class have reference variables like a1,a2...
 
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
It might look like we're instantiating A. But actually, we're creating instances of anonymous types that are subclasses of A. These subclasses are not abstract because they implement A's abstract method. The objects are then upcast to type A.

When you create an anonymous inner class, you essentially call the constructor of an existing class -- but after the constructor's arguments (where you would normally have just a semicolon), you insert a new class definition in brackets. From this, you get an object that is a subclass of the class whose constructor you called, and is automatically upcast from the anonymous type to the class whose constructor you called.

In this case, we have objects of an anonymous type that extend class A, and are automatically upcast to type A. In the anonymous class definitions -- the parts in brackets slipped in between "new A(2,1)" and the semicolon -- we are providing implementation for the method, math(). So although the superclass is abstract, the anonymous subclasses are not.


See Chapter 8 of Bruce Eckel's Thinking in Java (anonymous inner classes are discussed about halfway through)...

http://www.faqs.org/docs/think_java/TIJ310.htm

NOTE: Alternatively, anonymous classes can implicitly implement an interface instead of extending another class. The syntax is the same. Either way, the critical implementation is in the anonymous class body, and the resulting object is automatically upcast to the parent or interface type.
[ December 23, 2004: Message edited by: marc weber ]
 
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
I should add the following...

When you call a method implemented (or overridden) in the anonymous class, this is where polymorphism kicks in.

For example, a1 is upcast to type A, but its "actual" runtime type is the anonymous subclass. So when you call a1.math(), the method implementation in the anonymous class definition is invoked.
[ December 23, 2004: Message edited by: marc weber ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vishnu. We have these "CODE" tags that makes posting code keep their indentation, which makes things much easier to read.

The CODE button is underneath the Add Reply button, in the second column, right under Email and Italics buttons.

Thanks

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic