Java inheritance constructor overload, rewrite

Source: Internet
Author: User

 

 

Override)

Class Base

{

Public void function1 () // method without Parameters

{

Function 1;

}

Public void function1 (Datatype var1) // method with one parameter

{

Function 2;

}

}

 

Class Sub extends Base

{

Public void function1 ()

[

// If the function of function1 is identical to that of the parent class, call function1 () of the parent class directly ()

Super. function1 ();

// If you want the function1 () function of the subclass to be different from that of the parent class, you need to re-write the function1 () function of the subclass ()

// Various custom functions...

}

}

 

 

 

========================================================== ========================================================== =

 

Overload)

By default, subclass inherits all public methods of the parent class, and private methods cannot be obtained.

The method overload is the same function name, but the parameter list in the method is different. It is the same as the general method overload.

Therefore, when the function of the parent class is private, the function of the subclass is newly created and has no relationship with the parent class.

Class Base

{

Public void function1 () // method without Parameters

{

Function 1;

}

Public void function1 (Datatype var1) // method with one parameter

{

Function 2;

}

}

 

Class Sub extends Base

{

Public void function1 (different from the parameter list of the parent class)

{

Function;

}

}

========================================================== ========================================================== =

========================================================== ========================================================== =

Call the parent class Constructor

Subclass cannot obtain the constructor of the parent class

The subclass cannot directly use the constructor of the parent class, but you can use super (value) in the constructor of the subclass to call the constructor of the parent class.

 

Class Base

{

Base () {}// a constructor without Parameters

Base (Datatype var)

{

Function 1;

}

Base (Datatype var1, Datatype var2)

{

Function 1;

Function 2;

}

}

 

Class Sub extends Base

{

Sub () {}// subclass without Parameters

 

Sub (Datatype var1) // Sub-class constructor with a parameter

{

Super (var1 );

Other functions;

}

Sub (Datatype var1, Datatype var2) // subclass constructor with two parameters

{

Super (var1, var2 );

Other functions;

}

 

Public static void (String [] arg)

{

// Use the constructor of the Child class in this way. The constructor of the parent class is used to initialize

 

// Incorrect use

// New super (var1, var2); the constructor of the parent class cannot be called directly.

}

}

 

System. out. println ("FatherA constructor without Parameters ". name = System. out. println ("FatherA constructor with a parameter:" + FatherB System. out. println ("FatherB constructor without Parameters" System. out. println ("FatherB with a parameter constructor:" + FatherB (String name ,. age = System. out. println ("FatherB constructor with two parameters:" + name + ":" + FatherB (String name, (name, age );. sex = System. out. println ("FatherB constructor with three parameters:" + name + ":" + age + ":" + System. out. println ("hello world" System. out. println ("base class function" SonClass () System. out. println ("calling the constructor without parameters of its parent class" (name); System. out. println ("explicitly calls a parameter .. "SonClass (String name, System. out. println ("call two parameters... "SonClass (String name, System. out. println ("Call the three parameters... "myFunction (String name) System. out. println (name + "hello world" myFunction () System. out. println ("china hello world" SonClass (); System. out. println ("==========================" SonClass ("hello "); system. out. println ("==========================" SonClass ("china ", 20 System. out. println ("========================" SonClass ("japan", 20, "man" System. out. println ("=============================" (System. out. println ("=============================" (SonClass ()). myFunction ("admin "}

 

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.