• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstraction

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to instatiate abstraction
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic