Can a class inherit the constructors of its superclass?
Answers were Sorted based on User's Feedback
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 |
Answer / devarathnam
Hi... No.constructor cannot be inherited from super class.
Is This Answer Correct ? | 8 Yes | 1 No |
Answer / ravikiran(aptech mumbai)
No constructor overriding is not possible only constructor
overloading is possible
Is This Answer Correct ? | 6 Yes | 3 No |
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 |
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 |
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 |
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 |
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 |
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 |
How to sort a collection of custom Objects in Java?
Explain the importance of thread scheduler in java?
How can we make copy of a java object?
please send code example of inner classes?
Why does java not support pointers?
What is escape analysis algorithm in JVM and how garbage collection actually worked n how it transfer the objects from one kind of space to other?
Is the milky way in a void?
Suppose there is an array list [10,5,20,19,15,24].Print them in ascending & descending order in collection framework concept ???
How does compareto work in java?
What is api in java?
what is the use of pojo (plain old java objects)? how it is interact with crystal reports? pls urgent
What is unicode with example?