120 likes | 246 Vues
This lecture covers fundamental concepts in object-oriented programming including encapsulation, reference variables, immutability, and method/constructor overloading. A reference variable acts like a pointer, highlighting the importance of how changes to an object via one reference are reflected through another. Additionally, immutability guarantees thread safety and prevents unintended modifications. The lecture also explains the nuances of constructor and method overloading, enabling developers to create more versatile and context-specific code.
E N D
More EncapsulationOverloading 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 location 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 doesn’t point to anything. • Any attempt to access instance fields or methods will result in NullPointerException • If a variable could legitimately 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 fields (i.e., state) are set upon construction and do not change. • Implication: no setters, just getters • Why immutability? • Can be shared as a part of a plurality of other objects without danger. • Automatically “thread-safe”
lec06.ex3 • If points are immutable, then triangle class does not have to worry about points changing. • Related to the principle of encapsulation. • 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 different. • Permanently affects the array which may not be what you intend. • lec6.ex4
Polymorphism • Poly = many, morph = forms • General principle of providing access to an abstraction or method in many forms • Idea is that different forms “fit” different contexts • Note: underlying functionality is the same. • In OO programming, principle is evident in a number of different places. • Constructor overloading • Method overloading
Constructors • What happens when you don’t define a constructor. • Default constructor with no arguments. • Creates new object with all fields set to default value • Numeric fields set to 0 • Boolean fields set to false • String, Array, and any other sort of reference value field set to null. • lec6.ex5.v1
Constructor Overloading • Can define multiple versions of the constructor. • Distinguished from each other by type and number of parameters • Must be some difference otherwise the compiler won’t 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 still want a default no-argument constructor, you have to provide it explicitly. • lec6.ex5.v2
Constructor Chaining • Common pattern is to “chain” one constructor off of another. • First line of code in the constructor must be the this keyword as a function with parameters • Matching constructor is called first and allowed to execute. • Then remaining code in original constructor called. • Can chain multiple constructors one on to another • lec6.ex5.v3
Method Overloading • Regular methods can also be overloaded • Same method name defined more than once. • Return type may not be the same. • But usually is. • Method type must be the same. • Instance method or static class method • Parameter list must somehow be different • Again, this is how the compiler knows which one is meant. • Either different in number or type (or both) • One version can call another • No restrictions on when • No special syntax • lec5.ex5.v4, lec6.ex5.v5
Why Overload? • Provides access to constructor / method in a more context specific way. • Limitations of overloading • Does not handle the case when you have two different situations that aren’t distinguished by the number or type of parameters being passed.