SlideShare a Scribd company logo
The Role of Superclass ConstructorsLIS4930 © PICRemember when you create an object it inherits the Object class automatically.FamilyDoctor object
The Role of Superclass ConstructorsLIS4930 © PICAll the constructors in an object’s inheritance tree must run when you make a new object.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.ObjectThis all happens in a process called Constructor ChainingDoctorFamilyDoctorFamilyDoctor object
DemoLIS4930 © PICIn class demo – but if you’re not in class, read page 252 in the textbook.
Invoking the Superclass ConstructorLIS4930 © PICAs 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.But, if you wanted to call the superclass constructor explicitly, you could us:super( )1If you don’t provide a constructorpublic ClassName( ) {	super( );}The compiler puts one in that looks like:2If you do provide a constructor but you do not put in  a call to super()The compiler will put a call to super() in each of your overloaded constructors. The compiler supplied call looks like: super();It is always like that. The compiler-inserted call to super() is always a no-arg constructor, only the no-arg one is called.
Can the child exist before the parents?LIS4930 © PICRead page 254 in the textbook.The call to super() must be the first statement in each constructor, if you call it.
Superclass Constructors with ArgumentsLIS4930 © PICRead page 255 in the textbook.
Invoking One Overloaded Constructor From Another.LIS4930 © PICRead page 256 in the textbook.
Now we know how an object is born, how long does an object live?LIS4930 © PICAn object’s life depends entirely on the life of the reference referring to it.SO if an object’s life depends on the reference variable’s life, how long does a variable live?1A local variable lives only within the method that declared the variable.2An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.So the life of the reference variable is dependent on whether it is an local or instance variable.
The difference between life and scope for local variables.LifeA local variable is alive as long as its Stack frame is on the Stack.ScopeA 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.LIS4930 © PICYou can only use a variable when it is in scope.
What about Reference Variables?LIS4930 © PICHow does variable life affect object life?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?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.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.Your job is to make sure that you abandon objects (i.e. make them eligible for GC) when you’re done with them.
3 Ways to Kill an ObjectLIS4930 © PICThe first object is abandoned when z is ‘reprogrammed’ to a new objectThe first object is abandoned when z is ‘deprogrammed’Reference ‘z’ dies at end of methodvoid go( ) {	Life z = new Life( );}13A reference goes out of scope, permanentlyThe reference is explicitly set to nullLife z = new Life( );z = new Life( );2The reference is assigned another objectLife z = new Life( );z = null;Turn to pages 261 – 263 in your textbook for a more detailed explanation

More Related Content

PDF
Ia+ threading
DOCX
BigO and Sorting
PPT
Часы и время
PPT
Mysocial databasequeries

Similar to 13 life and scope (20)

PPT
Internet programming slide - java.ppt
PPTX
Java basics
PPT
Object and Classes in Java
PDF
iPhone Memory Management
PDF
Lezione03
PDF
Lezione03
PPTX
1 - Classes and Objects OOP.pptx
PPTX
Chapter 8 java
PPT
Core Java Concepts
PPT
Object concepts
PPTX
8. objects & classes
PPTX
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
PPT
Object and class in java
PPT
L5 classes, objects, nested and inner class
PPTX
IFI7184.DT lesson1- Programming languages
PPT
Md02 - Getting Started part-2
DOCX
Sample UML Diagram – Employee ClassEmployee- count int- .docx
PPTX
Class and Object.pptx
Internet programming slide - java.ppt
Java basics
Object and Classes in Java
iPhone Memory Management
Lezione03
Lezione03
1 - Classes and Objects OOP.pptx
Chapter 8 java
Core Java Concepts
Object concepts
8. objects & classes
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
Object and class in java
L5 classes, objects, nested and inner class
IFI7184.DT lesson1- Programming languages
Md02 - Getting Started part-2
Sample UML Diagram – Employee ClassEmployee- count int- .docx
Class and Object.pptx
Ad

More from Program in Interdisciplinary Computing (20)

Ad

13 life and scope

  • 1. The Role of Superclass ConstructorsLIS4930 © PICRemember when you create an object it inherits the Object class automatically.FamilyDoctor object
  • 2. The Role of Superclass ConstructorsLIS4930 © PICAll the constructors in an object’s inheritance tree must run when you make a new object.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.ObjectThis all happens in a process called Constructor ChainingDoctorFamilyDoctorFamilyDoctor object
  • 3. DemoLIS4930 © PICIn class demo – but if you’re not in class, read page 252 in the textbook.
  • 4. Invoking the Superclass ConstructorLIS4930 © PICAs 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.But, if you wanted to call the superclass constructor explicitly, you could us:super( )1If you don’t provide a constructorpublic ClassName( ) { super( );}The compiler puts one in that looks like:2If you do provide a constructor but you do not put in a call to super()The compiler will put a call to super() in each of your overloaded constructors. The compiler supplied call looks like: super();It is always like that. The compiler-inserted call to super() is always a no-arg constructor, only the no-arg one is called.
  • 5. Can the child exist before the parents?LIS4930 © PICRead page 254 in the textbook.The call to super() must be the first statement in each constructor, if you call it.
  • 6. Superclass Constructors with ArgumentsLIS4930 © PICRead page 255 in the textbook.
  • 7. Invoking One Overloaded Constructor From Another.LIS4930 © PICRead page 256 in the textbook.
  • 8. Now we know how an object is born, how long does an object live?LIS4930 © PICAn object’s life depends entirely on the life of the reference referring to it.SO if an object’s life depends on the reference variable’s life, how long does a variable live?1A local variable lives only within the method that declared the variable.2An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.So the life of the reference variable is dependent on whether it is an local or instance variable.
  • 9. The difference between life and scope for local variables.LifeA local variable is alive as long as its Stack frame is on the Stack.ScopeA 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.LIS4930 © PICYou can only use a variable when it is in scope.
  • 10. What about Reference Variables?LIS4930 © PICHow does variable life affect object life?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?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.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.Your job is to make sure that you abandon objects (i.e. make them eligible for GC) when you’re done with them.
  • 11. 3 Ways to Kill an ObjectLIS4930 © PICThe first object is abandoned when z is ‘reprogrammed’ to a new objectThe first object is abandoned when z is ‘deprogrammed’Reference ‘z’ dies at end of methodvoid go( ) { Life z = new Life( );}13A reference goes out of scope, permanentlyThe reference is explicitly set to nullLife z = new Life( );z = new Life( );2The reference is assigned another objectLife z = new Life( );z = null;Turn to pages 261 – 263 in your textbook for a more detailed explanation