Advertisement
programajster

Z4_Polimorfizm

Mar 20th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package polimorfizm;
  2.  
  3. class Pojazd
  4. {
  5. void jedzie()
  6. {
  7. System.out.println("Pojazd jedzie!");
  8. }
  9. }
  10.  
  11. class Auto extends Pojazd
  12. {
  13. @Override
  14. void jedzie()
  15. {
  16. System.out.println("Auto jedzie!");
  17. }
  18. }
  19.  
  20. class Rower extends Pojazd
  21. {
  22.  
  23. @Override
  24. void jedzie()
  25. {
  26. System.out.println("Rower jedzie!");
  27. }
  28. }
  29.  
  30.  
  31. public class Polimorfizm {
  32.  
  33. public static void main(String[] args) {
  34.  
  35. Pojazd p1 = new Pojazd();
  36. Auto a1 = new Auto();
  37. Rower r1 = new Rower();
  38.  
  39. Pojazd p2 = a1;
  40. Pojazd p3 = r1;
  41.  
  42. p1.jedzie(); //pojazd
  43. a1.jedzie(); //auto
  44. r1.jedzie(); //rower
  45.  
  46. p1.jedzie(); //pojazd
  47. p2.jedzie(); //auto
  48. p3.jedzie(); //rower
  49.  
  50. p2 = p1;
  51. p3 = p1;
  52. p1.jedzie(); //pojazd
  53. p2.jedzie(); //pojazd
  54. p3.jedzie(); //pojazd
  55.  
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement