CSCI 1301 September 18, 2001 Lecture 10 Constructors - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CSCI 1301 September 18, 2001 Lecture 10 Constructors

Description:

Required information on outside of manila envelope when you submit ... We could call the 'setter' methods as soon as the object is created. ( setName(..), etc) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: ValuedGate1945
Category:

less

Transcript and Presenter's Notes

Title: CSCI 1301 September 18, 2001 Lecture 10 Constructors


1
CSCI 1301 September 18, 2001Lecture
10Constructors
  • Topics for today
  • Announcements
  • Constructors
  • Tracing Constructor Calls

2
Announcements
  • Reading Chapter 3 in ProgramLive
  • Assignment 2 - DUE TODAY!
  • Required information on outside of manila
    envelope when you submit an assignment is
  • Your name
  • Your instructors name CSCI 1301
  • The assignment number
  • Your lab day, lab time, and lab instructors
    name
  • (This has been on the syllabus since day 1, and
    is now part of the Rules Style Sheet see
    web page.)

3
Constructors - motivation
  • When we declare a primitive variable, we can also
    provide a starting value.
  • int x 15
  • When objects are constructed, they have default
    starting values.
  • numbers zero
  • boolean false
  • characters empty
  • objects - null

How to start objects out at specific starting
values?
4
  • What about doing the following
  • public class Employee
  • private String name Joe
  • private double salary 50000
  • private int hireYear 2000
  • (Now every object we create has the same name,
    salary and hireYear! (Joe, 50000, 2000)
  • We could call the setter methods as soon as the
    object is created. (setName(..), etc)
  • We can write a constructor method(s) to
    initialize the instance variables upon object
    instantiation.

???
5
// Constructor to initialize name, salary and
hireYear public Employee ( String n, double s,
int y) name n salary s hireYear y
No type or void
a method inside the class
Name is same as class name
call on the constructor at the time of object
creation
// call on the constructor shown above Employee
e new Employee ( Joe, 80000, 1999)
6
Tracing Constructor Calls
Employee e new Employee ( Joe, 80000, 1999)
Employee
Lets trace the constructor call
// Constructor to initialize name, salary and
hireYear public Employee ( String n, double s,
int y) name n salary s hireYear y
7
Constructors
  • Special method invoked when an object is created
  • Purpose is to initialize the instance variables
    of the object
  • Often overloaded to provide flexibility
  • Different than a regular method
  • - name must be the name of the class
  • - have no return type and no void prefix
  • - automatically invoked in the new expression
    upon object creation

What does this mean?
8
Question How have we gotten along so far
without providing a constructor? Answer If you
dont provide any constructors, Java will provide
a default constructor with no parameters, that
does nothing.
For class Employee from last time
// default constructor public Employee ( )
Warning If you provide even a single
constructor, Java does not provide a default
constructor!
9
public class Employee private String name
private double salary private int hireYear
// Constructor to initialize name, // salary
and hireYear public Employee(String n,
double s, int y) name n salary s
hireYear y // Return the year the
person was hired public int getHireYear()
return hireYear // Return the persons
salary public double getSalary() return salary
Allows initialization of all 3 instance variables
upon creation
// Return the person's name public String
getName() return name //.methods
setName, etc go here // Raise the pay by p
percent public void raiseSalary(double p)
salary salary (1 p/100.0) // Yield a
String with persons data public String
toString() String s name s s
" " salary " "
hireYear return s
10
Possible object instantiations using class on
previous slide Employee s new Employee(Sue,
100000, 1997) Employee k new Employee(Kim,
75000, 1999)
What about Employee e new Employee()
Not with the class on previous page! No Default
constructor!
Since we provided a constructor to initialize all
three instance variables, no default is provided
automatically, and the above call is to the
default constructor (no parameters). Moral
provide a default constructor if you provide any
others unless you never want a default
initialization to happen.
11
public class Employee private String name
private double salary private int
hireYear // default constructor public
Employee( ) // constructor to initialize
name, // salary and hireYear public
Employee(String n, double s, int y)
name n salary s hireYear y //.
methods setName, etc go here // Return the year
the person was hired public int getHireYear()
return hireYear
// Return the persons salary public double
getSalary() return salary // Return the
person's name public String getName()
return name // Raise the pay by p percent
public void raiseSalary(double p) salary
salary (1 p/100.0) // Yield a String
with persons data public String toString()
String s name s s " " salary
" " hireYear
return s
2 constructors
12
  • Steps in evaluating a call on a constructor
  • new Class-Name(arguments)
  • 1. Create a new instance of class Class-Name with
    fields initialized to default values
  • 2. Execute the call (Scope box contains name of
    newly created object.)
  • Class-Name(arguments)
  • Yield the name of the newly created object.
    (Assign it to the variable for the object we are
    creating.)

Recall the steps used in executing a method call.
These take place here.
13
Execution of constructor call in the statement
Employee e new Employee(Joe, 80000,
1999) 1. Evaluate the arguments. 2. Draw frame
for the call --its scope box contains the name of
the new instance. 3. Draw in the parameters and
local variables. 4. Assign arguments to
corresponding parameters. 5. Execute constructor
body. 6. Erase the frame.
Lets execute this
14
Employee e new Employee(Joe, 80000, 1999)
15
Add a constructor with two parameters (name
salary), that assumes year is 2001.
// Constructor to initialize name and salary
// assume hireYear is 2001 public
Employee(String n, double s) name n
salary s hireYear 2001
We already have one to do most of this work the
one to initialize all three instance variables.
We can write the two parameter constructor to
call upon the one we already have.
// Constructor to initialize name and salary
// assume hireYear is 2001 public
Employee(String n, double s) this(n, s,
2001)
call on the 3-parameter constructor in this
instance
the current object
16
  • We have overloaded the constructor.
  • differing levels of initialization
  • more flexibility
  • less repetitive code
  • Calling one constructor from another
  • called constructor chaining
  • preferred approach
  • MUST be the first statement in the constructor
    method
  • other statements may follow

17
// Constructor to initialize name and // salary
assume hireYear is 2001 public Employee(String
n, double s) this(n, s, 2001) //.
methods setName, etc go here // Yield a String
with persons data public String toString()
String s name s s " " salary
" " hireYear
return s
public class Employee private String name
private double salary private int
hireYear // default constructor public
Employee( ) // constructor to initialize
name, // salary and hireYear public
Employee(String n, double s, int y)
name n salary s hireYear y
18
public Employee( ) public Employee(String n,
double s, int y) public Employee(String n,
double s)
With these three constructors we can create
objects in the following ways
Employee a new Employee ( ) Employee b new
Employee ( Snoopy, 500) Employee c new
Employee (Opus, 100, 1950)
We will trace the creation of Employee b
(You should try tracing the others.)
19
Employee b new Employee ( Snoopy, 500)
// Constructor to initialize name, salary and
hireYear public Employee ( String n, double s,
int y) name n salary s hireYear y
// Constructor to initialize name and // salary
- assume hireYear is 2000 public Employee(String
n, double s) this(n, s, 2000)
Write a Comment
User Comments (0)
About PowerShow.com