Java Parent class subclass inherits the same name as a variable with the same name

Source: Internet
Author: User


This interesting question that I stumbled upon.

Observe the following code, which has a String name variable defined in both the parent class and the subclass, and whether the final inheritance is left with 1 name only or the name of the parent subclass subclass is irrelevant.

By assigning a value, what the final output is. See below

public class Son extends parent{
    String name;                The name  
    void value () {  
        name = ' A ' in a subclass;   
        Super.value ();          Invokes value () in the parent class, assigning a value of B  
        System.out.println (name) to the name;       Result output a  
        System.out.println (super.name);//Results Output B  
                } public  


 static void Main (string[] args) {
     Testtest Test =new testtest ();
     Test.value ();
                                       }
                               End Class 

 class Parent{<span style= "White-space:pre" >				</span>       //define parent class
		String name ;                   The name
		void value () {
			name = ' B ';
		             }
	            } in the parent class End Class


Output:

A

B

This shows that the subclass first Name= "A", even if the name = "B" in the parent class value () is invoked,

SYSTEM.OUT.PRINTLN (name); is still output a, the visible subclass does not change the value of name in the subclass by referencing the parent class method, even if the variable name of the assignment is

However, the change is the name variable in the parent class, so System.out.println (super.name), Output b

In general, the parent class and subclass define variables with the same name, and they do not overwrite, but they also have their own space, even if the variable name is the same

A method that invokes the parent class's assignment to the name of the variable named in a subclass, still simply changes the value of the parent's own name variable

Look at one again, combine the parent class reference, polymorphism problem

public class Son_test extends Parent
	{

		int i = ten;

		public static void Main (string[] args)
			{

				son_test son_test = new Son_test ();

				System.out.println ("son.i=" + son_test.i);//output subclass of i=10;
				
				Son_test.set (m);  Invoke the inherited parent class method, from the above, the only change is the parent's own I
				System.out.println ("son.i=" + son_test.i);//    output is still a subclass of i=10
				
				
				Parent son =son_test;                   Polymorphism, the subclass object is assigned to the parent class reference, and the  reference type is parent 
				System.out.println (SON.I);             The output is unexpectedly the parent class of the i=100,,,,,,,
				
			}

		
	}


class parent
	{

		int i = ten;

		public void set (int i)
			{

				this.i = i;
			}
	}
Output:
son.i=10
son.i=10
100


Thus, when the parent class has a variable with the same name, the reference type is the parent class, even if it is an object of the subclass, I in the son.i is the I in the parent class.


Boundary


We know that as Parent a = new Son (); Where son is parent, using the reference a. Variable or a. method, even if there is a corresponding variable or method in the son class, and there is no a. Variable or a. method in the parent class, it cannot be compiled.


However, if there are corresponding variables and methods in the parent class, and there are also variables and methods with the same name in the son subclass, the A. method calls the method of the subclass, and a. Variable invocation is indeed a variable of the parent class.


See the following code

public class A {
	Animal  animals = new Dog ();
	Dog  dogs =  new Dog ();

	public static void Main (string[] args) {
              A as =new a ();
     
              As.go2 ();
	                                       }

	
        void Go2 () {
	animals.eat ();                 Animal Animals =new Dog (), parent type, subclass Object
	System.out.println (animals.i);

	

Internal class
 Animal  {
 	        int i=10;             I
		void Eat () {
			System.out.println ("Animal are eating") of subclasses;
		           }

 Class Dog extends Animal {
		int i=100;            The I
		void Eat () {
			System.out.println ("Dogs are eating") of
		           the parent class

	                          ;






Output:

Dogs are eating (animal.eat () calls the subclass's Eat ())


(Animal.i called I of the parent class)


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.