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 class types in java?

0 Answers  


Explain about serializable interface in java?

0 Answers  


What is a line separator in java?

0 Answers  


long d =10;int i =0;i=d; /// is this possible? If d is very long number (10 digits or some thing) then?

3 Answers  


What is autoboxing and unboxing?

0 Answers  






Write a program to print fibonacci series

0 Answers  


What is the difference between a scrollbar and a scrollpane?

0 Answers  


Which non-unicode letter characters may be used as the first character of an identifier?

0 Answers  


Why parsing is done?

0 Answers  


What is io stream in java?

0 Answers  


what are the oops concept in java explain with real time examples

24 Answers   Accenture, Bosch, Consummate Technologies, CTS, Current Technologies, iNautix, Infosys, Kekran Mekran, QA InfoTech, RTRT, SofTech, Tech Mahindra, Thorogood, Timios, Wipro,


If goto and const is reserve words than why it is not work in java?

0 Answers  


Categories