-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TharinduDilshan/Tharindu
Added Abstraction and Encapsulation
- Loading branch information
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package Inheritance; | ||
|
||
public abstract class Person { | ||
private String firstName; | ||
private String lastName; | ||
private String address; | ||
|
||
public Person() { | ||
super(); | ||
} | ||
|
||
public Person(String firstName, String lastName, String address) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.address = address; | ||
} | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public void setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public void setLastName(String lastName) { | ||
this.lastName = lastName; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
public abstract String print(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package Inheritance; | ||
|
||
public class Student extends Person { | ||
private String ownerName; | ||
private boolean isAtHostel; | ||
|
||
public Student(String ownerName, boolean isAtHostel) { | ||
this.ownerName = ownerName; | ||
this.isAtHostel = isAtHostel; | ||
} | ||
|
||
public Student(String firstName, String lastName, String address, String ownerName, boolean isAtHostel) { | ||
super(firstName, lastName, address); | ||
this.ownerName = ownerName; | ||
this.isAtHostel = isAtHostel; | ||
} | ||
|
||
public String getOwnerName() { | ||
return ownerName; | ||
} | ||
|
||
public void setOwnerName(String ownerName) { | ||
this.ownerName = ownerName; | ||
} | ||
|
||
public boolean isAtHostel() { | ||
return isAtHostel; | ||
} | ||
|
||
public void setAtHostel(boolean atHostel) { | ||
isAtHostel = atHostel; | ||
} | ||
|
||
@Override | ||
public String print() { | ||
return "Dear Teacher, my father is "+ ownerName+"."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package Inheritance; | ||
|
||
public class Teacher extends Person { | ||
private String subject; | ||
|
||
public Teacher(String subject) { | ||
this.subject = subject; | ||
} | ||
|
||
public Teacher(String firstName, String lastName, String address, String subject) { | ||
super(firstName, lastName, address); | ||
this.subject = subject; | ||
} | ||
|
||
public String getSubject() { | ||
return subject; | ||
} | ||
|
||
public void setSubject(String subject) { | ||
this.subject = subject; | ||
} | ||
|
||
@Override | ||
public String print() { | ||
return "Dear Students, I am your "+ subject+ " teacher."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package Inheritance; | ||
|
||
public class Test { | ||
public void main(String[] args){ | ||
Person teacher1 = new Teacher("John", "Tarbet", "Sri Lanka", "Science"); | ||
Person teacher2 = new Teacher("Ann", "Tarbet", "Australia", "Maths"); | ||
|
||
Person student1 = new Student("Nimal", "Jackson", "Sri Lanka", "Mason", true); | ||
Person student2 = new Student("Kamal", "Jackson", "Australia", "Avery", false); | ||
|
||
System.out.println(teacher1.print()); | ||
System.out.println(teacher2.print()); | ||
System.out.println(student1.print()); | ||
System.out.println(student2.print()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package Encapsulation; | ||
|
||
public class Student{ | ||
//private data member | ||
private String name; | ||
|
||
public Student() { | ||
super(); | ||
} | ||
|
||
public Student(String name) { | ||
this.name = name; | ||
} | ||
|
||
//getter method for name | ||
public String getName(){ | ||
return name; | ||
} | ||
//setter method for name | ||
public void setName(String name){ | ||
this.name=name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package Encapsulation; | ||
|
||
public class Test{ | ||
public static void main(String[] args){ | ||
//creating instance of the encapsulated class | ||
Student s=new Student(); | ||
//setting value in the name member | ||
s.setName("Tharindu"); | ||
//getting value of the name member | ||
System.out.println(s.getName()); | ||
} | ||
} |