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

Singlish to Sinhala Converter for PC

Singlish to Sinhala Realtime Converter ===== Less Requirements Size:  430 KB O/S : Windows xp, Windows vista, Windows 7 , 8, 8.1, 10 Ram: 256Mb Mode: Offline | Real-time application ===== No need to install. It's portable! 1. Download the software from Here! . 2. place the software anywhere you want. 3. Click on it and play! It's portable. You can copy it into your pen drive any run on any computer. ===== Learn to type in Singlish you can learn to type in singlish by referring the transliteration scheme.  Learning tutorial for singlish Click here to Download