Java Program using Two different Classes in Two Different File

Java Program using Two different Classes in Two Different File

A Java program can contain any number of classes. Following Java program comprises of two classes: TestEmployee1 and Employee1 in two different files.  After the creation, save both the files and then compile and them in Command Prompt.

public class TestEmployee1 { public static void main(String[] args) { Employee1 alex = new Employee1(); //creating 3 objects “alex, linda and john” of the class Employee1 Employee1 linda= new Employee1(); Employee1 john= new Employee1(); alex.salary = 10000; // assigning salary to the object “alex” alex.bonus = 2000; linda.salary = 12000; linda.bonus =1000; john.salary = 8000; john.bonus = 3000; alex.calculateTotalPay(); //here calculateTotalPay() is a method defined in Employee1 class linda.calculateTotalPay(); // we r invoking that method john.calculateTotalPay(); //will print total pay of john }}

public class Employee1 { double salary; double bonus; void calculateTotalPay() { double totalPay = salary + bonus; System.out.println(“Total Pay = ” +totalPay); //concatenation used to get totalPay } }
C:Program FilesJavajdk1.8.065bin>javac Employee1.javaC:Program FilesJavajdk1.8.065bin>javac TestEmployee1.javaC:Program FilesJavajdk1.8.065bin>java TestEmployee1 Total Pay = 12000.0Total Pay = 13000.0Total Pay = 11000.0

Leave a Reply

Your email address will not be published. Required fields are marked *