Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstraction

 
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic