Author Topic:   Abstract method read()
gunjan
ranch hand
posted April 13, 2000 10:13 PM         
Friends:
'in' is a static and final member of the System class.
in is a reference to the InputStream class
InputStream class is itself a abstract class and read method of the InputStream class is itself abstract.
My question is how can you invoke a abstract method of a abstract class and still get the desired working. abstract methods do not have any method body.
THat is System.in.read() works.

My second question will be in another post regarding System.out
Any help appreciated
Gunjan

Jane Rozen
ranch hand
posted April 14, 2000 10:29 AM             
1. Though it is not possible to instansiate objects of abstract classes (as InputStream), it is possible to define class variables ("in"), which reference these abstract classes. This makes static(class) methods of the abstract class available.
2. Method read() is not abstract in our case.

[This message has been edited by Jane Rozen (edited April 14, 2000).]

gunjan
ranch hand
posted April 14, 2000 11:24 AM         
Referring to Java API
read() method is abstract.
The other two overloaded methods
read(byte[] b, int off, int len)
read(byte[] b) are not abstract.

But read() is abstract. That means when you say System.in.read() you are calling a abstract method. Why does read method still work.

Gunjan

maha anna
bartender
posted April 14, 2000 12:36 PM             
gunjan,
The 'System' class has a static member 'in' which is a reference to an instance of type 'InputStream'. The class 'InputStream' as such is an abstract class, and it's 'int read()' method IS ABSTRACT. There is no doubt in that. Your doubt is reasonable. But you are missing another point here. Whenever you see a reference is of ABSTRACT CLASS TYPE, it DOES NOT MEAN it is pointing to an object of type of the abstract class. What it really means is , It is referring to an object of one of its SUBCLASSES which implements the abstract part of the base class, and an object/instance is created FROM THE SUBCLASS and assigned to the BASE CLASS type (which is abstract). In other words, the foll. sample code is legal in Java. (And this is what happens for System.in static reference. Also note that, the .in refers to the standard console input which is generally the keyboard input and is opened already and supplied by the system which has all the versions of overloaded read(..) methods from the InputStream class implemented. )


abstract class Base {
abstract void m1();
}
class Sub extends Base {
void m1() {}
}
class Test {
void m2() {
Base baseRef = new Sub();
}
}



From Java API Doc
public static final PrintStream out

The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

regds
maha anna

[This message has been edited by maha anna (edited April 14, 2000).]

Jane Rozen
ranch hand
posted April 14, 2000 02:11 PM             
quote:

But read() is abstract. That means when you say System.in.read() you are calling a abstract method. Why does read method still work.

What I meant was - you don't use method read() from a superclass, but an implementation of this method, because System.in is a predefined object of a subclass of InputSream which allows you to read info from a keyboard.

(Maha as usual gave already very detailed answer!)

[This message has been edited by Jane Rozen (edited April 14, 2000).]

|