Results 1 to 1 of 1
Thread: Subclass definition
-
Subclass definition
Java Code:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectRefTest { public static void main(String[] args) { try { Employee[] staff = new Employee[3]; Employee harry = new Employee("Harry Sample", 35000); staff[0] = harry; staff[1] = new Manager("Carl Java", 75000, harry); staff[2] = new Manager("Tony J", 38000, harry); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("employee.dat")); out.writeObject(staff); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream( "employee.dat")); Employee[] newStaff = (Employee[]) in.readObject(); for (int i = 0; i < newStaff.length; i++) newStaff[i].raiseSalary(100); for (int i = 0; i < newStaff.length; i++) newStaff[i].print(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } class Employee implements Serializable { public Employee(String n, double s) { name = n; salary = s; } public Employee() { } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public void print() { System.out.println(name + " " + salary); } private String name; private double salary; } class Manager extends Employee { private Employee secretary; public Manager(String n, double s, Employee e) { super(n, s); secretary = e; } public Manager() { } public void raiseSalary(double byPercent) { super.raiseSalary(byPercent + 10); } public void print() { super.print(); System.out.print("Secretary: "); if (secretary != null) secretary.print(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Error: no class definition found
By toby in forum New To JavaReplies: 6Last Post: 08-28-2011, 10:32 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM -
SubClass problems
By ravian in forum New To JavaReplies: 1Last Post: 11-19-2007, 05:54 PM -
Eclipse - jumping to method definition
By Java Tip in forum Java TipReplies: 0Last Post: 11-07-2007, 02:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks