Constructors & Garbage Collection Ch. 9 – Head First Java.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Road Map Introduction to object oriented programming. Classes
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Programming, 4E Y. Daniel Liang.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Java Implementation: Part 3 Software Construction Lecture 8.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Design issues for Object-Oriented Languages
The Object-Oriented Thought Process Chapter 03
Class Inheritance Part I
Static data members Constructors and Destructors
Andy Wang Object Oriented Programming in C++ COP 3330
University of Central Florida COP 3330 Object Oriented Programming
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
This pointer, Dynamic memory allocation, Constructors and Destructor
CSC 113 Tutorial QUIZ I.
Overloading and Overriding
Chapter 10: Method Overriding and method Overloading
Chapter 6 Objects and Classes
Method Overriding and method Overloading
Classes and Objects Object Creation
Corresponds with Chapter 5
Chapter 7 Objects and Classes
Presentation transcript:

Constructors & Garbage Collection Ch. 9 – Head First Java

Review from 1 st part of Ch. 9 Java has 2 areas of memory – stack & heap Instance variable declared inside a class, outside of a method Local variables declared inside a method or method parameter All local variables live on the stack, in the frame corresponding to the method declared Object reference variables work like primitive variables All objects live in the heap

The Miracle of Object Creation 3 Steps – Declare a Reference Variable – Create an Object (miracle) – Link the Object and the Reference. Duck myDuck = new Duck () Constructor Looks like a method, but it isn’t The code that runs when you say new. The code that runs when you instantiate (create) an object

Constructors Only way to invoke a constructor is with the keyword new, followed by the Class name Every class you create has a constructor, even if you don’t write it yourself – The compiler writes one for you!! The default constructor is always a no-arg constructor.

Construct a Duck Constructor runs before the object can be assigned to a reference You get a chance to step in and do things to get the object ready for use Initializing the state of a new Duck Most people use constructors to initialize the state of an object Make a constructor with Arguments – p

Overloaded Constructors More than one constructor in a class To compile, each constructor must have a different argument list You can have two constructors that have identical types, as long as the order is different.

Bullet Points Page 247!! Page 249 – four points: – A constructor is the code that runs when someone says new on a class type – A constructor must have the same name as the class, and no return type – If you don’t put a constructor in your class, the compiler puts in a default constructor. – You can have more than one constructor in your class; as long as arguments are different; overloaded constructor.

Superclasses & Constructors When an object is created, the object gets space for all the instance variables from all the way up the inheritance tree It’s almost as if multiple objects materialize. The object created has layers of itself representing each superclass.

Role of Superclass Constructors in an Object’s Life All the constructors in an object’s inheritance tree must run when you make a new object. Saying “new” is a Big Deal. – Starts the constructor chain reaction. When a constructor runs, it immediately calls its superclass constructor, all the way up the tree to the Object constructor. Process is called Constructor Chaining

Page 252 Output example & stack when objects are called. Sharpen your pencil; How do you invoke a superclass constructor? Only way to call is by calling super () Puts the superclass constructors on the top of the stack. If you don’t put one in, the compiler does it for you.

Order of things; Parent 1 st child next Superclass of an object have to be fully formed (completely built) before the subclass parts can be constructed. Each subclass constructor immediately invokes its own superclass constructor until the Object constructor is on the top of the Stack. The call to super() must be the first statement in each constructor. Examples – bottom of page 254

Superclass Constructors with Arguments You can pass arguments into the super() call Invoking one overloaded constructor from another – Use this() to call a constructor from another overloaded constructor in the same class – Every constructor can have a call to super() or this(), but never both!!

How long does an object live? Depends on the life of references that refer to it. If the reference is alive, the object is too How long does a variable live – Depends on whether the variable is a local variable or an instance variable

Local v. Instance Variables A local variable lives only within the method that declared the variable An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.

Difference between life and scope for local variables Life – a variable is alive as long as its Stack frame is on the stack; until the method completes Scope – A local variable is in scope only within the method in which the variable was declared. You can use a variable only when it is in scope.

What about reference variables? Rules for primitives & references – the same A reference variable can only be used when it’s in scope; you can’t use an object’s remote control unless you’ve got a reference variable that’s in scope. An object is alive as long there are live references to it. Trick – to know the point at which an object is eligible for garbage collection

Three ways to get rid of an object’s reference 1.The reference permanently goes out of scope 2.The reference is assigned another object 3.The reference is explicitly set to null.

Static Methods Public static int min(int a, int b) { – No objects in a static method Math methods - don’t use any instance variable values All you need is the math “class” Don’t need to have an “instance” of Math

Math – static methods Math Methods – Math.random() – Math.abs() – Math.round() – Math.min() – Math.max()