0% found this document useful (0 votes)
6 views7 pages

Java Constructor and Method Overloading Lab

lab8

Uploaded by

2023200000727
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Java Constructor and Method Overloading Lab

lab8

Uploaded by

2023200000727
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Southeast University

program: [Link]. in CSE


Course Code: CSE 282.3
Department of Computer Science & Engineering
Course Title: Programming Language II Java Lab
Course Teacher: Mohammed Ashikur Rahman, PhD

Submitted by,
Name: [Link] Al Sajid
ID: 2023200000727
Section: 3
Lab Task No: 04
Lab Task Name: Implementation of constructor overloading,
method overloading and overriding.

Problem 01: Create a class called Person with properties such as name, age, gender,
address. Use constructor overloading, method overloading and the ‘this keyword’.

Problem 02:Create a class called Employee with properties such as name, id, salary,
designation. Use constructor overloading, method overloading and the ‘this keyword’.

Background Theory:
●​ Constructors: In Java, a constructor is a special method that is used to initialize
objects of a class. It has the same name as the class and does not have a return
type, not even void. Constructors are called implicitly when an object is created
using the new keyword. They are used to set initial values to the instance
variables of an object.
●​ Key Points about the ‘this’ Keyword:
○​ ‘this’ can be used to refer to instance variables within a class.
○​ ‘this’ can be used to invoke a constructor from another constructor within
the same class.
○​ ‘this’ can be used to return the current object from a method
●​ Object methods perform actions specific to each object, whereas class methods
are associated with the class and can be called without creating an instance of
that class.
●​ Constructor Overloading: Constructor overloading is the process of defining
multiple constructors in a class, each with a different parameter list. This allows
us to create objects with different initializations based on the provided
arguments. By overloading constructors, we can provide flexibility in creating
objects with varying properties.
●​ Method Overloading: Method overloading is the ability to have multiple methods
with the same name but different parameter lists within a class. By overloading
methods, we can perform similar operations on different sets of arguments.
Method overloading improves code readability and provides convenience by
allowing multiple ways to interact with an object.

Algorithm Design For Person Class:


1. Create a Person class.
2. Declare object variables, such as name, age, gender, and address, to
store the person’s properties.
3. Implement overloaded constructors that accept different sets of
parameters and use the “this” keyword to assign values to the instance
variables as well as call one constructor from another
4. Implement overloaded methods that perform similar operations but on
different parameter sets.
5. Create objects of the Person class using different constructors.
6. Invoke the overloaded methods on the person objects with different
arguments.
7. Display the results of the method invocations.

Code:
1.​ public class Person {
2.​ String name ;
3.​ int age ;
4.​ String gender;
5.​ String address;
6.​
7.​ Person(String name,int age,String gender,String address){
8.​ [Link]=name;
9.​ [Link]=age;
10.​ [Link]=gender;
11.​ [Link]=address;
12.​ }
13.​Person(String name,int age,String gender){
14.​ this(name,age,gender,"Banasree");
15.​}
16.​void personDisplay(){
17.​ [Link]("Person information:");
18.​ [Link]("Name:" +[Link]);
19.​ [Link]("Age:" +[Link]);
20.​ [Link]("Gender:" +[Link]);
21.​ [Link]("Address:" +[Link]);
22.​}
23.​void NextPerson(int age,String address){
24.​ [Link]=age;
25.​ [Link]=address;
26.​}
27.​void NextPerson(String name ,String gender){
28.​ [Link]=name;
29.​ [Link]=gender;
30.​}
31.​ public static void main(String[] args) {
32.​
33.​ Person p1=new Person("Sajid",22,"MAle","Rampura");
34.​ Person p2=new Person("Shanto",23,"Male");
35.​
36.​ [Link]();
37.​ [Link]();
38.​ [Link]();
39.​
40.​ [Link]();
41.​
42.​ [Link](25,"Dhanmondi");
43.​ [Link]("Ashrafi","Male");
44.​
45.​ [Link]();
46.​ [Link]();
47.​ [Link]();
48.​ }
49.​}
50.​
Output:

Algorithm Design For Employee Class:


1. Create an Employee class.
2. Declare object variables, such as name, id, salary, and designation, to store
the employee’s detail properties.
3. Implement overloaded constructors that accept different sets of parameters
and use the “this” keyword to assign values to the instance variables as well as
call one constructor from another
4. Implement overloaded methods that perform similar operations but on different
parameter sets.
5. Create objects of the Employee class using different constructors.
6. Invoke the overloaded methods on the employee objects with different
arguments.
7. Display the results of the method invocations.
Code:
1.​ public class Employee {
2.​ String name;
3.​ int id;
4.​ int salary;
5.​ String designation;
6.​
7.​ Employee(String name,int id,int salary,String designation){
8.​ [Link]=name;
9.​ [Link]=id;
10.​ [Link]=salary;
11.​ [Link]=designation;
12.​ }
13.​ Employee(String name,int id,int salary){
14.​ this(name,id,salary,"Engineer");
15.​ }
16.​ void employeeDisplay(){
17.​ [Link]("Employee Information:");
18.​ [Link]("Name:"+[Link]);
19.​ [Link]("ID:"+[Link]);
20.​ [Link]("Salary:"+[Link]);
21.​ [Link]("Designation:"+[Link]);
22.​ }
23.​void NextEmployee(String name, int id, String designation){
24.​ [Link]=name;
25.​ [Link]=id;
26.​ [Link]=designation;
27.​}
28.​void NextEmployee(String name, int salary){
29.​ [Link]=name;
30.​ [Link]=salary;
31.​}
32.​
33.​ public static void main(String[] args) {
34.​
35.​ Employee e1=new Employee("Sajid",727,25000,"CEO");
36.​ Employee e2=new Employee("Shanto",728,30000);
37.​
38.​ [Link]();
39.​ [Link]();
40.​ [Link]();
41.​
42.​ [Link]();
43.​
44.​ [Link]("Alif",700, "CEO");
45.​ [Link]("Ashrafi",35000);
46.​
47.​ [Link]();
48.​ [Link]();
49.​ [Link]();
50.​
51.​
52.​ }
53.​}

Output:
.

Common questions

Powered by AI

The Java examples demonstrate key object-oriented programming principles such as encapsulation, inheritance, and polymorphism. Encapsulation is shown through the grouping of related variables and methods into classes ('Person' and 'Employee'), though true data hiding isn't fully realized due to the lack of private modifier use. Polymorphism is displayed via method overloading, where methods with the same name operate on different inputs. While inheritance isn't directly demonstrated as there's no superclass, the design reflects OOP thinking by potentially extending these classes if needed. This structured approach encapsulates behavior and state, while enabling code reuse and flexibility .

The 'NextPerson' and 'NextEmployee' methods illustrate method overloading by providing different operations with the same method name but distinct parameter lists. For example, 'NextPerson' has overloaded versions that take different sets of parameters: one accepts an age and address, while another takes a name and gender. Similarly, 'NextEmployee' is overloaded with one version taking a name, id, and designation, and another taking a name and salary. These demonstrate method overloading by allowing the same operation name to handle distinct types of updates based on diverse input parameter sets .

Object creation is crucial in executing instance methods as these methods operate on specific objects, utilizing their state or properties. In the provided Java examples, 'Person' and 'Employee' objects are created by instantiating classes using constructors. These objects are then used to execute methods like 'personDisplay' and 'employeeDisplay', which operate based on the object’s attribute values. Therefore, object creation is a prerequisite for method execution and plays a central role in the object-oriented paradigm by enabling encapsulation and data manipulation specific to the instance .

Method overloading can be misused if it leads to excessive and unnecessary duplication of method names without a clear benefit, causing confusion about the purpose and differences between overloaded methods. Pitfalls include defining methods with overly similar parameter lists, leading to ambiguity and making it difficult to discern which method version will be called. Furthermore, relying too heavily on method overloading can create difficulties in maintenance and debugging, especially if method functionality varies significantly. From the examples, care should be taken to ensure that overloaded methods have distinct and logical purposes to prevent such issues .

Constructor overloading is the process of creating multiple constructors within a class with different parameter lists, allowing objects to be instantiated in different ways. In the 'Person' class, constructor overloading is implemented with two constructors: one takes four parameters (name, age, gender, address) and the other takes three parameters (name, age, gender) and uses 'this' to initialize the address to 'Banasree' by calling the other constructor. This enables creation of 'Person' objects with flexibility in initial values .

Data encapsulation in Java is achieved by restricting direct access to object properties and instead providing public methods to access and modify them. This protects the integrity of the data within an object by ensuring it can only be changed or retrieved through controlled methods. In the 'Person' and 'Employee' classes, encapsulation is somewhat demonstrated, as the properties are assigned and modified primarily through constructors and method calls, though full encapsulation is not seen here due to the direct access to instance variables, as Java best practices would suggest using private access modifiers and providing getter and setter methods for better encapsulation .

In Java, class methods (static methods) are associated with the class and can be called without creating an instance, functioning similarly across all instances by accessing static variables or performing general utility tasks. Instance methods require an object of the class and can access instance-specific properties, thereby allowing individualized behavior based on the object's data. The choice between using a class method or an instance method is dictated by whether the task is related to a specific instance or is a broader function applicable to the class as a whole. In the given examples, the methods are instance methods because they operate on specific object properties, showcasing instance-specific behavior .

In the Java examples, the 'this' keyword is used to invoke constructors from another constructor within the same class, which is a form of constructor chaining. For instance, in the 'Person' class, the constructor taking three parameters invokes the four-parameter constructor using 'this(name, age, gender, "Banasree")'. Similarly, in the 'Employee' class, a constructor with three parameters invokes a four-parameter constructor using 'this(name, id, salary, "Engineer")'. This approach facilitates code reuse and simplifies object initialization by reducing redundancy .

Constructor chaining allows constructors to call other constructors, either within the same class or in the superclass, facilitating code reuse and minimizing redundancy. This is significant as it leads to a streamlined initialization process, ensuring consistency and avoiding errors from repeating initialization logic. In the Java examples, constructor chaining is achieved using the 'this' keyword, where a constructor with fewer parameters calls one with more parameters. This creates a hierarchy of constructors, efficiently handling different cases of object initialization with shared common logic .

Method overloading in Java allows multiple methods with the same name but different parameter lists, enabling similar operations on different sets of arguments. In the 'Person' and 'Employee' classes, method overloading provides multiple ways to update object attributes, such as using 'NextPerson' or 'NextEmployee' methods with different parameters. This improves code readability by keeping method names consistent while providing flexibility in usage, as it lets programmers intuitively guess method functionality without needing to remember different method names for slight variations in operations .

You might also like