is System.in an object reference of InputStream class?

后端 未结 3 958
不知归路
不知归路 2021-01-16 16:38

InputStream is an Abstract class.Then how are we able to access System.in.And moreover int read() is an abstract method in InputStream class.Then how are we able to access S

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 17:03

    InputStream is an abstract class which cannot be instantiated directly. System.in refers to the object of type InputStream which means that System.in refers to the object of that class which extends the InputStream class.

    For example

    abstract class IAmAbstract{
    // ...
    }
    
    class IAmNotAbstract extends IAmAbstract{
    // ...
    }
    

    Of course, the following statement is correct:

    IAmNotAbstract obj = new IAmNotAbstract();
    

    As well as this statement is also correct:

    IAmAbstract obj = new IAmNotAbstract();
    

    So, any object of subclass of InputStream is also a type of InputStream class and subclass itself.

提交回复
热议问题