This and Super keywords when Java inherits

Source: Internet
Author: User

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.

    1. 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

    1. 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

    1. 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

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.