super() in Java

前端 未结 15 2218
逝去的感伤
逝去的感伤 2020-11-22 05:36

Is super() used to call the parent constructor? Please explain super().

15条回答
  •  醉梦人生
    2020-11-22 05:46

    Some facts:

    1. super() is used to call the immediate parent.
    2. super() can be used with instance members, i.e., instance variables and instance methods.
    3. super() can be used within a constructor to call the constructor of the parent class.

    OK, now let’s practically implement these points of super().

    Check out the difference between program 1 and 2. Here, program 2 proofs our first statement of super() in Java.

    Program 1

    class Base
    {
        int a = 100;
    }
    
    class Sup1 extends Base
    {
        int a = 200;
        void Show()
        {
            System.out.println(a);
            System.out.println(a);
        }
        public static void main(String[] args)
        {
            new Sup1().Show();
        }
    }
    

    Output:

    200
    200

    Now check out program 2 and try to figure out the main difference.

    Program 2

    class Base
    {
        int a = 100;
    }
    
    class Sup2 extends Base
    {
        int a = 200;
        void Show()
        {
            System.out.println(super.a);
            System.out.println(a);
        }
        public static void main(String[] args)
        {
            new Sup2().Show();
        }
    }
    

    Output:

    100
    200

    In program 1, the output was only of the derived class. It couldn't print the variable of neither the base class nor the parent class. But in program 2, we used super() with variable a while printing its output, and instead of printing the value of variable a of the derived class, it printed the value of variable a of the base class. So it proves that super() is used to call the immediate parent.

    OK, now check out the difference between program 3 and program 4.

    Program 3

    class Base
    {
        int a = 100;
        void Show()
        {
            System.out.println(a);
        }
    }
    
    class Sup3 extends Base
    {
        int a = 200;
        void Show()
        {
            System.out.println(a);
        }
        public static void Main(String[] args)
        {
            new Sup3().Show();
        }
    }
    

    Output:

    200

    Here the output is 200. When we called Show(), the Show() function of the derived class was called. But what should we do if we want to call the Show() function of the parent class? Check out program 4 for the solution.

    Program 4

    class Base
    {
        int a = 100;
        void Show()
        {
            System.out.println(a);
        }
    }
    
    class Sup4 extends Base
    {
        int a = 200;
        void Show()
        {
            super.Show();
            System.out.println(a);
        }
        public static void Main(String[] args)
        {
            new Sup4().Show();
        }
    }
    

    Output:

    100
    200

    Here we are getting two outputs, 100 and 200. When the Show() function of the derived class is invoked, it first calls the Show() function of the parent class, because inside the Show() function of the derived class, we called the Show() function of the parent class by putting the super keyword before the function name.

提交回复
热议问题