Header Ads

Java OOP - Program 1

class Person{
private String name;
private String email;

Person(String name, String email){
this.name = name;
this.email = email;
}

public void display(){
System.out.println("Name: "+name);
System.out.println("Email: "+email);
}
}

class Student extends Person{
private int year;

Student(String name, String email, int year){
super(name, email);
this.year = year;
}

public void display(){
super.display();
System.out.println("Year: "+year);
}
}

class Employee extends Person{
private double salary;

Employee(String name, String email){
super(name, email);
}

public void setSalary(double salary){
this.salary = salary;
}

public double getSalary(){
return salary;
}

public void display(){
super.display();
System.out.println("Salary: "+salary);
}


}

class DemoPerson{
public static void main(String args[]){
Student stu = new Student("M.L.J.Kalhara", "kalhara@gmail.com", 2);
stu.display();

System.out.println("---------------------------------");

Employee emp = new Employee("P.W.Sumanasena", "Sumanasena@td.lk");
emp.setSalary(62000.00);
emp.display();
}
}

2 comments:

  1. This helps me a lot to pass my exam. Thank you sir iTech Digest!!!

    ReplyDelete

Thank you very much for your ideas!