Problem statement:
You are given a code for constructor overloading. What do you think, can we overload the constructor in java?
Ans: Yes.
You are given a code for constructor overloading. What do you think, can we overload the constructor in java?
Ans: Yes.
- class A {
- A() {
- System.out.println("default");
- }
- A(int id) {
- System.out.println("id");
- }
- A(int id, String name) {
- System.out.println("id & name");
- }
- A(String name, Double salary) {
- System.out.println("name & salary");
- }
- }
- public class Sample {
- public static void main(String[] args) {
- A a = new A();
- }
- }
Output:
default
No comments:
Post a Comment