Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SuperClass - Need to simplify program - Suggestions Please

Status
Not open for further replies.

NewToJava

Programmer
May 14, 2000
4
US
I am new to Java and have been working on an Employee/Salary program.&nbsp;&nbsp;The program runs fine, however I would like to clean it up.&nbsp;&nbsp;I was told that I should try to create a relationship with classes utilizing Polymorphism.&nbsp;&nbsp;Unfortunately, I have not been able to grasp the functionality of it.&nbsp;&nbsp;I am pasteing the portion of the program I am trying to cleanup and would welcome any suggestions.&nbsp;&nbsp;I am especially interest in a simplified explaination of polymorphism.<br><br>This program when put together with the other works fine, except it does not display salary and should and I need to clean up from //create boss.&nbsp;&nbsp;<br><br>public abstract class Employee {<br>&nbsp;&nbsp;&nbsp;private String firstName;<br>&nbsp;&nbsp;&nbsp;private String lastName;<br>&nbsp;&nbsp;&nbsp;private Date Birthdate;<br>&nbsp;&nbsp;&nbsp;private int DepartmentCode;<br><br>&nbsp;&nbsp;&nbsp;// Constructor<br>&nbsp;&nbsp;&nbsp;public Employee( String first, String last, int m, int d, int y, int dept )<br><br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Birthdate = new Date(m,d,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstName = first;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastName = last;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DepartmentCode = dept;<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;// Return the first name<br>&nbsp;&nbsp;&nbsp;public String getFirstName() { return firstName; }<br><br>&nbsp;&nbsp;&nbsp;// Return the last name<br>&nbsp;&nbsp;&nbsp;public String getLastName() { return lastName; }<br><br>&nbsp;&nbsp;&nbsp;// Return the Birthdate<br>&nbsp;&nbsp;&nbsp;public String toString()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ return firstName + ' ' + lastName + ' ' + Birthdate.toString(); }<br><br>&nbsp;&nbsp;&nbsp;public int getBirthmonth()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ return Birthdate.getmonth(); }<br><br>&nbsp;&nbsp;&nbsp;// Abstract method that must be implemented for each<br>&nbsp;&nbsp;&nbsp;// derived class of Employee from which objects<br>&nbsp;&nbsp;&nbsp;// are instantiated.<br>&nbsp;&nbsp;&nbsp;public abstract double earnings(); <br>}<br><br>import javax.swing.JOptionPane;<br>import java.text.DecimalFormat;<br>import java.io.*;<br>import javax.swing.*;<br><br>public class ReadEmployee {<br><br>private ObjectInputStream input;<br><br>&nbsp;&nbsp;&nbsp;private void openFile()<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JFileChooser fileChooser = new JFileChooser();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileChooser.setFileSelectionMode(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JFileChooser.FILES_ONLY );<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int result = fileChooser.showSaveDialog( null );<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// user clicked Cancel button on dialog<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( result == JFileChooser.CANCEL_OPTION )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File fileName = fileChooser.getSelectedFile();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( fileName == null ¦¦<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileName.getName().equals( &quot;&quot; ) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Invalid File Name&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Invalid File Name&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open the file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input = new ObjectInputStream(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new FileInputStream( fileName ) );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch ( IOException e ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Error Opening File&quot;, &quot;Error&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;private void closeFile()<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input.close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch( IOException ex ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Error closing file&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Error&quot;, JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit( 1 );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;public Employee readRecord()<br>&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Employee inputEmployee;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputEmployee = null;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// input the values from the file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inputEmployee = (Employee) input.readObject();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch ( EOFException eofex ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;No more records in file&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;End of File&quot;, JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch ( ClassNotFoundException cnfex ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Unable to create object&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Class Not Found&quot;, JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch ( IOException ioex ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Error during read from file&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Read Error&quot;, JOptionPane.ERROR_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return inputEmployee;<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;public ReadEmployee()<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int thisMonth = 11;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// what month is it now<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// loop counter<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Employee ref;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// superclass reference<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String output = &quot;&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// container for message<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Employee arrayOfEmployees[];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// define array for objects<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayOfEmployees = new Employee[4]; // initialize array of objects<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;openFile();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//create a boss<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref = readRecord();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boss b = new Boss( ref.getLastName(), ref.getFirstName(), 0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getbirthMonth(), ref.getbirthDay(), ref.getbirthYear(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getDepartmentCode());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//put object in array<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayOfEmployees[0] = b;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//create a commission worker<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref = readRecord();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CommissionWorker c =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new CommissionWorker( ref.getLastName(), ref.getFirstName(), 0, 0, 0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getbirthMonth(), ref.getbirthDay(), ref.getbirthYear(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getDepartmentCode());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//put object in array<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayOfEmployees[1] = c;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//create a piece worker<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref = readRecord();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PieceWorker p =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new PieceWorker( ref.getLastName(), ref.getFirstName(), 0, 0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getbirthMonth(), ref.getbirthDay(), ref.getbirthYear(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getDepartmentCode());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//put object in array<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayOfEmployees[2] = p;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//create an hourly worker<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref = readRecord();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HourlyWorker h =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new HourlyWorker( ref.getLastName(), ref.getFirstName(), 0, 0,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getbirthMonth(), ref.getbirthDay(), ref.getbirthYear(),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref.getDepartmentCode());<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//put object in array<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayOfEmployees[3] = h;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//create object to format earnings<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DecimalFormat precision2 = new DecimalFormat( &quot;0.00&quot; );<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output += &quot;This month is &quot; + thisMonth + &quot;\n&quot;;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// loop over array building output for each employee<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( i = 0; i &lt; arrayOfEmployees.length; i++ ) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//get one employee from the array<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref = arrayOfEmployees[ i ];<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ask the employee for their information<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output += ref.toString() + &quot; earned $&quot; +&nbsp;&nbsp;&quot;\n &quot; +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;precision2.format( ref.earnings() ) + &quot;\n&quot; ;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//ask the employee for their birth month<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (ref.getbirthMonth() == thisMonth) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output += &quot; Happy Birthday Bonus: $100 \n&quot;;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} // for<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;closeFile();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//display results<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.showMessageDialog( null, output,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Demonstrating Polymorphism&quot;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JOptionPane.INFORMATION_MESSAGE );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit( 0 );<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;public static void main( String args[] )<br>&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;new ReadEmployee();<br><br>&nbsp;&nbsp;&nbsp;}<br><br>}<br><br><br><br><br><br>
 
The idea behind polymorphism is to be able to reuse code. It also means &quot;many faces&quot;. Basically, PM works by overriding methods from extended classes. for example:<br><br>public class Manager extends Employee{<br>&nbsp;&nbsp;&nbsp;//create the new constructor because it is not inherited<br><br>&nbsp;&nbsp;&nbsp;//In this case all the methods would remain the same except for the toString method, it needs to be overridden becuase some extra information needs to be displayed.<br><br>//add methods that are specific to the Manager class, like salary level, etc.<br><br><br>}<br><br>In this example, the Manger class extends your Employee class. The Manager &quot;is a&quot; type of employee so it should have all the methods of the employee (firstname, lastname, etc.). But managers have some additional properties such as salary, benefits, stock options, etc. These methods need to be added. Now the PM comes into play, in over ridding the toString method because different information needs to be displayed. This is a simple case in that there is only one method to over ride. This will make life simpler in most cases. When I was taking java, I created the employee and all the other classes. Believe me, once the employee was finished the rest were quite trivial to finish. Another instance you would use PM is, say if you had a task to code and you already had code that did a similar job, you could simply extend the existing class and provide it with the extra functionallity that is required to do the job. <br><br>I give you another example, I am currently writting a cd-rom cataloging program. I realized that with very minor modifications I could get it to catalog mp3's in a different manor (remembering that mp3's have much different attributes then regular files ie id3 tags).<br><br>Hope this helps, I remeber it took me awhile to get the hang of Polymorphism, inheritence and encapsulation. But you have to keep at it and do your own assignments.<br><br>&nbsp; <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top