Java Method and Constructor Overloading Study Guide
Method Overloading Principles in Java
Definition of Method Overloading:
Method overloading occurs when a class contains multiple methods that share the exact same method name.
While class attributes (variables) require unique names so Java can distinguish between them, methods are permitted to share names.
Java Signature Identification Mechanism:
Java distinguishes between methods with identical names by examining their parameter lists.
For Java to successfully match and execute a method call, both the method name and the parameter signatures must match.
Parameter signature matching is determined by three factors:
The number of parameters.
The data types of the parameters.
The specific order and sequence of parameter data types.
If a method call matches the method name but fails to match the required parameter list, Java produces a compilation error indicating the method cannot be found.
Constructor Overloading Mechanics
Definition and Purpose:
Constructor overloading is the practice of creating multiple constructors within the same class, each accepting a different parameter list.
This allows object instantiation using different combinations or sources of data.
Constructor Naming Constraints:
A constructor is a special type of method designed to initialize objects.
A constructor must share the exact same name as the class in which it is defined.
Because the constructor name cannot be changed or varied, overloading is the only mechanism for defining multiple constructors within a single class.
Task Consistency in Overloaded Methods:
Overloaded methods and constructors should perform the same fundamental underlying task (e.g., initializing an object) using different types or structures of input data.
Implementation Strategies for Constructor Overloading in a Student Class
Original Constructor Baseline:
The initial
class constructor initializes a student object by accepting:Student name (
)Student number (
)Number of courses (
)Pre-existing faculty reference variable (
object reference)
Overloading Strategy 1: Changing Parameter Order:
Changing the sequence of data types (for example, swapping a
and anparameter) creates a distinct signature recognized by Java as a valid overload.Although changing parameter order creates an overloaded constructor, it still requires the exact same set of underlying information.
Overloading Strategy 2: Deconstructing Object Parameters:
Instead of requiring an address/reference to an already existing
object, an overloaded constructor can accept individual primitive/composite attributes that define a faculty:Faculty name (
orof type)Tuition cost (
of type)The constructor uses these individual parameter values to internally construct a new
object during student initialization.
Code Structure and Implementation Details
Constructor Signature for Deconstructed Faculty Parameters:
Method header:
Parameter Scope and Variable Naming:
The scope of parameters declared in a constructor header is strictly limited to the body of that specific constructor.
Parameter names can match or differ from instance attribute names without affecting functionality.
Variable Declaration Rules:
Variable declaration includes specifying both data type and variable name (e.g.,
).Once a variable or parameter is declared, it must never be re-declared with its data type inside the body of the same scope. Re-declaring existing instance variables or parameters causes compiler syntax errors.
Internal Object Instantiation with
new:Inside the constructor body, the internal
object is instantiated using thekeyword and theclass constructor.Syntax:
Parameter order must strictly match the signature defined in the
class constructor (passing faculty name first, followed by tuition).
Instance Attribute Assignment and the
thisKeyword:Attributes can be assigned in any order within the constructor body.
When a parameter variable name matches an instance attribute variable name (e.g., parameter
and instance attribute), shadowing occurs.The
keyword must prefix the attribute name () to distinguish the class instance attribute from the local parameter variable.If a parameter name does not shadow an instance variable name (e.g.,
assigned to), theprefix is not required.
Practical Trade-Offs of Overloaded Constructor Designs
Passing Pre-Existing Object References (Option 1):
Advantage: Object reusability and memory efficiency.
Use Case: In a driver program with many students enrolled in the same faculty (e.g., a CS faculty), a single
object can be created once and shared across multipleobject instantiations by passing the same reference variable.
Passing Direct Value Components (Option 2):
Advantage: Simplifies client/driver code.
Use Case: Allows direct creation of a
object without requiring the client code to explicitly manage or pre-instantiate a separateobject instance beforehand.
Accessor and Mutator Method Implementations
Accessor Methods (Getters):
Accessor methods retrieve the current values of instance attributes.
num coursesaccessor:Return type:
Body returns the integer value of number of courses.
student numberaccessor:Return type:
Body returns the integer student number.
facultyaccessor:Return type:
(the class type, capitalized).Method signature:
Return statement: Returns
(the lowercase instance variable holding the object reference address).Behavior: Returning a
variable returns the memory address/reference pointing to theobject. The caller can use this returned reference to execute methods inside theclass (such as retrieving tuition data).
Mutator Methods (Setters):
Mutator methods modify the internal values of instance variables post-instantiation.
Required mutators in
class:Mutator for
attribute.Mutator for
attribute.