Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Problems and rules of using constructors with inheritance

  1. #1
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Problems and rules of using constructors with inheritance

    The following statement is from Java Tutorial of Oracle Corporation. I am very new in java programming & I can't understand this statement clearly. Can anyone give example of a java program that proves the following statement:

    "If each of all constructors of a subclass doesn't explicitly invoke any constructor of immediate superclass, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error."
    Last edited by Rafiqul; 03-24-2013 at 07:12 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problems and rules of using constructors with inheritance

    Create a program that doesn't follow these rules and see the compiler error:

    Java Code:
    class A {
       public A(int foo) {}
    }
    Java Code:
    class B extends A {
       
    }
    B has a default constructor (all classes without an explicitly declared constructor will have one, but A doesn't since it has an explicit constructor *that takes a parameter*. B's default constructor tries to call A's parameterless constructor, but since one is not present, an error will occur at compilation.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Problems and rules of using constructors with inheritance

    Uh, I believe this was already asked and answered in a recent post by you.

    Basic inheritance and access member of superclass in direct and indirect subclasses.


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problems and rules of using constructors with inheritance

    Quote Originally Posted by jim829 View Post
    Uh, I believe this was already asked and answered in a recent post by you.
    ....
    Great, thanks for wasting our time.

  5. #5
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Problems and rules of using constructors with inheritance

    Sorry, I am not sure about that whether my previous thread posted exactly same question as this one.

    But only just this is still not clear to me that, what does it means "constructor of subclass explicitly calls constructor of superclass" ?
    I mean, how "a constructor of subclass explicitly calls constructor of superclass" ? I will be grateful, if you can give an example that illustrates that fact.

    Thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Problems and rules of using constructors with inheritance

    Java Code:
    class A {
        public A(int foo) {}
    } 
    class B extends A {
        public B(int foo) {
            super(foo);  // explicitly calling the constructor in A.
        }
    }
    Rafiqul likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Problems and rules of using constructors with inheritance

    I'm sorry. I was trying to be helpful.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Rafiqul is offline Member
    Join Date
    Mar 2013
    Location
    Bangladesh
    Posts
    12
    Rep Power
    0

    Default Re: Problems and rules of using constructors with inheritance

    Thanks.

Similar Threads

  1. constructors and inheritance
    By dawnMist in forum New To Java
    Replies: 10
    Last Post: 10-25-2012, 11:39 PM
  2. Inheritance problems...quick answers if possible :)
    By circuspeanuts in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 09:06 PM
  3. Inheritance problems
    By PVL268 in forum New To Java
    Replies: 3
    Last Post: 03-23-2009, 02:52 AM
  4. Replies: 1
    Last Post: 07-05-2008, 03:08 PM

Posting Permissions

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