Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

10. Numbers and Statics: Numbers Matter > Getting an object that extends Calend...

Getting an object that extends Calendar

How in the world do you get an “instance” of an abstract class? Well you don’t of course, this won’t work:

This WON’T work:

image with no caption

Instead, use the static “getInstance()” method:

image with no caption
image with no caption

You can’t get an instance of Calendar, but you can can get an instance of a concrete Calendar subclass.

Obviously you can’t get an instance of Calendar, because Calendar is abstract. But you’re still free to call static methods on Calendar, since static methods are called on the class, rather than on a particular instance. So you call the static getInstance() on Calendar and it gives you back... an instance of a concrete subclass. Something that extends Calendar (which means it can be polymorphically assigned to Calendar) and which—by contract—can respond to the methods of class Calendar.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial