Java Fundamentals-facing objects-inheritance

Source: Internet
Author: User
Tags repetition

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

Inherited:

Inheritance is the possession
Efficient code reuse (adding and upgrading on the basis of the original code, called the new feature)
Java can only inherit single
Inheritance is the use of, inherit everything that has the parent class.
Inheritance Keyword: This class extends parent class
When you create a subclass object, you create the parent class object, and then you create the child class object.
The subclass object is created, the subclass is first out of the parent class, the subclass method is always considered when the object is called, and the parent class is called if the subclass does not.
Rewrite: In the inheritance, subclasses feel that the parent method is outdated or problematic, you can declare a child class in the same way as the parent class.
Super Keyword:
1. Parent Class object
2. Select the parent class constructor (must be written in the first row of the constructor code)

How to apply:
Write on the extends parent class that has everything except the private property method.
Private to be accessible in the code class.

Example 1:
Code Duplication issues:
For example, we write a game in the Wild Monster class.
Cannibal Flower: Attribute (name, blood bar, attack)
Method (attack, attack).
Scarecrow: attributes (name, Blood bar, attack, speed of action)
Method (attack, attack)
Chicken: attributes (blood bar, speed of action, chicken quality)
Method (under attack)
In the above monster classes, we can find that the properties and methods they have are duplicated.
So we need to use inheritance to improve the repetition rate of the code.

We can write the common properties and methods of wild monsters in a class, by inheriting in the original properties and methods to add properties and methods to build a movable, non-movable, in the movable monster class to add a pair of properties and methods to construct the strange Movable monster Class (Scarecrow, Zombie). Non-removable addition of the corresponding property method constructs the corresponding non-movable monster class (Cannibal tree, Cannibal Flower).
Benefits of Inheritance:
1. Reduce the problem of code duplication (if you write a scarecrow, zombie, Cannibal, cannibal tree, you need to re-write, re-customize these property methods)
2. Speed up Code development
3. The code structure is clear and meticulous.

Example 2:
For example, the windows of our computers, their common properties (width, height, x-coordinate, y-coordinate)
Method (change size, change position)
You can derive a text width, a button, and then derive a single line of text width and a multiline text width in the text width, based on the code of the parent class.
Based on the original code is upgraded to add a new attribute method to create a new product.
This saves the development time, reduces the code repetition rate, the code structure is clear and meticulous.

Example 3: We create a mobile phone class.
Figure 1: First create the most basic common properties of the phone, methods.
(Call, ring, answer phone)

Figure 2:
Apply for a mobile phone class, inherited from the mobile phone 1.
Once you inherit, you don't have to write your phone 1 code.
It added text messages and received SMS messages.
Add new features to new products based on mobile 1.

Figure 3:
In the application of a mobile phone class, inherited from the mobile phone 2.
Add 17951 automatically when making long distance calls.
Re-cover the previous phone 1 call method.

Rewrite:
Only need to write a 11 touch with the same call method can be.
When the parent call method is not good, you can override the method of the parent class with overrides.
Called when the child class is considered, and the parent class is considered. (First call phone 3 call method, no words to consider the parent class call method)

Super Keyword Example:
1. Parent class object.
Apply for a, B, Class C, b inherit a,c B.
New C, it's called the C call B,b and then call a.
The nearest principle is that the parent class is unaware that there are subclasses inheriting it.

    1. Select the parent class constructor.
      Apply Test1, Test2 class, Inside write a constructor, Test2 inheritance and Test1.
      The new Test2 constructor, which prints the results first out of the parent class, and then out of the subclass.

Cause of Error:
Figure 1: The parent class has a parameter constructor, which should be passed to the parent class constructor.

Figure 2: Solution to the solution
Add the Super keyword to pass the parameter to the parent class.
Super (name); Declares the statement, declaring that the object is constructed using the parent class with the parameter constructor.

Java Fundamentals-facing objects-inheritance

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.