Results 1 to 8 of 8

Thread: are variables also initialised in the constructor of the superclass?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    are variables also initialised in the constructor of the superclass?

    Hello

    I was reading about the construction of constructors and this article says

    The first thing that happens inside the constructor is that the constructor of the class's superclass is called.
    Then, each class attribute of the object is initialized. These are the attributes that are part of the class definition (instance variables), not the attributes inside the constructor or any other method (local variables). In the DataBaseReader code presented earlier, the integer startPosition is an instance variable of the class.
    Then, the rest of the code in the constructor executes.

    (http://www.developer.com/design/arti...0925_3516911_3)

    if there are any local variables to the superclass, will these variables be initialised in the constructor of the superclass.

    w.e. if we have two classes dog and cat each derived from a superclass animal. will variables to do with say number of legs = 4 be initialised within the seuperclass animal?

  2. #2
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: are variables also initialised in the constructor of the superclass?

    If you have a super class with class scope variables they will be inherited(sp?).
    Code:
    public class animal
    {
        int legs = 4;
        ...
    }
    and then
    Code:
    public class dog extends animal
    {
        ...
    }
    The class dog will have access to the int legs with the value of four.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: are variables also initialised in the constructor of the superclass?

    Quote Originally Posted by vb_student
    w.e. if we have two classes dog and cat each derived from a superclass animal. will variables to do with say number of legs = 4 be initialised within the seuperclass animal?
    Yes. A constructor is always only responsible for the content of its immediate class, not any superclasses.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: are variables also initialised in the constructor of the superclass?

    each class attribute of the object is initialized.
    These are the attributes that are part of the class definition (instance variables), not the attributes inside the constructor or any other method (local variables).
    I guess this part is about one example, or maybe the author of the article haven't heard of lazy initialization. Or maybe he means assigning JVM references
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: are variables also initialised in the constructor of the superclass?

    thanks for the replies

    so local variables to the superclass will be initialised in the constructor of the superclass, correct?
    what is lazy initialisation?

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: are variables also initialised in the constructor of the superclass?

    Quote Originally Posted by vb_student
    thanks for the replies

    so local variables to the superclass will be initialised in the constructor of the superclass, correct?
    what is lazy initialisation?
    Lazy initialization is when you initialize local attribute upon usage.
    In other words you don't initialize a Object until you want to use it
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: are variables also initialised in the constructor of the superclass?

    That's an implementation technique, though, and has nothing to do with the technical side. You still have to initialize the object to the sentinel value in the constructor. (Of course, as that value is usually null, the compiler/JVM does it for you.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: are variables also initialised in the constructor of the superclass?

    Thanks For The Replies

Posting Permissions

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



Click Here to Expand Forum to Full Width