SlideShare a Scribd company logo
1 of 11
The Role of Superclass Constructors LIS4930 © PIC Remember when you create an object it inherits the Object class automatically. FamilyDoctor object
The Role of Superclass Constructors LIS4930 © PIC All 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. Object This all happens in a process called Constructor Chaining Doctor FamilyDoctor FamilyDoctor object
Demo LIS4930 © PIC In class demo – but if you’re not in class, read page 252 in the textbook.
Invoking the Superclass Constructor LIS4930 © PIC 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. But, if you wanted to call the superclass constructor explicitly, you could us: super( ) 1 If you don’t provide a constructor public ClassName( ) { 	super( ); } The compiler puts one in that looks like: 2 If 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 © PIC Read page 254 in the textbook. The call to super() must be the first statement in each constructor, if you call it.
Superclass Constructors with Arguments LIS4930 © PIC Read page 255 in the textbook.
Invoking One Overloaded Constructor From Another. LIS4930 © PIC Read page 256 in the textbook.
Now we know how an object is born, how long does an object live? LIS4930 © PIC An 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? 1 A local variable lives only within the method that declared the variable. 2 An 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. Life A local variable is alive as long as its Stack frame is on the Stack. Scope 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. LIS4930 © PIC You can only use a variable when it is in scope.
What about Reference Variables? LIS4930 © PIC How 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 Object LIS4930 © PIC The first object is abandoned when z is ‘reprogrammed’ to a new object The first object is abandoned when z is ‘deprogrammed’ Reference ‘z’ dies at end of method void go( ) { 	Life z = new Life( ); } 1 3 A reference goes out of scope, permanently The reference is explicitly set to null Life z = new Life( ); z = new Life( ); 2 The reference is assigned another object Life z = new Life( ); z = null; Turn to pages 261 – 263 in your textbook for a more detailed explanation

More Related Content

Similar to 13 life and scope

Similar to 13 life and scope (20)

12 constructors
12 constructors12 constructors
12 constructors
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
iOS Memory Management
iOS Memory ManagementiOS Memory Management
iOS Memory Management
 
Spring framework IOC and Dependency Injection
Spring framework  IOC and Dependency InjectionSpring framework  IOC and Dependency Injection
Spring framework IOC and Dependency Injection
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
11 interfaces
11 interfaces11 interfaces
11 interfaces
 
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and AnswersLatest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
C questions
C questionsC questions
C questions
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
 
Assign
AssignAssign
Assign
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
The prototype property
The prototype propertyThe prototype property
The prototype property
 

More from Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
15b more gui
15b more gui15b more gui
15b more gui
 

13 life and scope

  • 1. The Role of Superclass Constructors LIS4930 © PIC Remember when you create an object it inherits the Object class automatically. FamilyDoctor object
  • 2. The Role of Superclass Constructors LIS4930 © PIC All 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. Object This all happens in a process called Constructor Chaining Doctor FamilyDoctor FamilyDoctor object
  • 3. Demo LIS4930 © PIC In class demo – but if you’re not in class, read page 252 in the textbook.
  • 4. Invoking the Superclass Constructor LIS4930 © PIC 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. But, if you wanted to call the superclass constructor explicitly, you could us: super( ) 1 If you don’t provide a constructor public ClassName( ) { super( ); } The compiler puts one in that looks like: 2 If 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 © PIC Read 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 Arguments LIS4930 © PIC Read page 255 in the textbook.
  • 7. Invoking One Overloaded Constructor From Another. LIS4930 © PIC Read page 256 in the textbook.
  • 8. Now we know how an object is born, how long does an object live? LIS4930 © PIC An 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? 1 A local variable lives only within the method that declared the variable. 2 An 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. Life A local variable is alive as long as its Stack frame is on the Stack. Scope 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. LIS4930 © PIC You can only use a variable when it is in scope.
  • 10. What about Reference Variables? LIS4930 © PIC How 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 Object LIS4930 © PIC The first object is abandoned when z is ‘reprogrammed’ to a new object The first object is abandoned when z is ‘deprogrammed’ Reference ‘z’ dies at end of method void go( ) { Life z = new Life( ); } 1 3 A reference goes out of scope, permanently The reference is explicitly set to null Life z = new Life( ); z = new Life( ); 2 The reference is assigned another object Life z = new Life( ); z = null; Turn to pages 261 – 263 in your textbook for a more detailed explanation