-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
97 lines (78 loc) · 2.53 KB
/
MainClass.java
File metadata and controls
97 lines (78 loc) · 2.53 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package oops;
public class MainClass {
public static void main(String[] args) {
// System.out.println("Hello World");
//
// Person p2 = new Person();
// p2.name = "Soumili Kar";
// p2.age = 21;
// p2.gender = 'F';
//
// p2.details();
// p2.eat();
Person p1 = new Person("Rahul Dey",21,'M');
p1.details();
p1.eat();
p1.walk();
p1.walk(4);
Developer d1 = new Developer("Anu Sharma",24,'M',"Software Engineer","Freshers","Flipkart pvt. ltd.");
d1.jobProfile();
d1.walk();
d1.doWork();
// System.out.println(Person.count + " objects created.");
}
}
// inheritance
class Developer extends Person{
String jobRoll;
String workExperience;
String companyName;
public Developer (String name,int age,char gender,String jobRoll,String workExperience,String companyName){
super(name, age, gender);
this.jobRoll = jobRoll;
this.workExperience = workExperience;
this.companyName = companyName;
}
public void jobProfile(){
details();
System.out.println("Job Roll : "+this.jobRoll+"\nWork Experience : "+this.workExperience+"\nCompany Name : "+this.companyName);
}
// run time Polymorphism [ at run time it is decides which walk() called.]
public void walk(){
System.out.println(this.name +" ("+ this.jobRoll+" at "+this.companyName + ") is walking.");
}
public void doWork(){
System.out.println(this.name + " is working at the position "+ this.jobRoll + " at "+this.companyName);
}
}
class Person {
String name;
int age;
char gender;
static int count = 0;
// default Constructor
// public Person(){
// count++;
// System.out.println("Object created.");
// }
// Constructor
public Person(String name,int age,char gender){
// this(); // call previous constructor
this.name = name;
this.age = age;
this.gender = gender;
}
void details(){
System.out.println("Name : "+this.name+"\nAge : "+this.age+"\nGender : "+this.gender);
}
void eat(){
System.out.println(this.name + " is eating.");
}
void walk(){
System.out.println(this.name + " is walking.");
}
// compile time Polymorphism [we can use walk or walk(steps) both for same task , the compiler decides at the time of compilation which is used.]
void walk(int Steps){
System.out.println(this.name + " walked " + Steps +" steps.");
}
}