Assigning subclass object to Superclass instance type is only for overriding concept?or we are doing this for something else also?

前端 未结 3 804
你的背包
你的背包 2020-12-16 07:13

Do you assign a subclass object to a Superclass instance type only for overriding? Or are we doing this for something else also?

Example: Sub is subtype of Super and

3条回答
  •  独厮守ぢ
    2020-12-16 07:56

    The characteristics of OO design that allows this (to handle object as instances of a common supertype) is called polymorphism.

    One would assign a sub-type instance of to a supertype variable to handle all possible subtype classes in a uniform fashion, e.g. using methods declared (but possibly overriden) by the supertype class.

    Related OO topics you might find interesting to read about:

    • abstract classes
    • interfaces

提交回复
热议问题