150 likes | 278 Views
Overloading as Polymorphism and misc. topics. Today. Constructor and Method Overloading Our first example of polymorphism Revisiting Static Class Fields and Methods Enumerations Assignment 2 still coming. Polymorphism. Poly = many, morph = forms
E N D
Today • Constructor and Method Overloading • Our first example of polymorphism • Revisiting Static Class Fields and Methods • Enumerations • Assignment 2 still coming
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. • lec5.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. • lec5.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 • lec5.v3
Method Overloading • Regular methods can also be overloaded • Same method name defined more than once. • Return type must be the same. • 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.v4, lec5.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.
Static Class Fields • Indicated by static modifier to field declaration. • Can be accessed via the class name • access modifiers can also be used to control visibility • Contrast with instance fields • Each object has its own copy of an instance field • Value of the instance fields are the state of the object • Only one copy of the class field • Associated with the class as a whole • Common uses • As named constant values associated with the class • In this case, modify the declaration of the field with the keyword final • By convention, named constants are ALL_CAPS • As collective information associated with the class as a whole. • Avoid accessing static class fields through an object. • It does work, but it is misleading and not good style • lec5.v6
Static Class Methods • Methods associated with the class but that are not associated with a specific object. • No this reference within the function. • Common uses • Getters / Setters for static class fields • Helper / auxiliary functions • Factory methods for creating new instances • We’ll revisit this in more detail later. • lec5.v7
Motivating Enumerations • Often need to model part of an object as one value from a set of finite choices • Examples: • Suite of a playing card • Day of week • Directions of a compass • One approach is to use named constants • lec5.v8 • Drawbacks of this approach • No type safety • No value safety
Simple Java Enumerations • General syntax: access_typeenumEnumName {symbol, symbol, ...}; • Example: • public enum Genre {POP, RAP, JAZZ, INDIE, CLASSICAL} • Enumeration name acts as the data type for the enumerated values. • Enumerated values available as EnumName.symbol as in: Genre.POP • Outside of the class • Fully qualified name required as in: Song.Genre.POP • Symbol names don’t have to all caps, but that is traditional • lec5.v9
Not so simple enumerations • Java enumerations are actually much more powerful than this. • Check out this tutorial for more: http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html
Notes on using Arrays • Uninitialized arrays of reference types have all elements set to null. • You need to create / set the elements individually • Multidimensional Arrays • 2D arrays are simply an “array of arrays” • And so on for higher dimensions • Can specify all dimension sizes specified at time of declaration • Results in an “even” array • Can make “ragged” array by only specifying first dimension and then explicitly creating each subarray • lec5.v09 • Arrays are passed by reference • If passed to a method, any alterations to array made by the method affect original array • Similarly, if exposed as part of an object, breaks encapsulation • lec5.v10
Java Collection Framework • Arrays are not resizeable • Often need a collection of items that acts like an array than can grow or shrink • Java Collection Framework