Member variables are hidden, method overrides, Super keyword

Source: Internet
Author: User

The hiding of member variables: when the parent and child classes have the same member variable, the child class is hidden from the parent class variable when it defines the same member variable as the parent class. For subclasses of objects, a member variable with the same name in the parent class is hidden, and the subclass takes precedence over its own member variable, and the parent class member is hidden.

public class Yincang {
public static void Main (String []args)
{
SonA son=new SonA ();
Son.showson ();
Fathera father=new Fathera ();
Father.showfather ();
}
}
Class fathera{
int x1=1;
int x2=2;
void Showfather () {
System.out.print ("x1=" +x1+ "\ n");
System.out.print ("x2=" +x2+ "\ n");
}
}
Class SonA extends Fathera
{
int x1=11;
int y1=22;
void Showson () {
System.out.print ("x1=" +x1+ "\ n");
System.out.print ("x2=" +x2+ "\ n");
System.out.print ("y1=" +y1+ "\ n");

}
}

PS: The program hides the X1 in the parent class output the X1 in the subclass. Hidden and type-independent, when X1 in the parent class is changed to double type, subclass or int, the output is the int of the subclass.

Hides the modifier regardless of whether the X1 in the parent class is the modifier for the protected subclass X1 or unchanged, and remains the default.

Override of Method: Subclasses and parent classes can define the same method name, when, for objects of subclasses, they call their own members, overriding the member methods of the parent class.

public class Fugai {
public static void Main (String []args)
{
Fatherb father=new Fatherb ();
Father.show ();
SONB son=new sonb ();
Son.show ();
}
}
Class fatherb{
int x1=10;
int x2=20;
void Show () {
System.out.print ("x1=" +x1+ "\ n");
System.out.print ("x2=" +x2+ "\ n");
}
}
Class Sonb extends fatherb{
int y1=30;
int y2=40;
void Show () {
System.out.print ("y1=" +y1+ "\ n");
System.out.print ("y2=" +y2+ "\ n");
}

}

PS: In this program, there is a member method in the parent class Fatherb show (), and in subclass SONB There is also a member method show (), then the object that creates the subclass son, calls its own Member method show (), and the member method in the parent class is overwritten. Only the object father that creates the parent class is called its own member method. The override of the member variable must be the method name, the parameter type, the order, the number, and the return value exactly.

If the method name is the same, the parameter type, the number, the order is not the same when the subclass inherits down to form the overload. Overloads require the same method name, parameter type, number, and order.

public class Chongzai {
int get (int x) {
return x;
}
Class B extends chongzai{
int get (int x,int y) {
return x+y;
}
}
Class test1{
public void Main (String []args) {
b bb=new B ();
System.out.print (Bb.get (4));
System.out.print (Bb.get (4,5));
}
}
}

PS: In this program, The Get () method of the parent class takes a parameter, the Get () method of the subclass takes two parameters, the subclass inherits the methods in the parent class, and the two Get () methods form an overloaded relationship. The object that creates the subclass, the statement "Bb.get (4)" matches the Get () method in the parent class. The statement "Bb.get (4,5)" matches the subclass's own method.

If the method name, parameter type, number, order is exactly the same, only the return value is different then not overloaded or overwritten.

The final method in the parent class cannot be overwritten, and if the method in the parent class is final, it is represented as the final method and cannot be overridden by the quilt class, that is, the final method can be inherited and used by the class, but cannot be modified or redefined in the subclass, which is similar to the concept of constants.

You cannot overwrite a static method in a parent class, but you can hide it. That is, a static method of the same name declared in a subclass actually hides the static method of the parent class.

Super Keyword: If the child class and the parent class have the same member variables and methods, the subclass hides and overrides the member variables and the member methods of the parent class, using the subclass's own member variables and methods. But what if the subclass wants to access the member variables and member methods of the parent class? The solution is to use the Super keyword to use super when using members that are hidden and overwritten in the parent class, in the following format:

Super. Parent class member variable name

Super. Parent class Member Method name

For example:

public class Superlei {
public static void Main (String []args)
{
Employee Emp=new employee ();
Emp.sete ();
Emp.show ();
}
}
Class person{
protected String name;
protected char sex;
void Show () {
System.out.println ("The name in the parent class is:" +name);
System.out.println ("Gender:" +sex);
}
}
Class Employee extends person{
protected int salary;
protected String name;
void Sete () {
Name= "Zhang Qian";
Super.name= "Reese";
salary=3000;
Sex= ' man ';
}
void Show () {
System.out.println ("Name in Subclass:" +name);
System.out.println ("Gender:" +sex);
}

}

PS: To assign a value to the member variable name of the parent class, you must take a super call to the parent class's member Variable name assignment "Super.name=" Reese "". Similarly, subclasses and parent classes have the same output method as the show () method, in the show () method of the subclass, the show () method to invoke the parent class must also call "Super.show ()" through super.

Subclass Construction Method: In the previous program, the parent class does not have a write constructor method, when the parent class defaults to a non-parametric construction method. When you define a subclass, you must unconditionally inherit the parameterless constructs in the parent class, which means that the subclass defaults to calling the default parameterless constructs in the parent class. When the parent class has a constructor that has parameters, the subclass must call the constructor method in the parent class. The constructor method for subclasses to call the parent class is:

1: Subclasses are unconditionally inherited for the parent class that does not contain a parameter construction method.

2: If the parent class is a constructor with arguments, the subclass cannot inherit the parameterless construct by default, and must write super to call the parent class construction method.

3: If other construction methods in this class are called through this () in the subclass construction method, the super () is no longer called by default.

public class Gouzaofangfa {
public static void Main (String []args)
{
Qun qun=new Qun (3,4,5,4.5f);
Qun.show ();
Qun.quntity ();
}
}
Class rect{
int length;
int width;
int high;
Rect (int l,int w,int h) {
This.length=l;
This.width=w;
This.high=h;
}
int vert () {
int V=length*width*high;
return v;
}
}
Class Qun extends rect{
float p;
Qun (int lx,int wx,int hx,float px) {
Super (LX,WX,HX);
P=PX;
}
void Quntity () {
float m;
M=p*vert ();
SYSTEM.OUT.PRINTLN ("Quality is" +m);
}
void Show () {
System.out.println ("long as:" +length);
System.out.println ("width is:" +width);
System.out.println ("High as:" +high);

}
}

PS: The parent Rect rectangle class defines a constructor with parameters, giving long, wide, high assignments. In his derived class Qun class, you must define a constructor with parameters, and typically take 4 parameters, because the constructor of a subclass needs to assign a value to the length and height of the parent class and its own density. In the constructor of a subclass, the first sentence must be the constructor of the parent class called Super "super (LX,WX,HX);", he cannot be placed after the other statement "P=PX".

Member variables are hidden, method overrides, Super keyword

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.