fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7.  
  8. class Product
  9. {
  10. public void buy(Market m)
  11. {
  12. System.out.println("Search a product in market");
  13. }
  14.  
  15. public void buy(Shop s)
  16. {
  17. System.out.println("Search a product in shop");
  18. }
  19.  
  20. }
  21.  
  22. class Mobile extends Product
  23. {
  24. public void buy(Market m)
  25. {
  26. System.out.println("Search a mobile in market");
  27. }
  28.  
  29. public void buy(Shop s)
  30. {
  31. System.out.println("Search a mobile in shop");
  32. }
  33.  
  34. }
  35.  
  36. class Market
  37. {
  38. public void buy(Product product)
  39. {
  40. product.buy (this);
  41. }
  42. }
  43.  
  44. class Shop extends Market
  45. {
  46. public void buy(Product product)
  47. {
  48. product.buy (this);
  49. }
  50. }
  51.  
  52. /* Name of the class has to be "Main" only if the class is public. */
  53. class Ideone
  54. {
  55. public static void main (String[] args) throws java.lang.Exception
  56. {
  57. System.out.println("Initial example");
  58. Market market = new Market();
  59. Market shop = new Shop();
  60.  
  61. market.buy(new Product());
  62. shop.buy(new Mobile());
  63. Product p1 = new Product();
  64. Product p2 = new Mobile();
  65.  
  66. System.out.println("New example");
  67. shop.buy(p1);
  68. shop.buy(p2);
  69. p1 = p2;
  70. shop.buy(p1);
  71.  
  72. }
  73. }
Success #stdin #stdout 0.08s 2841600KB
stdin
Standard input is empty
stdout
Initial example
Search a product in market
Search a mobile in shop
New example
Search a product in shop
Search a mobile in shop
Search a mobile in shop