forked from sarjus/OODP-KTU-Java-Source-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructorOverloadingDemo.java
47 lines (42 loc) · 1.38 KB
/
ConstructorOverloadingDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.sjcet.oopdemo;
class Student{
int rollNumber;
String name;
String department;
//No argument constructor
Student(){
this.department ="CSE";
}
//parameterised constructor with two argument
Student(int rollNumber, String name){
this.rollNumber = rollNumber;
this.name = name;
department = "CSE";
}
//parameterised constructor with three argument
Student(int rollNumber, String name, String department){
this.rollNumber = rollNumber;
this.name = name;
this.department = department;
}
}
public class ConstructorOverloadingDemo {
public static void main(String[] args) {
Student student1 = new Student();
//Calling Constructor with no arguments
System.out.println("Student 1 details");
System.out.println("Department : "+student1.department);
Student student2 = new Student(12,"Robin Sharma");
//calling constructor with 2 arguments
System.out.println("Student 2 details");
System.out.println("Roll Number : "+student2.rollNumber);
System.out.println("Name : "+student2.name);
//calling constructor with 3 arguments
Student student3 = new Student(13,"Praveen","ME");
System.out.println("Student 3 details");
System.out.println("Roll Number : "+student3.rollNumber);
System.out.println("Name = "+student3.name);
System.out.println("Department : "+student1.department);
System.out.println();
}
}