abstract class can not provide constructor for sub class?

Discussion in 'Java' started by Kevin, Jul 24, 2006.

  1. Kevin

    Kevin Guest

    abstract class A
    {
    public A()
    {
    }
    public A(int i, int j)
    {
    }
    }

    public class B extends A
    {

    }

    B does not have the corrresponding constractor of B(int i, int j) from
    A, though B may have all other fucntions of A.

    That's to say:
    B b = new B(10, 14); will not work.

    Isn't it strange?
    I found the behavior of constractors are pretty different from normal
    functions. How to explain it?

    Thanks!
    Kevin, Jul 24, 2006
    #1
    1. Advertising

  2. Kevin

    Kevin Guest

    PS: does this mean that, I can not use the constructor to provide some
    operations for all the subclasses?
    Kevin, Jul 24, 2006
    #2
    1. Advertising

  3. Kevin

    Kevin Guest

    Well, my purpose is to make sure all the subclasses will have one
    uniform constructor (which takes certain parameters). How can I do
    that?

    Thanks. :)

    Kevin wrote:
    > PS: does this mean that, I can not use the constructor to provide some
    > operations for all the subclasses?
    Kevin, Jul 24, 2006
    #3
  4. Kevin wrote:
    > Well, my purpose is to make sure all the subclasses will have one
    > uniform constructor (which takes certain parameters). How can I do
    > that?
    >
    > Thanks. :)
    >
    > Kevin wrote:
    > > PS: does this mean that, I can not use the constructor to provide some
    > > operations for all the subclasses?


    Kevin,

    In Java, just like in C++, constructors are not typical functions.
    One of the differences is that constructors are not inherited by
    subclasses. You can confirm that by trying to use c'tors from
    non-abstract superclass not define in the subclass .
    puzzlecracker, Jul 24, 2006
    #4
  5. Kevin wrote:
    > abstract class A
    > {
    > public A()
    > {
    > }
    > public A(int i, int j)
    > {
    > }
    > }
    >
    > public class B extends A
    > {
    >
    > }
    >
    > B does not have the corrresponding constractor of B(int i, int j) from
    > A, though B may have all other fucntions of A.
    >
    > That's to say:
    > B b = new B(10, 14); will not work.
    >
    > Isn't it strange?
    > I found the behavior of constractors are pretty different from normal
    > functions. How to explain it?


    Perhaps it is because constructors are not methods?

    Constructors are not inherited. However, a subclass constructor can be
    very short and simple:

    public B(int i, int j){
    super(i,j);
    }

    If you want to ensure that every subclass has a constructor that deals
    with i and j, get rid of the parameterless new A() constructor. It
    implies that

    public B(){
    super();
    }

    would be a valid B constructor, and that is what the compiler makes up
    as default constructor if B does not specify at least one.

    Patricia
    Patricia Shanahan, Jul 24, 2006
    #5
    1. Advertising

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. Matthias Kaeppler
    Replies:
    1
    Views:
    406
    R.F. Pels
    May 22, 2005
  2. Replies:
    4
    Views:
    792
    Rolf Magnus
    May 17, 2006
  3. Ben
    Replies:
    2
    Views:
    852
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,933
    Lawrence D'Oliveiro
    May 20, 2011
  5. John Granade

    Can't provide constructor parameters

    John Granade, Nov 30, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    246
    John Granade
    Dec 3, 2007
Loading...

Share This Page