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

后端 未结 4 1234
粉色の甜心
粉色の甜心 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:47

    let's take an example: sup is a reference variable for a class A's objects. and sub is a reference variable for a class B's objects. let's suppose B extends A . so if we write "sup = sub;" or "sup = new B();" this statement enables us to get the attributes that are common between the 2 classes . i.e : the attributes that were inherited by B ... we use this trick to check for attributes inherited by B and were changed.

提交回复
热议问题