Superclass reference to subclass object showing same behaviour as subclass reference to subclass object

后端 未结 4 1238
粉色の甜心
粉色の甜心 2020-12-13 05:03

The following code in java, when run on elipse, gives same output even if we replace

superclass s=new sub();

with,

sub s= n         


        
4条回答
  •  遥遥无期
    2020-12-13 05:39

    you are creating Object of sub and assigning it to super's reference.if you dont 1)first case:-

    superclass s=new sub();   
            s.supermethod();
            s.method();
    

    in above case methods of sub will be called but these two method should be present in superclass also(if not present then compile time error)

    2)second case:-

    sub s=new sub();   
        s.supermethod();
        s.method();
    

    in this also methods of sub will be ,these method may or may not be present super.

提交回复
热议问题