In: Computer Science
Java Discussion Problem:
Your responses must be at least 1 paragraph long.
Explain why is it possible to have more than one constructor with the same name and different arguments. Post Programming Example.
Describe the basic components of a class. Provide at least one concrete programming example in your response.
Example:
public class Person {
private String name;
private int age;
public Person() {
name="";
age=0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Constructor:
It is same name as class name. It will execute when ever we create an object for that class. Constructors are used initialize the values of the instance variables
We can have more than 1 constructor with different type order,number, type arguments so that compiler can match the calls by differentiating the order and type
Basic component of class:
Attributes and methods and constructors
Note : If you like my answer please rate and help me it is very Imp for me