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 student\text{student} class constructor initializes a student object by accepting:

    • Student name (String\text{String})

    • Student number (int\text{int})

    • Number of courses (int\text{int})

    • Pre-existing faculty reference variable (faculty\text{faculty} object reference)

  • Overloading Strategy 1: Changing Parameter Order:

    • Changing the sequence of data types (for example, swapping a String\text{String} and an int\text{int} parameter) 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 faculty\text{faculty} object, an overloaded constructor can accept individual primitive/composite attributes that define a faculty:

    • Faculty name (f_name\text{f\_name} or at_name\text{at\_name} of type String\text{String})

    • Tuition cost (tuition\text{tuition} of type double\text{double})

    • The constructor uses these individual parameter values to internally construct a new faculty\text{faculty} object during student initialization.

Code Structure and Implementation Details

  • Constructor Signature for Deconstructed Faculty Parameters:

    • Method header: public student(String name, int studentNumber, int numCourses, String f_name, double tuition)\text{public student(String name, int studentNumber, int numCourses, String f\_name, double tuition)}

  • 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., String name\text{String name}).

    • 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 faculty\text{faculty} object is instantiated using the new\text{new} keyword and the faculty\text{faculty} class constructor.

    • Syntax: faculty=new faculty(f_name,tuition)\text{faculty} = \text{new faculty}(f\_name, tuition)

    • Parameter order must strictly match the signature defined in the faculty\text{faculty} class constructor (passing faculty name first, followed by tuition).

  • Instance Attribute Assignment and the this Keyword:

    • 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 name\text{name} and instance attribute name\text{name}), shadowing occurs.

    • The this\text{this} keyword must prefix the attribute name (this.name = name\text{this.name = 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., f_name\text{f\_name} assigned to faculty\text{faculty}), the this\text{this} prefix 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 faculty\text{faculty} object can be created once and shared across multiple student\text{student} object 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 student\text{student} object without requiring the client code to explicitly manage or pre-instantiate a separate faculty\text{faculty} object instance beforehand.

Accessor and Mutator Method Implementations

  • Accessor Methods (Getters):

    • Accessor methods retrieve the current values of instance attributes.

    • num courses accessor:

    • Return type: int\text{int}

    • Body returns the integer value of number of courses.

    • student number accessor:

    • Return type: int\text{int}

    • Body returns the integer student number.

    • faculty accessor:

    • Return type: faculty\text{faculty} (the class type, capitalized).

    • Method signature: public faculty getFaculty()\text{public faculty getFaculty}()

    • Return statement: Returns faculty\text{faculty} (the lowercase instance variable holding the object reference address).

    • Behavior: Returning a faculty\text{faculty} variable returns the memory address/reference pointing to the faculty\text{faculty} object. The caller can use this returned reference to execute methods inside the faculty\text{faculty} class (such as retrieving tuition data).

  • Mutator Methods (Setters):

    • Mutator methods modify the internal values of instance variables post-instantiation.

    • Required mutators in student\text{student} class:

    • Mutator for faculty\text{faculty} attribute.

    • Mutator for number of courses\text{number of courses} attribute.