java - Can different type of objects(sub-class types) added on a List (if type of List is their common superclass)? -


Recently, I saw similar articles about various objects on the list (Please see the duplicate form Do not mark in)

It has been mentioned in the article that for these items a general class has to be implemented.

However, in the following sections of the code, the classes of the above items are classified as employees
not applicable Use> and I wonder if it can work like that

  import java.util *; Public Sector Main {Public Stable Zero Main (String [] Args) {SalariedEmployee emp1 = New SalariedEmployee (); Hourly staffing staff = new hourly staff (); Emp1.setName ("Jack Smith"); Emp1.setAm ("72154f"); Emp1.setSalary (3000); Emp2.setName ("John Smith"); emp2.setAm ("13521a"); emp2.setHoursWorked (300); emp2.setHourlyPayment (4); & Lt; Employee & gt; Emp = new arreelist & lt; Employee & gt; (); emp.add (emp1); Emp.add (emp2); (For Employee Item: AMP) {System.out.println ("Employee ID:" + item.getID ()); System.out.println ("employee's name:" + item.getName ()); System.out.println ("Employee AM:" + item.getAm ()); System.out.println ("Employee Payment:" + Item.payment ()); }    

You want to create a list of employees, which you have and Will work.

  list & lt; Employee & gt; Employees = new arrivals & lt; & Gt; ();   

Instead of directly declaring you directly list & lt; Employee & gt; must be bound by:

  Public Zero DoSomething (list & lt; employee expansion & gt; list) {...} public zero doSomethingElse (list & lt ; Employee & gt; list) {...}   

Assume that we have two lists, from the list & lt; Employee & gt; Employee and list & lt; SalariedEmployee & gt; Employment will be the result of calling these methods:

  doSomething (employees); // Penaltetting (semi-contracted); // fine dosomethingElse (employees); // Dor Dosoming Else (paramilitary); // Error!   

By not compelling yourself, instead of all your sub-squares, the objects are strictly reducing employee type.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -