Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter list.
Aim:
Algorithm:
constructor overloading concept.
Step2: Create a base class named “over “ and initialize the employee no, name, job
and salary.
Step3: Using “this” operator we can get the instance variable and using in various
methods.
Step4: Print emp name, no, job, and salary using “System.out.println()” method.
Step5: Create a main class named “ consover” and create object x1, x2, x3 and x4.
Step6: Display the value of x1, x2, x3, and x4.
Step7: Save the program with “.java” extension.
Step8: Compile it using “javac” command and run the program using “java”
command.
Program:
{
int eno;
String ename,job;
double sal;
over()
{
eno=0;
ename=job=null;
sal=0.0;
}
over(int eno)
{
this.eno=eno;
this.ename="Ramesh";
this.job="Salesman";
this.sal=4500;
}
over(int eno,String ename)
{
this.eno=eno;
this.ename=ename;
this.job="Clerk";
this.sal=5600;
}
over(int eno,String ename,String job,double sal)
{
this.eno=eno;
this.ename=ename;
this.job=job;
this.sal=sal;
}
void Display()
{
System.out.println("Employee Number:"+eno);
System.out.println("Employee name:"+ename);
System.out.println("Job:"+job);
System.out.println("Salary:"+sal);
}
}
class consover
{
public static void main(String args[])
{
over x1=new over();
over x2=new over(101);
over x3=new over(102,"Ganesh");
over x4=new over(103,"Dinesh","programmer",6700);
x1.Display();
x2.Display();
x3.Display();
x4.Display();
}
}
OUTPUT:
Employee Number:0
Employee name:null
Job:null
Salary:0.0
Employee Number:101
Employee name:Ramesh
Job:Salesman
Salary:4500.0
Employee Number:102
Employee name:Ganesh
Job:Clerk
Salary:5600.0
Employee Number:103
Employee name:Dinesh
Job:programmer
Salary:6700.0
BY:
REGU RAM SV.
Post a Comment