Summary: When a subclass parent class has a method with the same name ...

Source: Internet
Author: User

Example:

public class Parentchildtest {
public static void Main (string[] args) {
Parent parent=new parent ();
Parent.printvalue ();
Child child=new Child ();
Child.printvalue ();

Parent=child;
Parent.printvalue ();

parent.myvalue++;
Parent.printvalue ();

((child) parent). myvalue++;
Parent.printvalue ();

}
}

Class parent{
public int myvalue=100;
public void Printvalue () {
System.out.println ("Parent.printvalue (), myvalue=" +myvalue);
}
}
Class Child extends parent{
public int myvalue=200;
public void Printvalue () {
System.out.println ("Child.printvalue (), myvalue=" +myvalue);
}
}

Summarize:

* If the subclass has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the sub-class method accesses the fields in the subclass (not the fields in the parent class).     If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword. * If a subclass is used as a parent class, the field accessed through the subclass is the parent class! Keep in mind: * in real development, avoid defining a field with the same name as the parent class in the child class. Don't ask for trouble!

Appendix: Some code and explanations that are extracted on the Web can help understand.

Example 1:

public class A {
protected String A = "1";

Public String Geta () {
return THIS.A;
}
}

public class B extends A {

protected String A = "2";

Public String Geta () {
return A;
//    }

public static void Main (string[] args) {

b x = new B ();
System.out.println (X.geta ());
}
}

The output is 1, and the parent class's method sees a variable that is a in the parent class.
If a Geta method is also written in B, see A is a in the subclass.

is actually the problem of visibility.
The variables for the parent and child classes exist at the same time, even with the same name.
A child class sees a variable in a subclass, and a variable in the parent class is seen in the parent class.
They are hidden from each other, and the method with the same name is a tangible overlay.

such as a x = new B ();
X is a B, and a,
Then when the method is called, it is called based on the actual type of the object.
The actual type is B, so the method of the subclass is called forever.

and the access member variable is different, it is B, access is a subclass of the member variables,
When you transition to a, you are accessing the member variables of the parent class.

Example 2:

Main.java
Class C
{
public int name = 12;
}
Class D extends C
{
D ()
{name = 13;}
void D2 ()
{
System.out.println (Super.name);
}
}
public class Main
{
public static void Main (String[]args)
{
D d1 = new D ();
System.out.println (D1.name);
D1.d2 ();
}
}
The result of the operation is: 13,13
Main1.java
Class C
{
public int name = 12;
}
Class D extends C
{
int name = 13;
void D2 ()
{
System.out.println (Super.name);
}
}
public class Main
{
public static void Main (String[]args)
{
D d1 = new D ();
System.out.println (D1.name);
D1.d2 ();
}
}
The result of the operation is: 13,12

The first thing to know about super is the meaning of the keyword, called the parent class,
void D2 ()
{
System.out.println (Super.name);
This is called the name of the parent class, although there is a name in the subclass, but it does not overwrite the name inside the parent class, but the name of the parent class is hidden, so that the name in the direct call subclass shows the name of the subclass definition.
The first main one. Java, needless to say, a subclass does not define a name variable, and the name variable inside the parent class is not private, the subclass can inherit the name directly, and the class and parent class share a name variable

If the parent class defines a method. Subclasses override this method. Then these two methods actually have their own memory.

Example 3:

Class parent{

int i=10;//Parent class variable

public void SetI (int i) {

This.i=i;

}

}

Class Son extends parent{

int i=10;//A variable with the same name as the parent class

public static void Main (String args[]) {

Son son=new son ();

System.out.println ("son.i=" +son.i);

Son.seti (100);

System.out.println ("After SetI: son.i=" +son.i);

Parent Parent=son;

System.out.println ("See Son as Parent:son.i=" +parent.i);

}

}

In this code, the subclass defines a member variable with the same name in the parent class, int I, a method that assigns a value to I in the parent class, SETI (), and the method is not defined in the subclass. When a subclass calls the inherited Seti () method to change the member variable I, the member variable i does not change when printing son.i directly. When you use son as the parent type and then print its member variable i, the result of the output is the value of the SETI () change.

Classes in Java are hierarchical, and when the definition of a subclass and a parent class has the same name, the parent class variable is hidden, the instance method of the parent class is overridden, and the static method belongs to the class-level method, which is not in the subclass and parent class.

Example 4: Override overriding and overloaded overloading for a method.

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. The overloaded method is to change the type of the return value. That is, the overloaded return value types can be the same or different.

1, heavy load (overloading)

A, method overloading is a means for a class to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different number/types of parameters. Overloaded overloading is a representation of polymorphism in a class. B, Java's method overloading, is that you can create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use.

C, when overloaded, the method name is the same, but the parameter type and number are different, the return value type can be the same or different. The return type cannot be used as a distinguishing criterion for overloaded functions.

2. Rewrite (overriding)

A, the polymorphism between the parent class and the subclass, redefining the function of the parent class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). In Java, a subclass can inherit a method from a parent class without rewriting the same method. But sometimes subclasses do not want to inherit the parent class's methods, but want to make some changes, which requires a method of rewriting. Method overrides are also called method overrides. B, the method in the Kawai class has the same method name, return type, and parameter table as a method in the parent class, and the new method overwrites the original method. If you need a method from the parent class, you can use the Super keyword, which references the parent class of the current class.

The access adornment permission for a subclass function cannot be less than the parent class's

Summary: When a subclass parent class has a method with the same name ...

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.