We have studded about constructor in the previous lecture
and now we will take it in an advance level. We will talk about how many
constructors we can make and how we can use them in different way to do
different tasks.
We Can make constructor with different arguments and put
different code in it so we can use it differently
_____________________________________________________________________________________
package studywebsite;
public class human
{
public int age;
public String
name;
public human() {
this.age=0;
this.name="";
}
public human(int age) {
this();
this.age =
age;
}
public human(int
age, String name) {
this.age =
age;
this.name =
name;
}
public human(int
age, String name,float a) {
this.age = 1 +
age;
this.name =
name;
}
public void age()
{
System.out.println("AGE is "+this.age);
}
public static void
main(String[] args)
{
human obj=new
human();
obj.age();
}
}
_____________________________________________________________________________________
In this following code there are four constructors these
four are different from each other
For this constructor
public human() {
this.age=0;
this.name="";
}
There are no arguments and the values are initialized as 0
and “”.
For other there are different arguments and different values
assigned them.
Hence, we can make
as much as constructor we want according to our need.
0 Comments
Your Comment is Submitted