Huaqing Vision Lecturer
Recently, a classmate asked me a small question, I think it is very interesting, I have never thought of. "There is a method overlay in Java, is there a variable overlay?" he said. We know that in Java, a subclass can inherit a parent class, and a method override occurs if the child class declares a method that has the same name as the parent class. In fact, this is actually divided into two cases, that is, methods and variables in the inheritance of the cover and hidden problems, these conceptual things seem boring, but in the interview is still more common, so here to discuss
First, let's look at some concepts.
Hide: Subclasses hide the variables and methods of the parent class, and the subclass cannot access the variables or methods that are hidden by the parent class, but by converting the subclass to a parent class, you can access the hidden variables or methods of the parent class
Overwrite: A subclass overrides a variable or method of a parent class, so a subclass cannot access a variable or method that is overridden by the parent class, and the parent class is not allowed to access a variable or method that is overridden after the child class is converted to a parent class
First look at the rules for overriding and hiding methods and variables in Java when they inherit
1. Instance and static variables of the parent class can be hidden by a variable of the same name in the quilt class
2. Static method of the parent class the quilt class with the same name static method hidden
3. Instance methods of the parent class overridden by an instance variable of the same name in the quilt class
There are a few more things to note.
1. You cannot use a static method of a subclass to hide an instance method that is equally marked in the parent class (that is, the return value name parameter is the same)
2. You cannot overwrite a static method that is equally marked in a parent class with an instance method of a subclass
3. Craved note that the variable will only be hidden from being overwritten, whether he is an instance variable or a static variable, and that the child class's static variable can hide the parent class's instance variable, and the child class's instance variable can hide the parent class's static variable
Create a class of two parent-child class relationships
Java code
Parent class
Class Parent
{
public static String kind= "Cn.com.farsight.parent";
public static int age=50;
Public String name= "Parent";
static method, return package name
public static String Getkind ()
{
System.out.println ("The Getkind () method of the parent has been called");
return kind;
}
static method, return age
public static int Getage ()
{
System.out.println ("The Getage () method of the parent has been called");
return age;
}
instance method, return name
Public String GetName ()
{
System.out.println ("The GetName () method of the parent has been called");
return this.name;
}
}
Sub-class
Class Child extends Parent
{
public static String kind= "Cn.com.farsight.child";
public int age=25;
Public String name= "Child";
To hide a parent class static method
public static String Getkind ()
{
SYSTEM.OUT.PRINTLN ("Child's Getkind () method was called");
return kind;
}
Get the parent class package name
public static String Getparentkind ()
{
return parent.kind;
}
Overriding parent class instance methods
Public String GetName ()
{
SYSTEM.OUT.PRINTLN ("Child's GetName () was called");
return this.name;
}
Get Parent class Name
Public String Getparentname ()
{
return super.name;
}
/*
* Error, instance method cannot overwrite static method of parent class
public int Getage ()
{
return this.age;
}
*/
}
Class Testdemo
{
public static void Main (string[] args)
{
Child child=new Child ();
System.out.printf ("Subclass name:%s, Age:%d, package name:%s%n", child.name,child.age,child.kind);
Output: Subclass name: Child, Age: 25, Package: Cn.com.farsight.child
Convert Child to Parent object
Parent Parent=child;
System.out.printf ("Converted name:%s, Age:%d, package name:%s%n", parent.name,parent.age,parent.kind);
Output: Converted name: Parent, Age: 50, Package: Cn.com.farsight.parent
System.out.printf ("Subclass access to the parent class is hidden by the instance variable name:%s%n", Child.getparentname ());
Output: Subclass accesses the instance variable that the parent class is hidden name:parent
System.out.printf ("Subclass access to the parent class is hidden static variable kind:%s", Child.getparentkind ());
Output: Subclass accesses a static variable that is hidden by the parent class Kind:cn.com.farsight.parent
Child.getname ();
Output: Child's GetName () is called
Pay attention to this method, return the getname of the subclass
Parent.getname ();
Output: Child's GetName () is called
Child.getkind ();
Output: Child's Getkind () method is called
Parent.getkind ();
Output: The Getkind () method of the parent is called
}
}
Summarize:
1. The instance method with the same name is overwritten, the static method with the same name is hidden, and the GetName instance method of the child class overrides the GetName instance method of the parent, and the Chind Getkind method hides the Getkind method of the parent class.
2. The difference between hiding and overriding is that when a subclass object is converted to a parent class object, it is able to access the hidden variables and methods of the parent class, and cannot access the method that the parent class is overridden
3. If you need to access an instance variable that the parent class is hidden, plus super, such as accessing the parent class name, using the Super.name
>>> more Excellent technology blog daily updates
This article is from the "Embedded Learning World" blog, please be sure to keep this source http://farsight.blog.51cto.com/1821374/1746314
overriding and hiding problems with methods and variables at inheritance