deyanmalinov

5. Say Hello Extend

Jul 8th, 2020
839
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         List<Person> people = new ArrayList<>();
  7.         people.add(new Bulgarian("Pesho"));
  8.         people.add(new European("Pesho"));
  9.         people.add(new Chinese("Pesho"));
  10.         for (Person person : people) {
  11.             print(person);
  12.         }
  13.     }
  14.     private static void print(Person person){
  15.         System.out.println(person.sayHello());
  16.     }
  17. }-------------------------------------
  18. package DPM;
  19. public interface Person {
  20.     String getName();
  21.     String sayHello();
  22. }------------------------------------
  23. package DPM;
  24. public abstract class BasePerson implements Person{
  25.     private String name;
  26.     private String hello;
  27.     public void setName(String name){
  28.         this.name=name;
  29.     }
  30.     @Override
  31.     public String getName() {
  32.         return name;
  33.     }
  34.     @Override
  35.     public String sayHello() {
  36.         return hello;
  37.     }
  38. }-----------------------------------
  39. package DPM;
  40. public class Bulgarian extends BasePerson {
  41.     private String name;
  42.     public Bulgarian(String name){
  43.         this.name=name;
  44.     }
  45.     @Override
  46.     public void setName(String name) {
  47.         super.setName(name);
  48.     }
  49.     @Override
  50.     public String getName() {
  51.         return name;
  52.     }
  53.     @Override
  54.     public String sayHello() {
  55.         return "Здравей";
  56.     }
  57. }-------------------------------------
  58. package DPM;
  59. public class European extends BasePerson{
  60.     private String name;
  61.     public European(String name){
  62.         this.name=name;
  63.     }
  64.     @Override
  65.     public void setName(String name) {
  66.         super.setName(name);
  67.     }
  68.     @Override
  69.     public String getName() {
  70.         return name;
  71.     }
  72.     @Override
  73.     public String sayHello() {
  74.         return "Hello";
  75.     }
  76. }-------------------------------------
  77. package DPM;
  78. public class Chinese extends BasePerson{
  79.     private String name;
  80.     public Chinese(String name){
  81.         this.name=name;
  82.     }
  83.     @Override
  84.     public void setName(String name) {
  85.         super.setName(name);
  86.     }
  87.     @Override
  88.     public String getName() {
  89.         return name;
  90.     }
  91.     @Override
  92.     public String sayHello() {
  93.         return "Djydjybydjy";
  94.     }
  95. }
RAW Paste Data