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

Java Lab 5

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)
10 views7 pages

Java Lab 5

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
You are on page 1/ 7

Southeast University

program: B.Sc. 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: Md.Abdullah 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.​ this.name=name;
9.​ this.age=age;
10.​ this.gender=gender;
11.​ this.address=address;
12.​ }
13.​Person(String name,int age,String gender){
14.​ this(name,age,gender,"Banasree");
15.​}
16.​void personDisplay(){
17.​ System.out.println("Person information:");
18.​ System.out.println("Name:" +this.name);
19.​ System.out.println("Age:" +this.age);
20.​ System.out.println("Gender:" +this.gender);
21.​ System.out.println("Address:" +this.address);
22.​}
23.​void NextPerson(int age,String address){
24.​ this.age=age;
25.​ this.address=address;
26.​}
27.​void NextPerson(String name ,String gender){
28.​ this.name=name;
29.​ this.gender=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.​ p1.personDisplay();
37.​ System.out.println();
38.​ p2.personDisplay();
39.​
40.​ System.out.println();
41.​
42.​ p1.NextPerson(25,"Dhanmondi");
43.​ p2.NextPerson("Ashrafi","Male");
44.​
45.​ p1.personDisplay();
46.​ System.out.println();
47.​ p2.personDisplay();
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.​ this.name=name;
9.​ this.id=id;
10.​ this.salary=salary;
11.​ this.designation=designation;
12.​ }
13.​ Employee(String name,int id,int salary){
14.​ this(name,id,salary,"Engineer");
15.​ }
16.​ void employeeDisplay(){
17.​ System.out.println("Employee Information:");
18.​ System.out.println("Name:"+this.name);
19.​ System.out.println("ID:"+this.id);
20.​ System.out.println("Salary:"+this.salary);
21.​ System.out.println("Designation:"+this.designation);
22.​ }
23.​void NextEmployee(String name, int id, String designation){
24.​ this.name=name;
25.​ this.id=id;
26.​ this.designation=designation;
27.​}
28.​void NextEmployee(String name, int salary){
29.​ this.name=name;
30.​ this.salary=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.​ e1.employeeDisplay();
39.​ System.out.println();
40.​ e2.employeeDisplay();
41.​
42.​ System.out.println();
43.​
44.​ e1.NextEmployee("Alif",700, "CEO");
45.​ e2.NextEmployee("Ashrafi",35000);
46.​
47.​ e1.employeeDisplay();
48.​ System.out.println();
49.​ e2.employeeDisplay();
50.​
51.​
52.​ }
53.​}

Output:
.

You might also like