Java Object-oriented Note 5

Source: Internet
Author: User
Tags define local

Encapsulation, inheritance and polymorphism are the three main features of object-oriented, and inheritance is an important means to implement class reuse, but inheritance brings one of the greatest disadvantages, which is to destroy

Packaging. So the next step is to introduce the combination, which is an important way to implement class reuse, which can provide better encapsulation.

When the subclass extends the parent class, you can inherit the field and method from the parent class, assuming that now we have sufficient permissions, the subclass can directly reuse the parent

The field and method of the class, but inheritance, while bringing a high degree of reuse, also poses a serious problem, which seriously destroys the encapsulation of the parent class. In the encapsulation

, we should adhere to this principle: each class should encapsulate its internal information and implementation details, and only expose the necessary methods for use by other classes. But by following

, subclasses can access the field and methods of the parent class directly, thus causing severe coupling between the subclass and the parent class. In other words, the implementation details of the parent class do not

Again, subclasses can access the field and methods of the parent class, and can change the implementation details of the parent class method, causing the subclass to maliciously change the method of the parent class.

In order to ensure that the parent class is well encapsulated and not subject to arbitrary changes in the quilt class, we typically follow these rules when designing a parent class:

1. Hide the internal data of the parent class as much as possible. Try to set all the field of the parent class to the private access type, and do not let the subclass directly access the field of the parent class.

2, do not let subclasses arbitrarily access, modify the method of the parent class. If there are some other tool methods that exist in the parent class that are auxiliary, you should use private access control

Fu Xiu, so that the subclass cannot access the method, and if the method in the parent class needs to be called by an external class, it is decorated with public, but does not want the subclass to override the method.

You can use the final modifier, or if you want a method of the parent class to be overridden by the quilt class, but you do not want to be freely accessible by other classes, you can use protected to decorate the

Method.

3. Try not to invoke the method that will be overridden by the quilt class in the parent class constructor.

As to when to derive a new subclass from the parent class? Not only is it necessary to guarantee that the subclass is a special parent class, but also requires one of the following two conditions:

1. Subclasses need to add additional attributes, not just property value changes.

2, subclasses need to increase their own unique behavior, including adding new methods or overriding the parent class method.

Faced with the problems of inheritance, if only for the purpose of reuse of the class, it does not necessarily need to use inheritance. We can do that with a combination.

If you need to reuse a class, in addition to inheriting the class as a base class, you can also use the class as a combined part of another class, allowing the new class to directly

Re-use the public method of the class. Using a combination, you can embed the old class object as a field in the new class and use it to implement the functionality of the new class, so it is often necessary in the new class

Use private to decorate the old class object that is embedded. such as the following code:

Class Computer{public void CPU () {SYSTEM.OUT.PRINTLN ("computer has CPU");}} Class Cpu{private computer computer;public Cpu (computer computer) {This.computer=computer;} public void CPU () {COMPUTER.CPU ();}} public class Computtest {public static void main (string[] args) {CPU Cpu=new CPU (new computer ()); Cpu.cpu ();}}
Summing up the above, usually, the inheritance relationship is the process of abstracting the common parent class from many subclasses, similar to the process of extracting the embedded class from multiple whole classes in a composite relationship, and the process of deriving subclasses from the parent class in the inheritance relationship, similar to the process of combining the embedded class object into the whole class in the composite relationship. In summary, inheritance is a "Yes (is-a)" relationship, and the combination represents a "have (has-a)" relationship.

In Java, in addition to using a constructor class to initialize a single object, we can also implement it by initializing the block. There can be more than one initialization block in a class, and there is a sequence between the initialization blocks of the same type: the initialization block that was previously defined executes first, and after the initialization block defined later. Initialization blocks that use the static modifier, called static initialization blocks, can define local variables in the initialization block, methods that invoke other objects, and use branches, loop statements, and so on. The following program defines the initialization block:

Class Person{{int num=25; SYSTEM.OUT.PRINTLN ("Normal initialization block 1");} Static{int num=45; SYSTEM.OUT.PRINTLN ("Static initialization Block");} {int num=25; SYSTEM.OUT.PRINTLN ("Normal initialization block 2");} Public person () {System.out.println ("no parameter Constructor");}} public class Maintest {public static void main (string[] args) {new person ();}}

The output results are as follows:

Static initialization blocks
Normal initialization block 1
Normal initialization Block 2
No parameter constructor


As you can see from the running result, when you create a Java object, the system always calls the static initialization block first, then executes the normal initialization block sequentially, and then executes the

constructor. Static initialization blocks are class-dependent and are used to initialize the entire class, and static initialization blocks are also referred to as class initialization blocks, which also belong to class static

Members, and therefore cannot access non-static members.

Finally, it is worth noting that when the JVM first actively uses a class, the system allocates memory for all static field of the class during the class preparation phase, during initialization

Phase is responsible for initializing these static field, initializing static field is the initial value specified when executing class initialization code or declaring class field. About class loading please

See " Java class Load notes (1) "




Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43876627 Emotional Control _  



























Java Object-oriented Note 5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.