Menu
Topics Index
...
`

Call Super Class Constructors2823


If you need explanation Read this topic

If you need Answer Take test on this topic

Call Super Class Constructors2823
What will be the output of the following program?
public class Example {
    public static void main(String args[]) {
        A a = new A(10);
        B b = new B(20, 30);
        System.out.println(a.i);
        System.out.println(b.i);
    }
}
class A {
    int i;
    A(int i) {
        this.i = i;
    }
}
class B extends A {
    int j;
    B(int i, int j) {
        this.j = j;
        super(i);
    }
}

A. 10
20
30
B. 10
20
40
C. 10
20
10
D. Compilation Error
E. Runtime Error
Topic: Super Keyword In Java To Call Super Class Constructor

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is D compilation error because class B's two arg constructor has a call super(i) and since it is a constructor call that should be the first statement in a constructor

Posted by Shashanka Mogaliraju    2014-12-02 12:38:15


Usage of super Keyword

1.super is used to refer immediate parent class instance variable.
2.super() is used to invoke immediate parent class constructor.
3.super is used to invoke immediate parent class method.

When invoking a superclass version of an overridden method the super keyword is used.
Using 'super' to call superclass constructor

Another purpose of using 'super' is to call superclass constructor from a subclass. Although every time a parameterized or non-parameterized constructor of a subclass is created, a default constructor of superclass in called by default implicitly.
Here the thing to be considered is that, implicitly a default constructor of superclass is called not a parameterized one. To call a parameterized constructor of a superclass we must use 'super(parameters..)' with matching parameters.

If a class dont extends another class and still uses a super() call, this is perfectly ok. No error will be shown because every independent class extends 'Object' class by default. And in this case a dafult constructor of 'Object' class is being called.

Java compiler assumes that the first line of every constructor is an implicit call to the default superclass constructor unless you explicitly use super() or this().

comint to this program we have 3 classes.
in main class we just creaed object for A,B class with arguments of number 1,2.

here class B extends A
in this class we try to override  superclass constructor.

here parameters not used in correct way.

to avoid this compiler error,
in subclass constructor you 1st invoke super class constructor by using super(i);thn initialize j with local value .thn u wil get 10,20 result
then

Posted by Maheshwari Natarajan    2014-12-02 19:11:27


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-03 03:03:24


© meritcampus 2019

All Rights Reserved.

Open In App