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

Discussion in 'Java' started by H.MuthuKumaraRajan, Feb 3, 2004.

  1. 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
     
    H.MuthuKumaraRajan, Feb 3, 2004
    #1
    1. Advertisements

  2. "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

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Feb 3, 2004
    #2
    1. Advertisements

  3. H.MuthuKumaraRajan

    Adam Maass Guest

    "H.MuthuKumaraRajan" <> wrote in message
    news:...
    > 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);
    }
    }
     
    Adam Maass, Feb 3, 2004
    #3
  4. Thanks to every one for the response.

    "Andrew Thompson" <> wrote in message news:<amITb.40584$>...
    > "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
     
    H.MuthuKumaraRajan, Feb 4, 2004
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    1
    Views:
    8,665
    ainigma
    Apr 4, 2008
  2. Noozer
    Replies:
    1
    Views:
    7,513
    Carolyn Marenger
    Mar 13, 2006
  3. Dominique
    Replies:
    7
    Views:
    505
    amit gulati
    Nov 17, 2003
  4. mrstephengross
    Replies:
    5
    Views:
    845
    Larry I Smith
    May 18, 2005
  5. Cuthbert
    Replies:
    8
    Views:
    702
    Ancient_Hacker
    Sep 13, 2006
  6. hurcan solter

    Invoking A Base class constructor

    hurcan solter, Jan 7, 2007, in forum: C++
    Replies:
    3
    Views:
    518
    Grizlyk
    Jan 12, 2007
  7. ali
    Replies:
    4
    Views:
    851
    David Harmon
    Mar 5, 2007
  8. Generic Usenet Account
    Replies:
    10
    Views:
    2,982
Loading...