Can a class inherit the constructors of its superclass?

Answers were Sorted based on User's Feedback



Can a class inherit the constructors of its superclass?..

Answer / ranganathkini

No a subclass cannot inherit the constructors of its
superclass. Constructors are special function members of a
class in that they are not inherited by the subclass.

Is This Answer Correct ?    17 Yes 3 No

Can a class inherit the constructors of its superclass?..

Answer / devarathnam

Hi... No.constructor cannot be inherited from super class.

Is This Answer Correct ?    8 Yes 1 No

Can a class inherit the constructors of its superclass?..

Answer / ravikiran(aptech mumbai)

No constructor overriding is not possible only constructor
overloading is possible

Is This Answer Correct ?    6 Yes 3 No

Can a class inherit the constructors of its superclass?..

Answer / sridhar

Inheriting will inherits the constructor as well.

// This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}

The output from this program is shown here:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076

BoxWeight inherits all of the characteristics of Box and
adds to them the weight
component. It is not necessary for BoxWeight to re-create
all of the features found in
Box. It can simply extend Box to meet its own purposes

Is This Answer Correct ?    2 Yes 1 No

Can a class inherit the constructors of its superclass?..

Answer / gayathri

import java.io.*;

class Parent
{
Parent()
{
System.out.println("This is super class Constructor");
}
}
class Code3 extends Parent
{
Code3()
{
System.out.println("This is child class");
}
public static void main(String ar[])
{
Code3 c=new Code3();
}
}

Here if i call the child class constructor,even the parent class constructor is implicitly invoked,that means constructors can be inherited.

Is This Answer Correct ?    2 Yes 2 No

Can a class inherit the constructors of its superclass?..

Answer / patil abhijeet

No, We cant inherit the constructor. Using super we can call
the constructor but we cannot modify the working of it.

Is This Answer Correct ?    0 Yes 1 No

Can a class inherit the constructors of its superclass?..

Answer / qim2010

No. A class cannot inherit constructor of its superclass but
can call the superclass constructor. If a class called
“SpecialPet” extends your “Pet” class then you can use the
keyword “super” to invoke the superclass’s constructor. E.g.

public SpecialPet(int id) {
super(id); //must be the very first statement in the
constructor.
}

To call a regular method in the super class use:
“super.myMethod( );”. This can be called at any line

Is This Answer Correct ?    0 Yes 1 No

Can a class inherit the constructors of its superclass?..

Answer / shaziya parveen

No, a class can not inherit the constructor of its
superclass. This subclass can inherit the instance
variables and tha methods of the super class.
Using 'super' keyword a sub class can inherit the
constructors. This is only the solution.

Is This Answer Correct ?    0 Yes 4 No

Can a class inherit the constructors of its superclass?..

Answer / kamala

In the overriding concepts subclass can inherit or get the
parentclass constructor using the super keyword

Is This Answer Correct ?    2 Yes 8 No

Post New Answer

More Core Java Interview Questions

What are constructors in java?

0 Answers  


What is percentage in java?

0 Answers  


What do you mean by buffering?

0 Answers  


What is thread life cycle in java?

0 Answers  


What is java virtual machine? Explain

0 Answers  






Can we force garbage collector to run ?

0 Answers   B-Ways TecnoSoft,


Can a class have more than one object?

0 Answers  


What is the difference between Array and Hash Table?

0 Answers   Impetus,


What does indexof mean?

0 Answers  


how to connect two diffrent applet files

0 Answers  


What is the difference between applet and application?

0 Answers  


What restrictions are placed on method overriding?

0 Answers  


Categories