super() in Java

前端 未结 15 2038
逝去的感伤
逝去的感伤 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.

    0 讨论(0)
  • 2020-11-22 05:46

    Constructors
    In a constructor, you can use it without a dot to call another constructor. super calls a constructor in the superclass; this calls a constructor in this class :

    public MyClass(int a) {
      this(a, 5);  // Here, I call another one of this class's constructors.
    }
    
    public MyClass(int a, int b) {
      super(a, b);  // Then, I call one of the superclass's constructors.
    }
    

    super is useful if the superclass needs to initialize itself. this is useful to allow you to write all the hard initialization code only once in one of the constructors and to call it from all the other, much easier-to-write constructors.

    Methods
    In any method, you can use it with a dot to call another method. super.method() calls a method in the superclass; this.method() calls a method in this class :

    public String toString() {
      int    hp   = this.hitpoints();  // Calls the hitpoints method in this class
                                       //   for this object.
      String name = super.name();      // Calls the name method in the superclass
                                       //   for this object.
    
      return "[" + name + ": " + hp + " HP]";
    }
    

    super is useful in a certain scenario: if your class has the same method as your superclass, Java will assume you want the one in your class; super allows you to ask for the superclass's method instead. this is useful only as a way to make your code more readable.

    0 讨论(0)
  • 2020-11-22 05:50

    super() calls the parent constructor with no arguments.

    It can be used also with arguments. I.e. super(argument1) and it will call the constructor that accepts 1 parameter of the type of argument1 (if exists).

    Also it can be used to call methods from the parent. I.e. super.aMethod()

    More info and tutorial here

    0 讨论(0)
  • 2020-11-22 05:50

    super is a keyword. It is used inside a sub-class method definition to call a method defined in the superclass. Private methods of the superclass cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class.

    Check here for further explanation.

    0 讨论(0)
  • 2020-11-22 05:50

    As stated, inside the default constructor there is an implicit super() called on the first line of the constructor.

    This super() automatically calls a chain of constructors starting at the top of the class hierarchy and moves down the hierarchy .

    If there were more than two classes in the class hierarchy of the program, the top class default constructor would get called first.

    Here is an example of this:

    class A {
        A() {
        System.out.println("Constructor A");
        }
    }
    
    class B extends A{
    
        public B() {
        System.out.println("Constructor B");
        }
    }
    
    class C extends B{ 
    
        public C() {
        System.out.println("Constructor C");
        }
    
        public static void main(String[] args) {
        C c1 = new C();
        }
    } 
    

    The above would output:

    Constructor A
    Constructor B
    Constructor C
    
    0 讨论(0)
  • 2020-11-22 05:51

    Source article: Java: Calling super()


    Yes. super(...) will invoke the constructor of the super-class.

    Illustration:




    Stand alone example:

    class Animal {
        public Animal(String arg) {
            System.out.println("Constructing an animal: " + arg);
        }
    }
    
    class Dog extends Animal {
        public Dog() {
            super("From Dog constructor");
            System.out.println("Constructing a dog.");
        }
    }
    
    public class Test {
        public static void main(String[] a) {
            new Dog();
        }
    }
    

    Prints:

    Constructing an animal: From Dog constructor
    Constructing a dog.
    
    0 讨论(0)
提交回复
热议问题