1 / 12

More Encapsulation Overloading

More Encapsulation 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 location in memory.

moira
Download Presentation

More Encapsulation Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More EncapsulationOverloading COMP 401, Spring 2014 Lecture 6 1/28/2013

  2. 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

  3. 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

  4. 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”

  5. 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.

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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.

More Related