• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with the working of method getOutputStream( )  RSS feed

 
vijay malhotra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These few lines given below are part of some code
related to Java Networking
...
Socket s = new Socket("internic.net",48);
OutputStream out = s.getOutputStream();
String str = "starwave.com";
byte buf[] = str.getbytes();
out.write(buf);
...
My Question is :-
Q1 What does the method getOutputstream() returns if it
returns a reference to the output stream then how
we are able to create the object variable 'out' .
Q2 As Outputstream class is an abstract class we can not
create and use it's object as we are doing in the
in the following lines of code
OutputStream out = s.getOutputStream();// Creating an object
out.write(buf); // Using the object
 
Prabhjyot Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,
The method getOutputStream()is a predefined method which returns an object of OutputStream.So you are not instantiating that class but this inbuilt method is returning you that object.You are least bothered about it's implmentation which is provided by some geek at Sun.
Who says you can't have an object of an abstract class.If any client class extends your beautiful abstract class and provides behaviour to every abstract method of ur lovely class then an instance of this client class IS ALSO an object of ur abstarct class.This is afterall what polymorphism is all about.Hope it helps.
rgds,
prabh
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!