• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract and annonymous classes

 
Mohammad Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammad Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic