Menu
Topics Index
...
`

Four Wheelers


If you need explanation Read this topic

If you need Answer Take test on this topic

Four Wheelers
What will be the output of the following program?
class FourWheeler
{
    public static void main(String[] args)
    {
        new Car(0);
    }
}

class Vehicle
{
    int i;
    int j;

    Vehicle(int i)
    {
        this.i = i;
        j = i;
    }

    Vehicle(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
}

class Bus extends Vehicle
{
    double j;

    Bus(double j)
    {
        this.j = j;
    }
}

class Car extends Vehicle
{
    Car(int i)
    {
        super(i);
        System.out.print("Executed Successfully");
    }
}

A. Executed Successfully
B. Compilation Error because the class Vehicle does not 
have a default constructor
C. Compilation Error for some other reason
D. Runtime Error
Topic: Inheritance And Constructors In Java

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 B

Class vehicle has one arg and two arg constructors (and no arg constructor)which means compiler will not consider invoking a default no arg constructor for Vehicle Class.
So all the other classes which extends Vehicle class like Bus and Car in this program will have to explicitly invoke one of the defined constructors in the Vehicle Class.We cannot rely on compiler to invoke the default no arg constructor in this case
if we failed to invoke the existing constructors that are defined,compiler will report an error.

So,
Bus(double j){
this.j=j;
}
will be augmented at runtime to
Bus(double j){
super(); // since before the child class constructor,parent class constructor has to be invoked,compiler adds a call super()
this.j=j;
}

Super() call is a constructor call to Vehicle class's no arg constructor,compiler will throw a error as it is not explicitly defined

Posted by Shashanka Mogaliraju    2015-03-17 03:55:00


ANS IS C

--Here,the concept of super Keyword is used.

java 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.

--Here,3 classes are declared:
1.Vehicle (parent/super class)
2.Bus (child/sub class)
3.Car (child/sub class)

--First of all,vehicle class is defined with 2 constructor with parameters where formal argumets are assigned to actual parameter.

--After that,class Bus is defined which extends the Vehicle class inside it constructor is defined where formal argumets are assigned to actual parameter.

--After that,class Car is defined which extends the Vehicle class inside it constructor is defined where formal argumets are assigned to actual parameter.

--In main(),object of car is created by passing value as 0(zero).

--Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

OUTPUT:
Compilation Error

Posted by Mânïshå Mùlchåndânï    2015-03-17 16:36:13


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. 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    2015-03-20 06:46:17


© meritcampus 2019

All Rights Reserved.

Open In App