Advertisement
wreed12345

Interface Purpose Example

May 28th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. Teacher t = new Teacher(): //implements person
  2. Worker w = new Worker( ); //implements person
  3. Slave s = new Slave(); //implements person
  4.  
  5. ArrayList<Person> people = new ArrayList<Person>();
  6.  
  7. private void addToList(){
  8.     people.add(t);
  9.     people.add(w);
  10.     people.add(s);
  11. }
  12.  
  13. private void printAges(){
  14.     for(People p : people){
  15.         System.out.println(people.getAge());// valid since getAge is a part of the interface rather then the specific class
  16.                             //if only slave had the getAge method this code would not be valid
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement