Results 1 to 6 of 6
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Sub class and super class

    I am trying to learn more about sub classes and super classes. I understand the sub class extends the superclass and is thus derived from the super class.

    So is it true to say that a sub class is then a subset of a super class?

    I also understand that if a subclass does not define a constructor it invokes the constructor of the super class but is that always the case? Or is that only when there is not a sub class constructor defined?

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

    Default Re: Sub class and super class

    Quote Originally Posted by dragstang86 View Post
    ...So is it true to say that a sub class is then a subset of a super class?
    No. A sub class does not have the super class's private fields and methods, but it has all the public and protected fields and methods and often a few of its own fields and methods.

    I also understand that if a subclass does not define a constructor it invokes the constructor of the super class
    A sub class always invokes the super class constructor, always, either explicitly or implicitly.

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Sub class and super class

    Thank you. Just to make sure I am understanding correct. To invoke the constructor explicitly would be to call super() and to invoke it implicitly would be simply to just do nothing?

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

    Default Re: Sub class and super class

    Quote Originally Posted by dragstang86 View Post
    Thank you. Just to make sure I am understanding correct. To invoke the constructor explicitly would be to call super() and to invoke it implicitly would be simply to just do nothing?
    Correct. When you call it explicitly, you can choose which constructor to call, the default one with no parameters or one with one or many parameters. If you do nothing, the default constructor is always called. This can cause a compiler error if no default constructor is available in the super class.

  5. #5
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Sub class and super class

    Ok that makes sense. What are the requirements for defining a constructor within the subclass? Playing around with it in the IDE I can only get classes to compile if either there is no constructor defined in the subclass or if both the superclass constructor and subclass constructors have no arguments.

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

    Default Re: Sub class and super class

    Java Code:
    public class MyParent {
        public MyParent(int someParameter) {
        }
    }
    ....
    public class MySub extends MyParent {
        public MySub(int someParameter) {
            super(someParameter);  // Works as there is a constructor that takes an int argument in the parent.
        }
        public MySub() {
            super(1);  // Works as there is a constructor that takes an int argument in the parent.
        }
        public MySub() {
            super();  // Doesn't work as there is no constructor that takes no parameters.
        }
        public MySub() {
            // Doesn't work for the same reason.
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 08-17-2011, 09:09 AM
  2. Referring to a super class
    By blug in forum New To Java
    Replies: 7
    Last Post: 03-20-2011, 06:06 AM
  3. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  4. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 08:23 PM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 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
  •