Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Extending class

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extending class

    Hi, I know when we extend a class our class gets all of the properties of the class we extend.

    I am abit confused. I have read that when our class extends the JFrame class, our class then IS the window.

    So when we extend the JFrame class our class gets all of the properties (attributes & methods) of the JFrame class.

    Do our class also inherit JFrame's constructor?


  2. #2
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending class

    I would understand how it all works if our class also inherits JFrame's constructor, if not then I can not see how creating an instance of our class creates a window.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Extending class

    Technically, no. You must use one of the base class's constructor to build part of your object (the part that got inherited), but that's it. It doesn't make sense to have the base class constructor be able to construct one of it's child classes.

    For example, say you have a base class Room. The constructor for Room would build the 4 walls, a floor, and a ceiling. A constructor could take in how you want the walls built (drywall, tile, etc.), what kind of floor (carpet, hardwood, etc.), and what to do with the ceiling (sky portal, drywall, etc.), but as far filling the room or adding in special features that's left to the child classes.

    public class Room
    {
        public Room(wall_type, floor_type, ceiling_type) // builds a basic room
        {
            // ... code to build a Room
        }
    }

    If you extended it to be Kitchen, the Kitchens constructor would first call Room's constructor and build the walls, floor, and ceiling. This way you can do stuff like say all Kitchens must have hardwood floors. On top of that, the Kitchen's constructor would also add in items specific for the Kitchen such as a sink, refrigerator, stove, etc.

    public class Kitchen extends Room
    {
        public Kitchen(Appliance appliances[], wall_type, ceiling_type) // builds a Kitchen with various appliances.
        {
            // build a basic room with hardwood floors
            super(wall_type, hard_wood_floors, ceiling_type);
            // ... build the rest of the kitchen
        }
    }

    A Room doesn't know how to build these appliances into the room, so it is physically incapable of building a Kitchen. However, part of the Kitchen is a Room, and those parts can (actually, must) be built by the Rooms constructor.

    When you extend the JFrame class a similar process is happening. Your class would invoke one of the the JFrame's constructors which builds a basic JFrame with your specifications. On top of that, your class would also build anything on top of that. For example, say you wanted your class to add a red button into the middle of the JFrame. You could then build that on top of the JFrame.
    Last edited by helloworld922; January 9th, 2012 at 05:22 PM.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    elisha.java (January 10th, 2012)

  5. #4
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending class

    Thank you very much for your detailed reply, much appreciated.

    So, when i create an instance of my class which has extended JFrame, both my class constructor and the JFrame constructor is called upon?

  6. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extending class

    Quote Originally Posted by TP-Oreilly View Post
    I would understand how it all works if our class also inherits JFrame's constructor, if not then I can not see how creating an instance of our class creates a window.
    The job of a constructor is to get an instance into a usable state with everything it needs initialised.

    Just to reinforce HW's point, your class doesn't *inherit* the constructor but - one way or another - one of the frame constructors will be called just before the rest of your constructor runs. In this way a fully initialised window is created.

  7. #6
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending class

    Got it, thank you

  8. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Extending class

    I think I may have answered your question (yes: one of the frame constructors is called). But from this thread at forums.oracle.com there is a handy list which summarises the "Constructor rules" courtesy of jverd:

    Constructor Rules

    Okay, here are the rules for constructors--"ctors" because I'm lazy. Also, because I'm lazy, "super(...)" and "this(...)" mean any super or this call, regardless of how many args it takes, including those that take no args.

    1) Every class has at least one ctor.

    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a
    public MyClass() {...}
    if you want one.

    1.3) Constructors are not inherited.

    2) The first statement in the body of any ctor is either a call to a superclass ctor
    super(...)
    or a call to another ctor of this class
    this(...)


    2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor
    super()
    as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

  9. The Following User Says Thank You to pbrockway2 For This Useful Post:

    elisha.java (January 10th, 2012)

  10. #8
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending class

    Thank you

  11. #9
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Extending class

    I want to acknowledge the fact that this response is the best I have ever read anywhere. Thank you so much for putting everything into perspective. It is just outstanding for whoever wants to understand constructors and inheritance.

Similar Threads

  1. Difference between extending and importing a class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: December 5th, 2011, 05:53 PM
  2. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  3. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM
  4. Replies: 7
    Last Post: November 16th, 2010, 08:42 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM