• Post Reply Bookmark Topic Watch Topic
  • New Topic

is it really instantiating an abstract class here?  RSS feed

 
subhajit paul
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some time back in a thread i found a post saying
Of corse an abstract class can be instantiated, though only through an anonymous class


So in this code,

are we really instatialing an object of class A? are not we first making an anonymous class which extends A and implements its abstract method and creating an object of that anonymous class?

please clarify.

TIA
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, and the post you found is wrong. This is not instantiating an abstract class.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by subhajit paul:
are we really instatialing an object of class A? are not we first making an anonymous class which extends A and implements its abstract method and creating an object of that anonymous class?
I presume instatialing should be spelt instantiating!

You are actually doing both. Try declaring it an an A, and try using the instanceof operator on it . . . you should get "true."
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does sound like what I might say; if it was me, I am sorry about the mistake.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
That does sound like what I might say; if it was me, I am sorry about the mistake.

I believe it was you; I can vaguely remember that post.

The anonymous class is a subclass of A without a proper name. I'm saying proper name because it does have a name; in this case the name would be AbstractExample$1.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was me; I found it last night and can't find it now.

I did actually say "don't know" and "try new A(){};" which is not quite the same as what is being done here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!