Polymorphism

Polymorphism is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.
Upcasting revisited
Forgetting the object type  
The twist
Method-call binding
Producing the right behavior
Extensibility
Polymorphism is an important technique for the programmer to “ separate the things that change from the things that stay the same.” 
Pitfall: “overriding” private methods
The result of this is that only  non-private methods may be overridden, but you should watch out for the appearance of overriding private methods, which generates no compiler warnings, but doesn’t do what you might expect. To be clear, you should use a different name from a private base-class method in your derived class.  A private method is automatically final, and is also hidden from the derived class.
Pitfall: fields and static methods
Once you learn about polymorphism, you can begin to think that everything happens polymorphically.  However, only ordinary method calls can be polymorphic. For example, if you access a field directly, that access will be resolved at compile time.
If a method is static, it doesn’t behave polymorphically.
static  methods are  associated with the class, and not the individual objects.
Constructors and polymorphism
Constructors are not polymorphic ( they’re actually  static  methods, but the static declaration is implicit)
Order of constructor calls
This means
The order of constructor calls for a complex object is as follows:
1.  The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached. 
2.  Member initializers are called in the order of declaration. 
3.  The body of the derived-class constructor is called. 
Inheritance and cleanup
Behavior of polymorphic methods inside constructors 
The only safe methods to call inside a constructor are those that are final in the base class. (This
also applies to private methods, which are automatically final.)
 The actual process of initialization is:
  •  The storage allocated for the object is initialized to binary zero before anything else happens.  
  •  The base-class constructors are called as described previously. At this point, the overridden draw( ) method is called (yes, before the RoundGlyph constructor is called), which discovers a radius value of zero, due to Step 1.  
  •  Member initializers are called in the order of declaration.  
  •  The body of the derived-class constructor is called. 
Covariant return types
Designing with inheritance
A general guideline is “Use inheritance to express differences in behavior, and fields to
express variations in state.”
Substitution vs. extension 
Downcasting and runtime  type information 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值