0% found this document useful (0 votes)
25 views

Comp401sp14lec06MoreEncaps Overloading

The document discusses reference variables, null references, immutability, polymorphism, and overloading in object-oriented programming. Reference variables hold the memory location of an object. If two reference variables point to the same object, a change made through one variable is visible through the other. Null means a reference points to nothing. Immutable objects cannot change state after construction. Polymorphism provides the same interface in different forms. Constructor and method overloading allow multiple implementations distinguished by parameters.

Uploaded by

petahpen7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Comp401sp14lec06MoreEncaps Overloading

The document discusses reference variables, null references, immutability, polymorphism, and overloading in object-oriented programming. Reference variables hold the memory location of an object. If two reference variables point to the same object, a change made through one variable is visible through the other. Null means a reference points to nothing. Immutable objects cannot change state after construction. Polymorphism provides the same interface in different forms. Constructor and method overloading allow multiple implementations distinguished by parameters.

Uploaded by

petahpen7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

More

Encapsula.on
Overloading
COMP 401, Spring 2014
Lecture 6
1/28/2013

The Value of A Reference Variable


A variable for a reference type holds a reference to an
object in memory.
Also known as a pointer
The value of this reference is a loca.on in memory.

If you set a reference variable equal to the value of


another reference variable, you now have two
variables the point to the same object.

More importantly, if you use one variable to change the


underlying object, the other variable sees that change.
Really because there is only one object with two variables that
point to it.

lec6.ex1

null references
The value null is valid for any reference type
variable
Really means no value at all.
Or in other words, this variable doesnt point to
anything.

Any aUempt to access instance elds or methods


will result in NullPointerExcep.on
If a variable could legi.mately be null, then your code
needs to check for null before trying to dereference it.

lec6.ex2

The Merits of Immutability


An immutable object is one whose elds (i.e.,
state) are set upon construc.on and do not
change.
Implica.on: no seUers, just geUers

Why immutability?
Can be shared as a part of a plurality of other
objects without danger.
Automa.cally thread-safe

lec06.ex3
If points are immutable, then triangle class
does not have to worry about points changing.
Related to the principle of encapsula.on.

Suppose we wanted an immutable triangle


class that worked with possibly mutable
points.

Arrays Are Mutable


Be aware of passing arrays to/from methods.
Even though individual elements of an array may
be immutable, the array itself is not.
Element may be changed to be something new or
dierent.
Permanently aects the array which may not be what
you intend.

lec6.ex4

Polymorphism
Poly = many, morph = forms
General principle of providing access to an
abstrac.on or method in many forms
Idea is that dierent forms t dierent contexts
Note: underlying func.onality is the same.

In OO programming, principle is evident in a


number of dierent places.
Constructor overloading
Method overloading

Constructors
What happens when you dont dene a
constructor.
Default constructor with no arguments.
Creates new object with all elds set to default value
Numeric elds set to 0
Boolean elds set to false
String, Array, and any other sort of reference value eld set to
null.

lec6.ex5.v1

Constructor Overloading
Can dene mul.ple versions of the constructor.
Dis.nguished from each other by type and number of
parameters
Must be some dierence otherwise the compiler wont be
able to tell them apart.

When you use the constructor, the right one will be


chosen based on the parameters provided.
Note that if you s.ll want a default no-argument
constructor, you have to provide it explicitly.

lec6.ex5.v2

Constructor Chaining
Common paUern is to chain one constructor o
of another.
First line of code in the constructor must be the this
keyword as a func.on with parameters
Matching constructor is called rst and allowed to
execute.
Then remaining code in original constructor called.
Can chain mul.ple constructors one on to another

lec6.ex5.v3

Method Overloading
Regular methods can also be overloaded

Same method name dened more than once.

Return type may not be the same.


But usually is.

Method type must be the same.

Instance method or sta.c class method

Parameter list must somehow be dierent

Again, this is how the compiler knows which one is meant.


Either dierent in number or type (or both)

One version can call another


No restric.ons on when
No special syntax

lec5.ex5.v4, lec6.ex5.v5

Why Overload?
Provides access to constructor / method in a
more context specic way.
Limita.ons of overloading
Does not handle the case when you have two
dierent situa.ons that arent dis.nguished by
the number or type of parameters being passed.

You might also like