wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract and annonymous classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract and annonymous classes" Watch "abstract and annonymous classes" New topic
Author

abstract and annonymous classes

Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
abstract class AbstractTest {}

public class MyApp {
public static void main(String[] args) {
// I understand following code would fail compile, so commented
// AbstractTest at = new AbstractTest();

// But why won't the following code
// Would anybody please explain,
AbstractTest c = new AbstractTest() {};

// Thanks, in advance !
}
}
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
OK, I jumped with my previous comment, I thought the original poster was saying it won't compile.

Anyways, an abstract class cannot be instantiated cause by definition, it doesn't provide a complete implementation. Refer to JLS 8.1.1.1

In the second instantiation example you're creating an anonymous class of type AbstractTest & since there are no abstract methods to implement it ends up being a concrete implementation.

HTH
Ashish Hareet
[ October 01, 2008: Message edited by: Ashish Hareet ]
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
The line -

AbstractTest c = new AbstractTest() {};

compiles because the object is not an abstract class now, it is a sub-class of AbstractTest. Try to remember when we use anonymous class to implement an interface or subclass a super class.
For example, consider an anonymous class which implement Runnable interface -
Thread t = new Thread(
new Runnable() {
public void run() {
System.out.println("This is not an interface instatiation.
It is an implementation of Runnable.");
}
}
);
You can not do -
new Runnable(); // you can not instantiate an interface

You can do -
new Runnable() {} // it is implementation. Same true for sub-class

Hope it helps.


Ahsan Habib
SCJP5, SCWCD5
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Hi Sheema,

Thanks for your example code.

Exactly I did the same thing when I was waiting for reply and it made me to understand it.

Thanks again,
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Sorry guys, Ashish is right, I compiled Mohammad's code with a abstract method in AbstractTest and failed to give an implementation for it in the anonymous class.

Mohammad, the code works as is. Sorry for the confusion, I'll delete my explanation given earlier lest it confuses you!
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Originally posted by Seema Gaurav:
Sorry guys, Ashish is right, I compiled Mohammad's code with a abstract method in AbstractTest and failed to give an implementation for it in the anonymous class.

Mohammad, the code works as is. Sorry for the confusion, I'll delete my explanation given earlier lest it confuses you!


Aren't we both quick Seema , both of us gave the correct answers it was just our understanding of what the original poster was trying to say.

To sum it up, the orignal code provided will compile as-is, why it'll compile, please refer to previous posts.
[ October 01, 2008: Message edited by: Ashish Hareet ]
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Thanks Ashish, this forum has been of immense help in understanding the nuances of the language. And with people like you here, who are tolerant of others' mistakes, really makes one feel comfortable
Thanks,
Seema
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract and annonymous classes
 
Similar Threads
about abstract class
Virtual Constructor
abstract
Why a non-abstract Parent Class can be extended by an abstract Child Class ?
Inner Classes