Question

In: Computer Science

In general Describe when, where, and how is super keyword used in a constructor.

In general Describe when, where, and how is super keyword used in a constructor.

Solutions

Expert Solution

Ans:-

Super keyword:-

* The super keyword refers to superclass (parent) objects. It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class 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.

* The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name. super() should be first statement inside any constructor. It can be used only inside constructor and nowhere else. super() is used to refer only parent class’s(super class’s) constructor.

* Super keyword (super() ) is used to call Base class’s constructor(i.e, Parent’s class).

How a super keyword used in a constructor can be understand by a simple program:-

class Parent {

    Parent()

    {

        System.out.println("Parent class's No " +

                              " arg constructor");

    }

}

class Child extends Parent {

    Child()

    {

        super();

        System.out.println("Flow comes back from " +

                        "Parent class no arg const");

    }

    public static void main(String[] args)

    {

        new Child();

        System.out.println("Inside Main");

    }

}


Related Solutions

What does the super keyword represents and where can it be used? Give an example of...
What does the super keyword represents and where can it be used? Give an example of a superclass and subclass. Be sure to make all the instances variables of the super class private. Include at least one constructor in each class and ensure that the constructor of the subclass calls the constructor of the superclass. Also include a toString method in both classes that returns the values of the instance variables with appropriate labels. Ensure that the toString method of...
How to identify when to use super() in constructor for java language in a easy way?
How to identify when to use super() in constructor for java language in a easy way?
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
Describe which methods General Dynamics has used to enter foreign markets. How has General Dynamics gained...
Describe which methods General Dynamics has used to enter foreign markets. How has General Dynamics gained competitive advantage through competing internationally?
If a Vigenere cipher uses the keyword CATS, what is the key used and what is...
If a Vigenere cipher uses the keyword CATS, what is the key used and what is the ciphertext that corresponds to the plaintext CATSCANS
Explain what Quantitative Easing means, how it works, where and when this policy was used. Was...
Explain what Quantitative Easing means, how it works, where and when this policy was used. Was QE an appropriate policy option? Can you suggest a different policy that could be used by the Fed or the Government to address the same problem?
Describe the shikimic acid pathway? Where is it used?
Describe the shikimic acid pathway? Where is it used?
Discuss when and where imagery is most often used. Describe three potential negative outcomes of using...
Discuss when and where imagery is most often used. Describe three potential negative outcomes of using imagery.
23)Describe the pharmaceutical effects of Acetaminophen. When is it used? When is it not used? Treatment...
23)Describe the pharmaceutical effects of Acetaminophen. When is it used? When is it not used? Treatment for medication overdose 24)What is the pharmaceutical differences between Aspirin and Acetaminophen? 29)Review the signs and symptoms in Opioid withdrawal? 30)Review the signs and symptoms of Opioid toxicity / overdose. What medication is given to counteract he opioid toxicity?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT