This and super keywords when JAVA inherits
This article mainly discusses where the compiler looks for the corresponding function when using the this or Super keyword before the method.
- Specify the This keyword in the subclass . Find in this class first, if not found in this class, and then in the parent class.
|
Class A
{
public void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Test ()
{
This.fun ();
}
public void Fun ()
{
System.out.println ("Fun () in subclasses ");
}
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
The output is fun in a subclass . If we delete the function in the subclass, then the fun in the parent class is called .
|
Class A
{
public void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Test ()
{
This.fun ();
}
public void Fun ()
// {
System.out.println ("Fun () in subclasses ");
// }
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
output the fun in the parent class
- use super in subclasses , then skip subclass lookup to find directly from the parent class
|
Class A
{
public void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Test ()
{
Super.fun ();
}
public void Fun ()
{
System.out.println ("Fun () in subclasses ");
}
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
output the fun in the parent class
- this keyword. and subclasses, specify this The keyword, like, first, to find a way to write from a subclass, If found, the method that calls the subclass directly, if not found, is then looked up from the parent class.
|
Class A
{
public void Test ()
{
This.fun ();
}
public void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Fun ()
{
System.out.println ("Fun () in subclasses ");
}
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
the fun in the output sub-class
If you delete a fun in a subclass at this time
|
Class A
{
public void Test ()
{
This.fun ();
}
public void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Fun ()
// {
System.out.println ("Fun () in subclasses ");
// }
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
output the fun in the parent class
fun Span style= "FONT-FAMILY:CALIBRI;" >private fun
|
Class A
{
public void Test ()
{
This.fun ();
}
private void Fun ()
{
System.out.println ("The Fun () of the parent class ");
}
}
Class B extends A
{
public void Fun ()
{
System.out.println ("Fun () in subclasses ");
}
}
public class Hello
{
public static void Main (string[] args) throws Exception
{
b b = new B ();
B.test ();
}
}
|
This and Super keywords when Java inherits