13 life and scope


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

13 life and scope

  1. 1. The Role of Superclass Constructors<br />LIS4930 © PIC<br />Remember when you create an object it inherits the Object class automatically.<br />FamilyDoctor object<br />
  2. 2. The Role of Superclass Constructors<br />LIS4930 © PIC<br />All the constructors in an object’s inheritance tree must run when you make a new object.<br />For an object to be fully-formed, all the superclass parts of itself must be fully-formed, and that’s why the super constructor must run.<br />Object<br />This all happens in a process called Constructor Chaining<br />Doctor<br />FamilyDoctor<br />FamilyDoctor object<br />
  3. 3. Demo<br />LIS4930 © PIC<br />In class demo – but if you’re not in class, read page 252 in the textbook.<br />
  4. 4. Invoking the Superclass Constructor<br />LIS4930 © PIC<br />As you may have guessed, when creating a new object, the constructor automatically calls the object’s superclass constructors, as we just saw in the demo.<br />But, if you wanted to call the superclass constructor explicitly, you could us:<br />super( )<br />1<br />If you don’t provide a constructor<br />public ClassName( ) {<br /> super( );<br />}<br />The compiler puts one in that looks like:<br />2<br />If you do provide a constructor but you do not put in a call to super()<br />The compiler will put a call to super() in each of your overloaded constructors. The compiler supplied call looks like: <br />super();<br />It is always like that. The compiler-inserted call to super() is always a no-arg constructor, only the no-arg one is called.<br />
  5. 5. Can the child exist before the parents?<br />LIS4930 © PIC<br />Read page 254 in the textbook.<br />The call to super() must be the first statement in each constructor, if you call it.<br />
  6. 6. Superclass Constructors with Arguments<br />LIS4930 © PIC<br />Read page 255 in the textbook.<br />
  7. 7. Invoking One Overloaded Constructor From Another.<br />LIS4930 © PIC<br />Read page 256 in the textbook.<br />
  8. 8. Now we know how an object is born, how long does an object live?<br />LIS4930 © PIC<br />An object’s life depends entirely on the life of the reference referring to it.<br />SO if an object’s life depends on the reference variable’s life, how long does a variable live?<br />1<br />A local variable lives only within the method that declared the variable.<br />2<br />An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.<br />So the life of the reference variable is dependent on whether it is an local or instance variable.<br />
  9. 9. The difference between life and scope for local variables.<br />Life<br />A local variable is alive as long as its Stack frame is on the Stack.<br />Scope<br />A local variable is in scope only within the method in which the variable was declared. When its own method calls another, the variable is alive, but not in scope until its method resumes.<br />LIS4930 © PIC<br />You can only use a variable when it is in scope.<br />
  10. 10. What about Reference Variables?<br />LIS4930 © PIC<br />How does variable life affect object life?<br />An object is alive as long as there are live references to it. But what happens when the Stack frame holding the reference gets popped off the Stack at the end of the method?<br />If that was the only live reference to the object, the object is now abandoned on the Heap and eligible for Garbage Collection (GC). The trick is to know the point at which an object becomes eligible for garbage collection.<br />When your program starts running low on memory, before it runs out, GC will destroy some or all of the eligible objects, to keep you from running out of RAM.<br />Your job is to make sure that you abandon objects (i.e. make them eligible for GC) when you’re done with them.<br />
  11. 11. 3 Ways to Kill an Object<br />LIS4930 © PIC<br />The first object is abandoned when z is ‘reprogrammed’ to a new object<br />The first object is abandoned when z is ‘deprogrammed’<br />Reference ‘z’ dies at end of method<br />void go( ) {<br /> Life z = new Life( );<br />}<br />1<br />3<br />A reference goes out of scope, permanently<br />The reference is explicitly set to null<br />Life z = new Life( );<br />z = new Life( );<br />2<br />The reference is assigned another object<br />Life z = new Life( );<br />z = null;<br />Turn to pages 261 – 263 in your textbook for a more detailed explanation<br />