Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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 ?    15 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

why is multiple inheritance not allowed in java?

7 Answers   Elementus Technologies, Huawei, Infosys,


Difference between Application and Applet ?

4 Answers   Wipro,


Can you declare a private method as static?

0 Answers   TCS,


Can you pass functions in java?

0 Answers  


What are 5 boolean operators?

0 Answers  






What is a “stateless” protocol ?

0 Answers  


how to create a jar file in java

1 Answers  


What do you mean by singleton class in java?

0 Answers  


Can we access a database using applets?

2 Answers   Hewitt,


Explain 5 io best practices?

0 Answers  


What is stack explain?

0 Answers  


How many types of constructors are used in java?

0 Answers  






Categories