i want to call there A's class constructor using "super" keyword how it is possible
help me plz.....

class A
{}

class B extends A
{}
class C extends B
{
i want to call here A's class constructor using "super" keyword how it is possible 
help me plz.....
}
class D
{
 A a=new A();
}

Recommended Answers

All 3 Replies

That would be up to the writer of the B class.

All constructors automatically call the immediate superclass's no-args constructor super() as the first line, unless you code an explicit call to a constructor of the immediate superclass yourself as the first line of your constructor.
If no constructor is defined for a class, the compiler creates an empty no-args constructor, which, as described above, automatically calls super().
This guarantees that, no matter what you do, when you call a new C(); a constructor for class A will be executed first, followed by a constructor for class B followed by a constructor for class C.

Short answer: No matter what you do or don't code, A's constructor will always be called. You can't prevent it.

unless you decide to add a constructor to A, which does take parameters, then you'll need to add the call to super in the constructor of B, with a parameter.

if you don't, the (automatically) ran super(); will try to find the public A(), won't find it, and bleep ... your code won't compile, since you're trying to access a non-existing constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.