File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Ghost Instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Ghost Instances" Watch "Ghost Instances" New topic
Author

Ghost Instances

Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
I wrote this post, but it seems to have vanished, so i'll try again. Here is a brain teaser that I hope someon can solve. You cannot have instances of an abstract class. And yet InputStream and Graphics are both abstract classes. Once can get instances of these classes using the getInputStream() and getGraphics() methods respectively. So what gives?
Thanks in advance
Dan
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You cannot directly instantiate an abstract class - that is, you cannot create an object which is an instance of that class but not any subclass. But you can create an instance of a subclass, which (by OO definitions) "is" an instance of the abstract class.


"I'm not back." - Bill Harding, Twister
Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
That is what I suspected was going on (i.e. the instances are converted to instances of the respective abstract classes). But what classes (i.e. the subclasses) are being instantiated when the getGraphics() and getInputStream() are used?
Thanks in advance
Dan
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The whole point is that you don't know what implementation you are using. That is the beauty of Object Oriented programming. Example (not the real classes used): Sun's virtual machine might pass you a com.sun.java.socket.SocketInputImpl when IBM's would pass you a com.ibm.core.Sockets.InputStream. Each one fits that vendor's separate JVM implementation. There is no need to know -- it just works.
[This message has been edited by David Garland (edited August 21, 2001).]
Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
I get it ... thanks for the info!
Dan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ghost Instances
 
Similar Threads
Regarding abstract class
interface
enum extending abstract class
Difference between interfaces and abstract classes
Abstract Factory and Factory Method