Experiment 4
Experiment 4
Experiment no 4
Theory
Method overloading
Method overloading in Java is a feature of polymorphism that allows a class to have multiple methods with the
same name but different parameter lists (type, number, or both). This enables the same method name to
perform different tasks based on the input parameters, making the code more flexible and easier to read. It is
resolved at compile time, meaning the appropriate method is selected by the compiler based on the method
signature (name and parameter list) used in the call. Method overloading is a key aspect of polymorphism,
which allows one interface to be used for a general class of actions.
Constructors
Constructors in Java are special methods used to initialize objects when they are created. Unlike regular
methods, constructors have the same name as the class and do not have a return type, not even void. They are
called automatically when a new instance of a class is instantiated using the new keyword. Constructors can be
overloaded, meaning a class can have multiple constructors with different parameter lists, allowing objects to be
initialized in various ways. The primary purpose of a constructor is to set up the initial state of an object, such as
initializing variables or allocating resources.
1. Object Initialization: The primary use of constructors is to initialize an object's state when it is created.
They set initial values for instance variables and perform any setup required for the object.
2. Overloading Flexibility: Constructors can be overloaded, allowing objects to be created with different sets
of parameters, providing flexibility in how objects are initialized. For example, a Person class could have
constructors for creating a person with just a name, or with a name and age.
3. Encapsulation and Validation: Constructors can enforce rules or validation during object creation. For
example, a constructor can check if the provided parameters meet certain conditions (like a non-negative age)
before allowing the object to be created.
4. Resource Allocation: Constructors can allocate resources that the object might need during its lifetime,
such as opening files, establishing database connections, or allocating memory for data structures.
1. Program to calculate area of square, rectangle, circle or triangle as desired by user, demonstrating
the use of method overloading
Anish Dharnidhar AI & DS Batch S23 Roll no : 129
Output:
Anish Dharnidhar AI & DS Batch S23 Roll no : 129
2. Program to create a class called "Book" with instance variables: title, author & price. Implement a
default constructor & 2 parameterized constructor:
· One constructor takes title & author as parameters. The other constructor takes title,
author & price as parameters. Print the values of the variables for each constructor.
Code:
Output:
Anish Dharnidhar AI & DS Batch S23 Roll no : 129
3. Program to create a class called as "Account" with instance variables: accNumber & balance.
Implement a parameterized constructor that initializes these variables with validation: accNumber
should be non-NULL & non-empty. balance should be not negative. Print an error message if the
validation fails.
Code:
Anish Dharnidhar AI & DS Batch S23 Roll no : 129
Output: