Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Is there anyway not to call superclass constructor ?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Is there anyway not to call superclass constructor ?

    I have two classes:

    Toy
    and FancyToy extends Toy

    Constructors:

    Toy()
    {
    System.out.print("Toy created");
    }

    FancyToy()
    {
    System.out.println("Fancytoy created");
    }

    In my main method, when I do FancyToy flasycar = new FancyToy();

    I see both "Toy created" and "Fancytoy created", altough I do not have the super(); in my FancyToy constructor.

    Anything to avoid this ?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Is there anyway not to call superclass constructor ?

    No. And if you even think you need that, you have a misuse of inheritance.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Is there anyway not to call superclass constructor ?

    The constructor mechanism in Java works as follows:

    1) on the first line of a constructor either a superclass constructor is called or:
    2) on the first line of a constructor another constructor from the same class is called.

    If you don't put a first line as described above in your constructor, the compiler puts a super() call as the first line. If all first lines of all constructors are of form 2), the compiler generates an error. Also, if the compiler had to insert a super() call somewhere but your superclass doesn't have a no-arg constructor, an error is generated. If you don't have any constructors at all in your class, the compiler creates an empty no-arg constructor for you.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Is there anyway not to call superclass constructor ?

    Thanks for the detailed answers.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Is there anyway not to call superclass constructor ?

    That is a good question and I think you should start a new thread for it, it may not get attention here.

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  2. Replies: 7
    Last Post: 10-28-2011, 08:26 AM
  3. Having trouble with inheriting constructor from superclass
    By coasterguy10 in forum New To Java
    Replies: 3
    Last Post: 10-20-2011, 06:07 AM
  4. y superclass constructor is called automatically
    By _ShivamKapoOr_ in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 02:58 PM
  5. Constructor call not calling
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-09-2009, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •