There are two main ways of reusing classes: combination, inheritance
Combination
Cases:
Class A {}
Class B {a A;}
Inherited
Inheritance is an integral part of all OOP languages and the Java language. When a class is created, it is always inherited, so unless explicitly stated to inherit from another class, it is implicitly inherited from the Java standard root class object.
Inheritance application Keywords: extends, inheritance automatically gets all the fields and methods in the base class
Super
The Super keyword denotes the meaning of the superclass, and the current class inherits from the superclass. Call Super class Method: Super.funcname ();
Initialize base class
Without a parameter constructor, Java automatically inserts a call to the base class constructor in the constructor of the parent class.
With the parameter constructor, you must explicitly write the statement that calls the base class constructor with the keyword super, with the appropriate argument list, and the super statement must be placed at the beginning of the subclass constructor, such as:
Class A {a (int i) {print ' A ';}}
Class B extends B {b (int i) {super (i);p rint ' B '}}
Agent
You can have more control when using proxies, because we can choose to provide only a subset of the methods in the member object
Protected keywords
This is private for the class user, but it is accessible to any export class that inherits from this class or any other class that is in the same package. Projected also provides in-package access rights
Although you can create a projected domain, the best way is to keep the domain private; you should always keep the right to change the underlying implementation. The projected method is then used to control the access rights of the inheritors of the class.
Upward transformation
"Providing methods for new classes" is not the most important method of inheritance technology, and the most important aspect is to represent the relationship between the new class and the base class. This relationship can be summed up with the phrase "The new class is a type of an existing class."
Converting an exported class reference to a base class reference is called an upward transformation.
Since the upward transformation is converted from a more specialized type to a more generic type, it is always safe to do so. In other words, the export class is a superset of the base class. It may contain more methods than the base class, but it must have at least the methods contained in the base class. In the process of upward transformation, the only thing that can happen in the class interface is the loss of methods, not the acquisition of them. This is why the compiler still allows for an upward transition in situations where "no explicit transformation" or "no special tag has been specified".
In object-oriented programming, the most common is the direct development class, or the use of combinatorial techniques to develop new classes using existing classes. And the inheritance technology is actually not very common,
But inheritance technology is actually not very common. So, although we have repeatedly emphasized inheritance in the course of teaching oop, that does not mean using it as much as possible. Instead, this technique should be used with caution, and its use is limited to situations where you are sure that the technology is actually valid. Whether it's a combination or a succession, one of the clearest ways to judge it is to ask yourself if you need to move up from a new class to a base class.
Inheritance is necessary if it has to be transformed upward.
Final keyword
Depending on the context, there is a subtle difference in the meaning of the Java keyword final, but it usually refers to "this cannot be changed". There are two reasons why you might not want to change: design and efficiency. Because these two reasons are far apart, the keyword final may be misused
Final data
A domain that is both static and final occupies only a certain amount of storage space that cannot be changed.
When final is applied to an object reference instead of the base type, final causes the value to be constant for the base type, and for the object reference, the final
Make the reference constant.
That is, the static and final fields (that is, compiler constants) are represented in uppercase, and the words are separated by underscores;
Final parameter
Java allows you to specify the parameters as final in the parameter list declaratively. This means that you cannot change the object that the parameter reference points to in the method
Blank final
Java allows the generation of "blank final", the so-called blank final refers to a domain declared final but not given an initial value. In either case, the compiler ensures that the blank final must be initialized before it can be used. However, the blank final provides greater flexibility in the use of the keyword final, so that the final domain in a class can be different depending on the object, while maintaining its constant characteristics.
Such as:
public class Test {
private final int i;
Private final string[] arr;
Public Test () {
i = 1;
arr = {"A", "B"};
}
Public Test (int i, string[] arr) {
THIS.I = i;
This.arr = arr;
}
}
Final method
There are two reasons to use the final method. The first reason is to lock the method in case any inherited class modifies its meaning. This is a design consideration: you want to make sure that the method behavior remains the same in inheritance and is not overwritten.
Final class
When you define the whole of a class as final (by placing the keyword final before its definition), you do not intend to inherit the class, and you do not allow others to do so. In other words, for some reason, you never need to make any changes to the design of the class, or for security reasons, you don't want it to have subclasses.
This article is from the "Small City Ops" blog, please be sure to keep this source http://lixcto.blog.51cto.com/4834175/1895370
"Java Programming thought" study notes--seventh Chapter reuse class