filmov
tv
Constructor Overloading in Java | Java for Beginners

Показать описание
Constructor Overloading in Java | Java for Beginners
About this Video:
In this video,I have explained about Constructor Overloading in simple way with real life scinario.
Links:
Java Constructor:
Java class and Object:
Java Methods:
Java variable:
Declaration and
Initialization of Java Variables:
Why do we need programming language?
Instance Block:
Notes:
1.What is Constructor Overloading?
2.Purpose of Constructor Overloading.
In the last tutorial, we have learnt about Instance
Initialization Block.In this tutorial we are going to learn about Constructor Overloading.
Basically, Constructor is used for objects initialization that means, we can give values to an object's fields using constructor in java.
So question comes that what is the role of Constructor Overloading in java..
So let's understand with real life example.
Now, let's imagine, if you have a cake Shop, first customer came into your shop and order for banana cake of 1 kg
So here cake has two properties or we can say fields,name of cake and weight of cake.
Customer 1
Name : banana cake
Weight:1 kg
After sometime, 2nd customer came into your shop and order for chocolate cake of 2 kg with rectangle shape,so here cake has 3 fields name of cake, weight of cake and shape of cake
Customer 2
Name: chocolate cake
Weight: 1 kg
Shape: square
And then later, 3rd customer came into your shop and order for circle shape strawberry cake of 2 kg with mango flavour,so here,Cake has 4 fields
Name:strawberry cake
Weight:2 kg
Shape: circle
Flavour:Mango
So here,all are cake but each cake has different fields and due to different fields each cake has different structure or we can say different form.
So basically,we can create same object in different form
Similarly,In java we can create different forms of same object and it is possible only because of Constructor overloading.
Now, let's think as a programming point of view,
Suppose,
You have a software company and thousands of employees are working there and you want to keep record of all those employees.
Suppose,
For Emp1,
You decided to add two fields.
EmpName;EmpId;
But later,you want to add one more field and it's designation of some of those employees..
So here,
Emp2 has three fields
EmpName;EmpID and Designation;
But again in future, you decided to add one more field and it's address of some of those employees sothat you can provide them pickup and drop service
So here,
Emp3 has two fields and it's
EmpName;
EmpAddr;
So here
Emp1 has different fields
Emp2 has different fields and emp3 has also different fields
And due to different fields each employee has different form..
Now, we need to initialize emp1, emp2 and emp3 by using constructor bcoz constructor is used to initialize an object
So here,
To initialize,emp1 object, we need to create constructor with parameters EmpName and EmpID
Emp(String empName,int empID){
}
And to initialize emp2 object, we need to create another constructor with parameters EmpName, EmpID and
Designation of employee
Emp(String empName,int empID,String Designation){
}
To initialize emp3 object,we need one more constructor with parameters EmpName and address of employees
Emp(String empName, String empAddr){
}
So here we have to create total 3 constructor in a class one for emp1 object another for emp2 object and one for emp3 and class having more than one constructor with different fields/parameters
is nothing but the Constructor Overloading in Java.
1.What is Constructor Overloading?
Constructor overloading is a technique in which class can have more than one constructor with different parameter list.
2.Purposes of Constructor Overloading
2.1 It provides flexibility to create different forms of same object.
2.2 It provide Backward/Forward Compatibility.
Clear explanation about purposes has been given in this video.
#javaconstructoroverloading #constructoroverloadinginjava #java #sgtutorial
About this Video:
In this video,I have explained about Constructor Overloading in simple way with real life scinario.
Links:
Java Constructor:
Java class and Object:
Java Methods:
Java variable:
Declaration and
Initialization of Java Variables:
Why do we need programming language?
Instance Block:
Notes:
1.What is Constructor Overloading?
2.Purpose of Constructor Overloading.
In the last tutorial, we have learnt about Instance
Initialization Block.In this tutorial we are going to learn about Constructor Overloading.
Basically, Constructor is used for objects initialization that means, we can give values to an object's fields using constructor in java.
So question comes that what is the role of Constructor Overloading in java..
So let's understand with real life example.
Now, let's imagine, if you have a cake Shop, first customer came into your shop and order for banana cake of 1 kg
So here cake has two properties or we can say fields,name of cake and weight of cake.
Customer 1
Name : banana cake
Weight:1 kg
After sometime, 2nd customer came into your shop and order for chocolate cake of 2 kg with rectangle shape,so here cake has 3 fields name of cake, weight of cake and shape of cake
Customer 2
Name: chocolate cake
Weight: 1 kg
Shape: square
And then later, 3rd customer came into your shop and order for circle shape strawberry cake of 2 kg with mango flavour,so here,Cake has 4 fields
Name:strawberry cake
Weight:2 kg
Shape: circle
Flavour:Mango
So here,all are cake but each cake has different fields and due to different fields each cake has different structure or we can say different form.
So basically,we can create same object in different form
Similarly,In java we can create different forms of same object and it is possible only because of Constructor overloading.
Now, let's think as a programming point of view,
Suppose,
You have a software company and thousands of employees are working there and you want to keep record of all those employees.
Suppose,
For Emp1,
You decided to add two fields.
EmpName;EmpId;
But later,you want to add one more field and it's designation of some of those employees..
So here,
Emp2 has three fields
EmpName;EmpID and Designation;
But again in future, you decided to add one more field and it's address of some of those employees sothat you can provide them pickup and drop service
So here,
Emp3 has two fields and it's
EmpName;
EmpAddr;
So here
Emp1 has different fields
Emp2 has different fields and emp3 has also different fields
And due to different fields each employee has different form..
Now, we need to initialize emp1, emp2 and emp3 by using constructor bcoz constructor is used to initialize an object
So here,
To initialize,emp1 object, we need to create constructor with parameters EmpName and EmpID
Emp(String empName,int empID){
}
And to initialize emp2 object, we need to create another constructor with parameters EmpName, EmpID and
Designation of employee
Emp(String empName,int empID,String Designation){
}
To initialize emp3 object,we need one more constructor with parameters EmpName and address of employees
Emp(String empName, String empAddr){
}
So here we have to create total 3 constructor in a class one for emp1 object another for emp2 object and one for emp3 and class having more than one constructor with different fields/parameters
is nothing but the Constructor Overloading in Java.
1.What is Constructor Overloading?
Constructor overloading is a technique in which class can have more than one constructor with different parameter list.
2.Purposes of Constructor Overloading
2.1 It provides flexibility to create different forms of same object.
2.2 It provide Backward/Forward Compatibility.
Clear explanation about purposes has been given in this video.
#javaconstructoroverloading #constructoroverloadinginjava #java #sgtutorial
Комментарии