Unformatted text preview:

Overloading as Polymorphism and misc topics COMP 401 Spring 2013 Lecture 5 1 24 2013 Today Constructor and Method Overloading Our rst example of polymorphism RevisiHng StaHc Class Fields and Methods EnumeraHons Assignment 2 sHll coming Polymorphism Poly many morph forms General principle of providing access to an abstracHon or method in many forms Idea is that di erent forms t di erent contexts Note underlying funcHonality is the same In OO programming principle is evident in a number of di erent places Constructor overloading Method overloading Constructors What happens when you don t de ne 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 lec5 v01 Constructor Overloading Can de ne mulHple versions of the constructor DisHnguished from each other by type and number of parameters Must be some di erence 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 sHll want a default no argument constructor you have to provide it explicitly lec5 v02 Constructor Chaining Common pa ern is to chain one constructor o of another First line of code in the constructor must be the this keyword as a funcHon with parameters Matching constructor is called rst and allowed to execute Then remaining code in original constructor called Can chain mulHple constructors one on to another lec5 v03 Method Overloading Regular methods can also be overloaded Same method name de ned more than once Return type must be the same Method type must be the same Instance method or staHc class method Parameter list must somehow be di erent Again this is how the compiler knows which one is meant Either di erent in number or type or both One version can call another No restricHons on when No special syntax lec5 v04 lec5 v05 Why Overload Provides access to constructor method in a more context speci c way LimitaHons of overloading Does not handle the case when you have two di erent situaHons that aren t disHnguished by the number or type of parameters being passed StaHc Class Fields Indicated by sta c modi er to eld declaraHon Can be accessed via the class name access modi ers can also be used to control visibility Contrast with instance elds Each object has its own copy of an instance eld Value of the instance elds are the state of the object Only one copy of the class eld Associated with the class as a whole Common uses As named constant values associated with the class In this case modify the declaraHon of the eld with the keyword nal By convenHon named constants are ALL CAPS As collecHve informaHon associated with the class as a whole Avoid accessing staHc class elds through an object It does work but it is misleading and not good style lec5 v06 StaHc Class Methods Methods associated with the class but that are not associated with a speci c object No this reference within the funcHon Common uses Ge ers Se ers for staHc class elds Helper auxiliary funcHons Factory methods for creaHng new instances We ll revisit this in more detail later lec5 v07 MoHvaHng EnumeraHons Ofen need to model part of an object as one value from a set of nite choices Examples Suite of a playing card Day of week DirecHons of a compass One approach is to use named constants lec5 v08 Drawbacks of this approach No type safety No value safety Simple Java EnumeraHons General syntax access type enum EnumName symbol symbol Example public enum Genre POP RAP JAZZ INDIE CLASSICAL EnumeraHon 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 quali ed name required as in Song Genre POP Symbol names don t have to all caps but that is tradiHonal lec5 v09 Not so simple enumeraHons Java enumeraHons are actually much more powerful than this Check out this tutorial for more h p javarevisited blogspot com 2011 08 enum in java example tutorial html


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lec05Overloading_Misc

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lec05Overloading_Misc and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lec05Overloading_Misc and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?