Article View: borland.public.jbuilder.java.api
Article #277Re: Socket.getInputStream() question
From: Kevin Dean
Date: Thu, 25 Mar 2004 14:32
Date: Thu, 25 Mar 2004 14:32
116 lines
5102 bytes
5102 bytes
Consider the classes below. Assume all of these classes are in different
packages so that they don't implicitly import each other and aren't imported
with another's "import packagename.*".
public abstract class Base {
public abstract void doSomething();
}
public class Derived extends Base {
public void doSomething() {
System.out.println("Hi there!");
}
public void doSomethingElse() {
System.out.println("Good bye!");
}
}
public class SomeFactory {
public Base getBase() {
return new Derived();
}
}
public class MyClass {
public void main(String[] args) {
SomeFactory sf = new SomeFactory();
Base b = sf.getBase();
b.doSomething();
// b.doSomethingElse();
}
}
The declaration of the SomeFactory.getBase() method says that this method
will return a Base object or an object of a type that extends the Base
class. Because Base is abstract, the method can't instantiate it directly
but can instantiate the Derived class, which extends Base and is therefore
assignment-compatible with the return type.
The MyClass.main() method doesn't need to know about the Derived class; all
it needs to know is that it's going to get some object of type Base, and it
knows that the Base class supports a method called doSomething(). The fact
that it gets a Derived object is completely irrelevant. In fact, if you
uncomment the last line of code and try to call the doSomethingElse() method
on the Base object, the compiler would complain that the method doesn't
exist in class Base. Even if you imported the Derived class, the code still
wouldn't compile, because as far as the compiler knows, you are dealing with
a Base object only; what gets returned at runtime is not the compiler's
concern. If you wanted to use the Derived.doSomethingElse() method, you
would need to rewrite the code as follows:
public class MyClass {
public void main(String[] args) {
SomeFactory sf = new SomeFactory();
Base b = sf.getBase();
b.doSomething();
// Check for the type we're looking for.
if (b instanceof Derived) {
// Cast object b to type Derived before calling the Derived method.
((Derived)b).doSomethingElse();
}
else {
System.out.println("Object b is not a recognized type; true type is "
+ b.getClass().getName());
}
}
}
--
Check out our latest white papers at
http://www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
http://www.datadevelopment.com/
"Ron" <rgans@nospamRonaldgansphotographyDOTcom> wrote in message
news:4062e7ff$1@newsgroups.borland.com...
> Thanks for the help, but I am still a little confused. The fragment I
wrote
> just imports java.io.* and java.net.* not
> org.apache.commons.net.io.SocketInputStream; so, if Socket does return a
> SocketInputStream, where does it get it from? The Java documentation just
> says
> public InputStream getInputStream()
> throws IOExceptionReturns an input stream for
> this socket.
> so, where does the SocketInputStream come from?
>
> Thanks.
>
> RON
> "Shankar Unni" <shankarunni@netscape.net> wrote in message
> news:40620071$1@newsgroups.borland.com...
> > Ron wrote:
> >
> > > InputStream in = sock.getInputStream();
> > > in.read(....);
> > >
> > > what I don't understand (as a novice) is: InputStream is abstract so
it
> > > cannot be instantiated. But an InputStream object is being
instantiated,
> > > right? So, how?
> >
> > No, it's not being *directly* instantiated. What's being instantiated is
> > a concrete subclass of InputStream (namely a SocketInputStream).
> >
> > The reason the API is defined in this fashion (as returning the abstract
> > base class) is to give the implementor some flexibility in returning a
> > different type of InputStream in a different version, without forcing
> > all the callers to recompile.
>
>
Technical Details
Message-ID:
<4062ed7e$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4061e5c3$1@newsgroups.borland.com> <40620071$1@newsgroups.borland.com> <4062e7ff$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,6e32650011d9be01 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2004-03-25 06:40:39 PST From: "Kevin Dean" <NkOdSePaAnM@datadevelopment.com> Newsgroups: borland.public.jbuilder.java.api References: <4061e5c3$1@newsgroups.borland.com> <40620071$1@newsgroups.borland.com> <4062e7ff$1@newsgroups.borland.com> Subject: Re: Socket.getInputStream() question Date: Thu, 25 Mar 2004 09:32:27 -0500 Lines: 118 Organization: Dolphin Data Development Ltd. X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 24.57.150.243 Message-ID: <4062ed7e$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1080225150 24.57.150.243 (25 Mar 2004 06:32:30 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail