aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous -Q Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Anonymous -Q" Watch "Anonymous -Q" New topic
Author

Anonymous -Q

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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...


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17233
    
    1

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Anonymous -Q
 
Similar Threads
abstract class
Use of method local inner class being abstract...
Confused about Dan's exam...
Sorting ArrayList
Anonymous inner class