Super keyword, the difference between the Super keyword and the This keyword

Source: Internet
Author: User
Tags access properties
Super Keyword:
Use super to invoke the composition of the parent class: the member variable of the parent class, the method of the parent class, and the constructor method of the parent class.

Note: Neither the Super keyword nor the This keyword can be used in static methods, but only in common methods (instance methods).

Access Properties:
Example:

Parent class
  public class fatherclass{public
      int age=18;
    }

Sub Class

public class ChildClass extends fatherclass{public
      int age = ten;
 
      public void Test1 () {
        System.out.println (this.age);//This will output 10, accessing the properties of own (this class)
      } public
      void Test2 () {
        System.out.println (super.age);//This will output 18, accessing the properties of the parent class
      } public
      void Test3 () {
      System.out.println (age); This will output 10; By default, if the parent class has an age attribute and the child class also has the age attribute, direct access to age will be accessed in the this.age situation. However, if the age attribute is not present in this class, it will be accessed as a super.age situation.
      }
      
    }


access Method:
Example:

Parent class

  public class fatherclass{public
      Void Show () {
        System.out.println ("Father class Method");
      }
    

Sub Class
public class ChildClass extends fatherclass{public
      Void Show () {
        System.out.println (' child class method ');
      }
      
      public void Test1 () {
        this.show ();//This will output: the child class method
      } public
      void Test2 () {
        super.show ();// This will output: Father class method
      } public
      void Test3 () {show
      	();//This will output: the child class method;
        
      //By default, if the parent class exists the show () method, Subclasses also have the show () method, direct access to the show () method will be accessed in the this.show () situation. However, if the show () method does not exist in this class, it will be accessed in the context of super.show ().
      }
      
    }

the difference between this and super:
Super is used in inheritance relationships, primarily for subclasses to invoke the parent class's constructor and common methods
Constructor method invocation mode: Super (parameter list);
Normal method invocation mode: Super. Method name

This can only be used in this class (parameter list) or this ()




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.