Reusing Classes

Composition syntax
Inheritance syntax
Initializing the base class 
Constructors with arguments
The preceding example has default constructors; that is, they don’t have any arguments. It’s easy for the compiler to call these because there’s no question about what arguments to pass. If your class doesn’t have default arguments, or if you want to call a base-class constructor that has an argument, you must explicitly write the calls to the base-class constructor using the  super keyword and the appropriate argument list: 
In addition,  the call to the base-class constructor must be the first thing you do in the derived-class constructor. (The compiler will remind you if you get it wrong.) 
Delegation
A third relationship, which is not directly supported by Java, is called delegation. This is midway between inheritance and composition, because you place a member object in the class you’re building (like composition), but at the same time you expose all the methods from the member object in your new class (like inheritance).
Combining composition and inheritance
Guaranteeing proper cleanup
The  try keyword indicates that the block that follows (delimited by curly braces) is a guarded region, which means that it is given special treatment. One of these special treatments is that the code in the  finally clause following this guarded region  is always executed, no matter how the  try block exits. (With exception handling, it’s possible to leave a try block in a number of non-ordinary ways.) 
Name hiding
If a Java base class has a method name that’s overloaded several times, redefining that method name in the derived class will not hide any of the base-class versions (unlike C++). Thus overloading works regardless of whether the method was defined at this level or in a base class.
Java SE5 has added the  @Override annotation, which is not a keyword but can be used as if it were.  When you mean to override a method, you can choose to add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding.
Choosing composition vs. inheritance
The is-a relationship is expressed with inheritance, and the has-a relationship is expressed with composition.
protected
Upcasting
Why “upcasting”?
The only thing that can occur to the class interface during the upcast is that it can lose methods, not gain them. This is why the compiler allows upcasting without any explicit casts or other special notation. 
Composition vs. inheritance revisited
One of the clearest ways to determine whether you should use composition or inheritance is to ask whether you’ll ever need to upcast from your new class to the base class. If you must upcast, then inheritance is necessary, but if you don’t need to upcast, then you should look closely at whether you need inheritance.
The final keyword
final data
Blank finals
You’re forced to perform assignments to finals either with an expression at the point of definition of the field or in every constructor. That way it’s guaranteed that the final field is always initialized before use. ( 定义时没有初始化的final变量,需要早构造方法中对其进行初始化)
final arguments
You can read the argument, but you can’t change it. This feature is primarily used to pass data to anonymous inner classes。
final methods
There are two reasons for final methods.  The first is to put a “lock” on the method to prevent any inheriting class from changing its meaning. This is done for design reasons when you want to make sure that a method’s behavior is retained during inheritance and cannot be overridden. 
The second reason for final methods is efficiency.
final and private
Any  private methods in a class are implicitly  final. Because you can’t access a  private method, you can’t  override it. You can add the  final specifier to a  private method, but it doesn’t give that method any extra meaning. 
final classes
Note that the fields of a  final class can be  final or not, as you choose. The same rules apply to  final for fields regardless of whether the class is defined as  final. However, because it prevents inheritance, all methods in a  final class are implicitly  final, since there’s no way to override them.  You can add the final specifier to a method in a final class, but it doesn’t add any meaning. 
final caution
Initialization and class loading 
The constructor is also a static method even though the static keyword is not explicit. So to be precise, a class is first loaded when any one of its static members is accessed. 
Initialization with inheritance
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值