Java Learning Diary-6 Inheritance

Source: Internet
Author: User

Inherited
1. Basic Introduction
One of the three main principles of object-oriented programming. Inherited is called a parent class, and an inherited class is called a subclass.
Keyword: extends
Example:

1 classtwodshape{2   Doublewidth;3   Doubleheight;4   5   voidShowdim () {6System.out.println ("Width and height are" +width+ "and" +height);7   }8 }9 Ten classTriangleextendstwodshape{ One String style; A    -   DoubleArea () { -     returnWidth*height/2; the   } -   voidshowstyle () { -System.out.println ("Triangle is" +style); -   } +}

Subclasses inherit the members of the parent class (variables and methods), and can have their own unique members.
Java does not support multiple inheritance, and a class can have only one parent class.

2. member access control in the inheritance
Inheritance does not break through private access restrictions, and subclasses cannot directly access private members in the parent class, even if he owns those members

3. Inheritance and constructors
When you generate an object of a class that has an inheritance relationship, the constructor of the parent class constructs the parent class part, and the constructor of the subclass constructs the subclass part.
The first line of code in the subclass constructor must be super (Parameter-list) to call the parent class constructor.

4.super/this
If a subclass has a member with the same name as the parent class, the child class member overrides the parent class member, and if you want to invoke the parent class member, use Super. Member name
When invoking members of this object, use the this. Member name
This () means that another constructor is called in one constructor and must be the first valid statement in the constructor
Super () means that a constructor of the parent class is called in a constructor in a subclass and must be the first valid statement in the constructor
Note: Super (), this () all require the first position, so they can no longer coexist in the same constructor!!

5. References
The parent class reference can refer to the subclass object, but only the parent class member is called, and if the child class member is called, the reference is strongly converted to a subclass reference.
A subclass reference cannot reference a parent class object!

6. Method overrides
There can be a method in a subclass whose return type is exactly the same as a method in the half, but the method body is different, so the
The situation is called rewriting. Methods in the parent class are overridden by the quilt class, and the Super keyword is required to invoke the overridden method in the parent class.
The overriding mechanism allows the parent class to reference a variable by referencing a different subclass object, using the same method name to implement different functions, when the Java implementation
A means of polymorphism.
Note: The parent class references a variable that refers to a subclass object, and when the overridden method is called, the method that is overridden in the subclass is called, not the parent class that is heavily
Wrote the method.

7.final
The method cannot be overridden before the method
The class cannot be inherited before the class name
The variable becomes a constant before the member variable

8.Object class
The object class is the parent class of all classes

Java Learning Diary-6 Inheritance

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.