Skip to main content

Java - Classes and Objects Example


Employee.java

//Employee Class (Base class)
public class Employee {

 int Empno;
 String name;
 String telephone;
 double basicsalar;
 double othrs;
 double otrate;
 double netSal;

 public double calcNetSalary(){
  return basicsalar + othrs * otrate;

 }

 public void displayNetSalary() {
  netSal = basicsalar + othrs * otrate;
  System.out.println("Net Salary: "+netSal);


 }

 //Set name
 public void setName(String name) {
  this.name =  name;
 }

 //Set telephone
 public void setTelephone(String telephone) {
  this.telephone =  telephone;
 }

 //Set Empno
 public void setEmpno(int Empno) {
  this.Empno =  Empno;
 }

 //Set OT hours
 public void setOthrs(double othrs) {
  this.othrs =  othrs;
 } 

 //Set OT Rate
 public void setOtrate(double otrate) {
    this.otrate =  otrate;
 }

 //Set OT Rate
 public void setBasicsalar(double basicsalar) {

    this.basicsalar =  basicsalar;

 }

}


Manager.java

//Manager Class public class Manager extends Employee { double allowances; double deductions; public double calcNetSalary() { return basicsalar + allowances - deductions; } public void displayNetSalary() { netSal = basicsalar + allowances - deductions; System.out.println("Net Salary : " + netSal); } //Set allowances public void setAllowances(double allowances) { this.allowances = allowances; } //Set name public void setName(double deductions) { this.deductions = deductions; } }

Director.java


//Director class
public class Director extends Manager{
 double bonus;

 double entertainmentAllowance;
 public double calcNetSalary() {
  return basicsalar + allowances - deductions + bonus + entertainmentAllowance;
 }

 

 public void displayNetSalary() {
  netSal = basicsalar + allowances - deductions + bonus + entertainmentAllowance;
  System.out.println("Net Salary : " + netSal);

 }

  //Set bonus
  public void setBonus(double bonus ) {
   this.bonus =  bonus;

  }

  //Set entertainmentAllowance
  public void setEntertainmentAllowance(double entertainmentAllowance) {
   this.entertainmentAllowance =  entertainmentAllowance;

  }

}

Now we can create the main functionalities to access those classes. Then we can create and initialize objects. Following is an example main method. It uses the classes created above.

Main.java

//Main public class Main { public static void main(String[] args) { /*-------Creating objects---*/ Employee emp1 = new Employee();//Employee 1 Manager m1 = new Manager(); // Manager 1 Director d1 = new Director();// Director 1 Employee emp2 = new Employee();//Employee 2 Manager m2 = new Manager();// Manager 2 Director d2 = new Director();// Director 2 /*------ setting values for Employee 1----*/ emp1.setName("Amal the Employee"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(5); emp1.setOtrate(500.00); emp1.setBasicsalar(10000.00); /*------ setting values for Employee 2----*/ emp2.setName("Kamal the Employee"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(3); emp1.setOtrate(500.00); emp1.setBasicsalar(10000.00); /*------ setting values for Manager 1----*/ m1.setName("Nimal the Manager"); emp1.setTelephone("0786141343"); emp1.setEmpno(0112); emp1.setOthrs(7); emp1.setOtrate(530.00); emp1.setBasicsalar(10400.00); /*------ setting values for Manager 2----*/ m2.setName("Sunil the Manager"); emp1.setTelephone("0783424343"); emp1.setEmpno(0115); emp1.setOthrs(3); emp1.setOtrate(300.00); emp1.setBasicsalar(10500.00); /*------ setting values for Director 1----*/ d1.setName("Namal the Directoe"); emp1.setTelephone("0774641343"); emp1.setEmpno(0122); emp1.setOthrs(9); emp1.setOtrate(460.00); emp1.setBasicsalar(130000.00); /*------ setting values for Director 2----*/ d2.setName("Imal the Director"); emp1.setTelephone("0707141343"); emp1.setEmpno(0132); emp1.setOthrs(0); emp1.setOtrate(870.00); emp1.setBasicsalar(140000.00); System.out.println(""); } }

Comments

Popular posts from this blog

Introduction to ER Diagrams

In this tutorial We will be discussing following concepts in ER modeling. 

ICT Grade 10 - 1st Term Test Model Paper 1

Grade 10 ICT - 1st Term Test MCQ Model Paper 2018 sample Click Here to view  in Google Drive

Happy New Year SMS Greetings - අලූත් අවුරුද්දට සුබ පතන්න නියම SMS මෙන්න.

සිංහල  SMS suba aluth awruddak wewa @=-=-=-=-= @     )) *Happy* ))   // * New *  //  ((  * Year*  ((  @=-=-=-=-= @ siyalu petum ituwana subama suba aluth awruddak wewa....!!!    Aluth bara balaporoththu Aluth balaporoththu.. Ahurak aran.. Aluth irak payanne.. Aluth Wasarak aran.. Me SMS eken mama.. Suba nawa wasarak.. Pathami obasemata mama.. Subama Subama Suba Aluth Awuruddak Wewa Me Labana Wu Alut Awurudda Kiriyen!!!! Paniyen Sathutin Piri… Jaramara!!!! Barabara Karadara Neti Adaren!!!! Samayen Karunawen Piri Subama Subama Suba Aluth AwuruddakWewa Oba Semeta oyage jiwitheta aye mama enne ne Mata therenawa oyagey magey athara thibba serama ewara wenna lagay kiyala Oyata mawa, mata oyawa one unath mata yannama wenawa oyage jiwitheta aye mama enne ne sudu Mama oyagema wishwasaniya Parana awurudda Suba aluth awuruddak wewa !! Suba Aluth awruddak wewa ! Labannawu nawa wasara kiriyen peniyen ...