Java study Note 6-Java class inheritance and polymorphism (1)

Source: Internet
Author: User

1. Java class inheritance

1. Why inherit?

A: inheritance can make the object description clearer and implementCodeCan be used to override the variables or methods in the class.Source codeModify the inherited class.

2. How do Java classes implement inheritance?

A: The Inheritance of Java classes is single and completely inherited. It is implemented through the keyword extends. The inherited class is called the parent class, and the class that inherits the parent class is called a subclass. Inheritance Syntax: Public calss classname extends superclassname. Because Java class inheritance is single, only one parent class can be specified after extends. Full inheritance means that "a class inherits all the member variables and member methods of the parent class.

For example, in the plane coordinate system of class 1, set X and Y. To define another spatial coordinate system, you only need to inherit Class 1, add a Z axis to its subclass.

Public ClassOne {IntX;IntY;PublicOne () {x= 0; Y= 0;}}

Then define class 2 and inherit Class 1 through Class 2:

Public ClassTwoExtendsOne {//Inheritance class oneIntZ;PublicOne () {z= 0;}}

In this way, class inheritance is realized.

3. What about hiding, overwriting, and overloading in class inheritance?

Answer: (1) hiding a class: Hiding occurs between the subclass and the parent class, for the member variables and static methods in the parent class. When the Declaration in the subclass is the same as the member variable name declared in the parent class, the member variable of the parent class is hidden.

(2) overwrite: overwrite also occurs between the subclass and parent class, A subclass declares a method with the same method name, input parameter list, return value, and access permission as the parent class, except for the method body. Rewrite the method implementation function in the subclass.It is often used to replace methods with the same parent class to update functions.

Example:

ClassThree {Public VoidDisplay (){//Define the display method in the parent classSystem. Out. println ("This is class three");}}ClassFourExtendsThree {//Inheritance Class ThreePublic VoidDisplay (){//Overwrite the display method in the three of the parent classSystem. Out. println ("this is Class Four");}}

(3) Class overloading: it can occur between the parent class and the subclass, or in the same class. It refers to defining multiple methods with the same method name/access permission between the subclass and the parent class or in the same class.

Example:

Class  Five {  Public   Void Display (){ //  Display Method in parent class System. Out. println ("This is class five" );}}  Class Six Extends Five { //  Inheritance Class Five      Public   Void Display (string s ){ // Display Method in parent class  System. Out. println (s );}}  Public   Class  Seven {  Public   Static   Void  Main (string [] ARGs) {Six = New  Six (); A. Display ();  //  Call the display method in the parent class A. Display ("this is Class Six "); // Call the display method in subclass  }} 

4. overload of the constructor.

In the preceding example, six A = new six (); New is followed by the class constructor. If the class does not have a constructor, the default constructor without any operation is called when a class object is created. When a subclass has its own constructor without a form List, the parent class object is created before the subclass object is created.

The following code is used:

 //  First class ------- parent class  Public   Class  Output {  Public  Output () {system. Out. println ( "This is class output" );}}  //  The second class ------- subclass Public   Class Input Extends  Output {  Public  Input () {system. Out. println ( "This is class input" );}}  //  Category 3  Public   Class  Sample {  Public   Static   Void Main (string [] ARGs) {input = New  Input ();}} 

The final output of the above Code is:

This is class output

This is class Input

It can be seen that if there is no constructor method at the end, the parent class object will be called after the object is created, and then the subclass object will be called.

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.