Skip to content

Commit 0fd8a7b

Browse files
Add Exercise 6
1 parent c125fab commit 0fd8a7b

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class InheritanceDemo {
2+
public static void main(String[] args) {
3+
// Create an object of Person class
4+
PartTimeStudent ps1 = new PartTimeStudent("Lakshan", "Matara", "IT20123456", 50);
5+
6+
// Call showDetail() method
7+
ps1.showDetail();
8+
}
9+
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class PartTimeStudent extends Student{
2+
private int noOfHours;
3+
4+
//constructor
5+
public PartTimeStudent(String name, String address, String studentId, int noOfHours){
6+
super(name, address, studentId);
7+
this.noOfHours = noOfHours;
8+
}
9+
//mutator methods
10+
public void setNoOfHours(int noOfHours){
11+
this.noOfHours = noOfHours;
12+
}
13+
//accessor methods
14+
public int getNoOfHours(){
15+
return this.noOfHours;
16+
}
17+
//showDetail method
18+
public void showDetail(){
19+
System.out.println("Part-Time Student");
20+
super.showDetail();
21+
System.out.println("No. of Working Hours: " + this.noOfHours);
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Person {
2+
private String name;
3+
private String address;
4+
5+
//default constructor
6+
public Person() {
7+
this.name = "";
8+
this.address = "";
9+
}
10+
//constructor with parameters
11+
public Person(String name, String address) {
12+
this.name = name;
13+
this.address = address;
14+
}
15+
//Mutator methods
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
public void setAddress(String address) {
20+
this.address = address;
21+
}
22+
//Accessor methods
23+
public String getName() {
24+
return this.name;
25+
}
26+
public String getAddress() {
27+
return this.address;
28+
}
29+
//showDetails method
30+
public void showDetail() {
31+
System.out.println("Name: " + name);
32+
System.out.println("Address: " + address);
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Student extends Person {
2+
private String studentId;
3+
//constructor
4+
public Student(String name, String adress, String studentId) {
5+
super(name, adress);
6+
this.studentId = studentId;
7+
}
8+
//Mutator methods
9+
public void setStudentId(String studentId) {
10+
this.studentId = studentId;
11+
}
12+
//Accessor methods
13+
public String getStudentId() {
14+
return studentId;
15+
}
16+
//showDetail method
17+
public void showDetail() {
18+
System.out.println("Student Details");
19+
super.showDetail();
20+
System.out.println("Student ID: " + studentId);
21+
}
22+
}

0 commit comments

Comments
 (0)