Thread View: borland.public.jbuilder.java.api
5 messages
5 total messages
Started by Ron
Wed, 24 Mar 2004 19:47
Socket.getInputStream() question
Author: Ron
Date: Wed, 24 Mar 2004 19:47
Date: Wed, 24 Mar 2004 19:47
21 lines
1372 bytes
1372 bytes
Socket.getInputStream() returns an InputStream object. So, if I have this code: 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? Thanks. RON _________________________________________________________ Black and White Photography: http://www.ronaldgansphotography.com
Re: Socket.getInputStream() question
Author: Shankar Unni
Date: Wed, 24 Mar 2004 21:41
Date: Wed, 24 Mar 2004 21:41
16 lines
1663 bytes
1663 bytes
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.
Re: Socket.getInputStream() question
Author: Ron
Date: Thu, 25 Mar 2004 14:09
Date: Thu, 25 Mar 2004 14:09
31 lines
2260 bytes
2260 bytes
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.
Re: Socket.getInputStream() question
Author: 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.
>
>
Re: Socket.getInputStream() question
Author: Ron
Date: Thu, 25 Mar 2004 15:05
Date: Thu, 25 Mar 2004 15:05
134 lines
5547 bytes
5547 bytes
Thanks. That totally cleared it up for me.
RON
"Kevin Dean" <NkOdSePaAnM@datadevelopment.com> wrote in message
news:4062ed7e$1@newsgroups.borland.com...
> 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.
> >
> >
>
>
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads