Credits

Thursday, April 28, 2011

Inheritance: Java's Power

One of the core principles and advantages of Java is the implementation of inheritance which allows programmers to write once programs or codes in the parent class and all connecting subclasses  can make use of the methods and member variables at anytime.


/*    this is creating an object of child class to access the fields and methods defined in the parent class and subclass*/   
  Transaction1 empObj = new Transaction1(myname,myposition);  
:
:
JOptionPane.showMessageDialog(null,"Payroll Period: "+mypayperiod+"\n Name: "+empObj.name+"\n Position: "+empObj.position+"\n Hours worked: "+hrsworked+"\n Hourly Rate: "+hrate+ " \n Employee's Total Deductions: "+tdeductions+" \n Employee's Net Pay: "+empObj.computenetpay(hrate,hrsworked,tdeductions));


With the presence of parent class, the child class can contain methods and member variables among others that can be unique to it since methods and member fields in the parent class can be accessed. In fact, a sub class can override the methods found in the parent class should these methods must have the same name and parameters but different  actions.

The program below illustrates the use of parent and child classes where EMPLOYEE is the parent class and TRANSACTION1  is the child class.
[+/-] show/hide



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Rose
*/

import javax.swing.*;
class Inheritance {



public static void main(String args[]) {



String myname = JOptionPane.showInputDialog("Input employee's name:");
String myposition = JOptionPane.showInputDialog("Input employee's position:");

Transaction1 empObj = new Transaction1(myname,myposition);


String mypayperiod = JOptionPane.showInputDialog("Input payroll period:");

String myhrate = JOptionPane.showInputDialog("Input employee's hourly rate:");
double hrate=Double.parseDouble(myhrate);

String myhrsworked = JOptionPane.showInputDialog("Input employee's hours worked:");
double hrsworked=Double.parseDouble(myhrsworked);

String mysss = JOptionPane.showInputDialog("Input employee's SSS:");
double sss=Double.parseDouble(mysss);

String myph = JOptionPane.showInputDialog("Input employee's Philhealth:");
double ph=Double.parseDouble(myph);

String myothers = JOptionPane.showInputDialog("Input employee's other deductions:");
double others=Double.parseDouble(myothers);

double tdeductions=empObj.computedeductions(sss,ph,others);

JOptionPane.showMessageDialog(null,"Payroll Period: "+mypayperiod+"\n Name: "+empObj.name+"\n Position: "+empObj.position+"\n Hours worked: "+hrsworked+"\n Hourly Rate: "+hrate+ " \n Employee's Total Deductions: "+tdeductions+" \n Employee's Net Pay: "+empObj.computenetpay(hrate,hrsworked,tdeductions));
}

}
class Employee{
protected String name;
protected String position;


String getName()
{ return name; }


String getPosition()
{ return position; }

}

class Transaction1 extends Employee {

Transaction1(String yname, String yposition){// passing parameters to parent
super.name = yname;
super.position = yposition;

}
double computedeductions(double mysss, double myph, double myothersded)
{ double tdeduct= mysss+myph+myothersded;
return tdeduct;
}

double computenetpay(double hr, double hw, double td)
{ double netpay= (hr*hw) - td;
return netpay;
}

}






AddThis