Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstraction  RSS feed

 
manohar reddy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to instatiate abstraction
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstraction in Java is probably an interface or abstract class. You can't instantiate these. Both of these will fail:

x = new Runnable();
y = new AbstractList();

You can make a concrete class that implements the interface or extends the abstract class. The compiler will require the concrete class to provide all the abstract methods. Here's a concrete class you could instantiate:


And there's another tricky way to create a class right in line instead of defining it in its own file:

That looks a lot like we instantiated Runnable, an interface, but in fact we defined a tiny class with a run() method. It has no name so it's called anonymous, but it's a perfectly good little class as far as the compiler's concerned.

Does that all make sense?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!