Can a subclass use a superclass constructor?
Using super to Call Superclass ConstructorsA subclass can call a constructor defined by its superclass by use of the following form of super: Show
super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor. To see how super( ) is used, consider the version of TwoDShape in the following program. It defines a constructor that initializes width and height. Here, Triangle( ) calls super( ) with the parameters w and ... The Before we learn about the Uses of super keyword
Let’s understand each of these uses. 1. Access Overridden Methods of the superclassIf methods with the same name are defined in both superclass and subclass, the method in the subclass overrides the method in the superclass. This is called method overriding. Example 1: Method overriding
Output I am a dog In this example, by making an object dog1 of Dog class, we can call its method printMessage() which then executes the Since What if the overridden method of the superclass has to be called? We use Example 2: super to Call Superclass Method
Output I am a dog I am an animal Here, how the above program works. 2. Access Attributes of the SuperclassThe superclass and subclass can have attributes with the same name. We use the Example 3: Access superclass attribute
Output: I am a mammal I am an animal In this example, we have defined the same instance field type in both the superclass Animal and the subclass Dog. We then created an object dog1 of the Dog class. Then, the Inside the
Hence, 3. Use of super() to access superclass constructorAs we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use
Example 4: Use of super()
Output I am an animal I am a dog Here, when an object dog1 of Dog class is created, it automatically calls the default or no-arg constructor of that class. Inside the subclass constructor, the The flow of the program then returns back to the subclass constructor and executes the remaining statements. Thus, I am a dog will be printed. However, using So, why use redundant code if the compiler automatically invokes super()? It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor. The parameterized Example 5: Call Parameterized Constructor Using super()
Output Type: Animal I am a dog The compiler can automatically call the no-arg constructor. However, it cannot call parameterized constructors. If a parameterized constructor has to be called, we need to explicitly define it in the subclass constructor. Note that in the
above example, we explicitly called the parameterized constructor Can we call super class constructor in subclass?To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
Can a subclass access a superclass?Yes, a subclass has access to all nonprivate members of its superclass.
Can a subclass constructor override a superclass constructor?Constructors are never overriden or inherited. In fact, they cannot be called virtually. However, you can create a constructor in the subclass which calls a constructor in the parent class.
When can I call a superclass constructor from a subclass?The constructors of the subclass can initialize only the instance variables of the subclass. Thus, when a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass. To call a superclass constructor the super keyword is used.
|