Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008, 09:03 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Subclass definition
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclass vs inner class bugger New To Java 1 01-13-2008 08:31 PM
which class is superclass and subclass? java_fun2007 New To Java 0 12-11-2007 09:55 PM
SubClass problems ravian New To Java 1 11-19-2007 06:54 PM
Eclipse - jumping to method definition Java Tip Java Tips 0 11-07-2007 03:52 PM
Error: no class definition found toby New To Java 4 07-27-2007 08:01 PM


All times are GMT +2. The time now is 07:48 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org