super() in Java

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

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

相关标签:
15条回答
  • 2020-11-22 05:55

    I have seen all the answers. But everyone forgot to mention one very important point:

    super() should be called or used in the first line of the constructor.

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

    Is super() is used to call the parent constructor?

    Yes.

    Pls explain about Super().

    super() is a special use of the super keyword where you call a parameterless parent constructor. In general, the super keyword can be used to call overridden methods, access hidden fields or invoke a superclass's constructor.

    Here's the official tutorial

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

    I would like to share with codes whatever I understood.

    The super keyword in java is a reference variable that is used to refer parent class objects. It is majorly used in the following contexts:-

    1. Use of super with variables:

    class Vehicle 
    { 
        int maxSpeed = 120; 
    } 
    
    /* sub class Car extending vehicle */
    class Car extends Vehicle 
    { 
        int maxSpeed = 180; 
    
        void display() 
        { 
            /* print maxSpeed of base class (vehicle) */
            System.out.println("Maximum Speed: " + super.maxSpeed); 
        } 
    } 
    
    /* Driver program to test */
    class Test 
    { 
        public static void main(String[] args) 
        { 
            Car small = new Car(); 
            small.display(); 
        } 
    } 
    

    Output:-

    Maximum Speed: 120
    
    1. Use of super with methods:
    /* Base class Person */
    class Person 
    { 
        void message() 
        { 
            System.out.println("This is person class"); 
        } 
    } 
    
    /* Subclass Student */
    class Student extends Person 
    { 
        void message() 
        { 
            System.out.println("This is student class"); 
        } 
    
        // Note that display() is only in Student class 
        void display() 
        { 
            // will invoke or call current class message() method 
            message(); 
    
            // will invoke or call parent class message() method 
            super.message(); 
        } 
    } 
    
    /* Driver program to test */
    class Test 
    { 
        public static void main(String args[]) 
        { 
            Student s = new Student(); 
    
            // calling display() of Student 
            s.display(); 
        } 
    }
    

    Output:-

    This is student class
    This is person class
    

    3. Use of super with constructors:

    class Person 
    { 
        Person() 
        { 
            System.out.println("Person class Constructor"); 
        } 
    } 
    
    /* subclass Student extending the Person class */
    class Student extends Person 
    { 
        Student() 
        { 
            // invoke or call parent class constructor 
            super(); 
    
            System.out.println("Student class Constructor"); 
        } 
    } 
    
    /* Driver program to test*/
    class Test 
    { 
        public static void main(String[] args) 
        { 
            Student s = new Student(); 
        } 
    } 
    

    Output:-

    Person class Constructor
    Student class Constructor
    
    0 讨论(0)
  • 2020-11-22 05:57

    Yes, super() (lowercase) calls a constructor of the parent class. You can include arguments: super(foo, bar)

    There is also a super keyword, that you can use in methods to invoke a method of the superclass

    A quick google for "Java super" results in this

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

    That is correct. Super is used to call the parent constructor. So suppose you have a code block like so

    class A{
        int n;
        public A(int x){
            n = x;
        }
    }
    
    class B extends A{
        int m;
        public B(int x, int y){
            super(x);
            m = y;
        }
    }
    

    Then you can assign a value to the member variable n.

    0 讨论(0)
  • 2020-11-22 06:01

    The super keyword can be used to call the superclass constructor and to refer to a member of the superclass

    When you call super() with the right arguments, we actually call the constructor Box, which initializes variables width, height and depth, referred to it by using the values of the corresponding parameters. You only remains to initialize its value added weight. If necessary, you can do now class variables Box as private. Put down in the fields of the Box class private modifier and make sure that you can access them without any problems.

    At the superclass can be several overloaded versions constructors, so you can call the method super() with different parameters. The program will perform the constructor that matches the specified arguments.

    public class Box {
    
        int width;
        int height;
        int depth;
    
        Box(int w, int h, int d) {
            width = w;
            height = h;
            depth = d;
        }
    
        public static void main(String[] args){
            HeavyBox heavy = new HeavyBox(12, 32, 23, 13);
        }
    
    }
    
    class HeavyBox extends Box {
    
        int weight;
    
        HeavyBox(int w, int h, int d, int m) {
    
            //call the superclass constructor
            super(w, h, d);
            weight = m;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题