Invoking the Constructor of the Top Most Class in the Hierarchy from the Bottom most class

  • Thread starter H.MuthuKumaraRajan
  • Start date
H

H.MuthuKumaraRajan

Hi,
Is it possible to invoke the constructor of the Top Most Class in the Hierarchy
from the Bottom most class.
for eg.,
class A {
long l;
A(long l) {
this.l = l;
}
A(){}
}

class B extends A {
String s;
B(String s) {
this.s=s;

}
B(){}
}

public class C extends B {
C(long l){
// TO INVOKE THE CONSTRUCTOR WITH LONG PARAMETER IN CLASS A
}
}
I want to invoke the Constructor (with long parameter) in class A from class C,
How to do this?
Thanks in advance,
Rajan
 
A

Andrew Thompson

"H.MuthuKumaraRajan" ...
Hi,
Is it possible to invoke the constructor of the Top Most Class in the Hierarchy
from the Bottom most class.
Yes.

for eg.,

There may be other ways, but this works
______________________
class A
{
long l;

A(long l)
{
this.l = l;
}

A(){}
}

class B extends A
{
String s;

B(String s)
{
this.s=s;
}

B(long l)
{
super(l);
}

B(){}
}

class C extends B
{
C(long l)
{
super(l);
// TO INVOKE THE CONSTRUCTOR WITH LONG PARAMETER IN CLASS A
}
}
______________________
HTH
 
A

Adam Maass

H.MuthuKumaraRajan said:
Hi,
Is it possible to invoke the constructor of the Top Most Class in the Hierarchy
from the Bottom most class.
for eg.,
class A {
long l;
A(long l) {
this.l = l;
}
A(){}
}

class B extends A {
String s;
B(String s) {
this.s=s;

}
B(){}
}

public class C extends B {
C(long l){
// TO INVOKE THE CONSTRUCTOR WITH LONG PARAMETER IN CLASS A
}
}
I want to invoke the Constructor (with long parameter) in class A from class C,
How to do this?
Thanks in advance,
Rajan


You need a constructor in B that takes a long:

class B extends A
{
B (long l)
{
super(l);
}
}


And now, you can chain the long constructor in C:

class C extends B
{
C(long l)
{
super(l);
}
}
 
H

H.MuthuKumaraRajan

Thanks to every one for the response.

Andrew Thompson said:
"H.MuthuKumaraRajan" ...

There may be other ways, but this works
______________________
class A
{
long l;

A(long l)
{
this.l = l;
}

A(){}
}

class B extends A
{
String s;

B(String s)
{
this.s=s;
}

B(long l)
{
super(l);
}

B(){}
}

class C extends B
{
C(long l)
{
super(l);
// TO INVOKE THE CONSTRUCTOR WITH LONG PARAMETER IN CLASS A
}
}
______________________
HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top