atanasovetr

Animals Abstraction

Mar 5th, 2021
652
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public abstract class Animal {
  2.     private String name;
  3.  
  4.     public Animal(String name) {
  5.         this.name = name;
  6.     }
  7.     public String getName() {
  8.         return name;
  9.     }
  10.  
  11.     public void setName(String name) {
  12.         this.name = name;
  13.     }
  14.  
  15.     public abstract void talk();
  16. }
  17.  
  18. public class Cat extends Animal{
  19.     public Cat(String name){
  20.         super(name);
  21.     }
  22.     @Override
  23.     public void talk(){
  24.         System.out.println("Meow, Meow");
  25.     }
  26. }
  27.  
  28. public class Dog extends Animal{
  29.     public Dog(String name){
  30.         super(name);
  31.     }
  32.     @Override
  33.     public void talk(){
  34.         System.out.println("Bark, bark");
  35.  
  36.     }
  37. }
  38.  
  39. public class Chicken extends Animal{
  40.     public Chicken(String name){
  41.         super(name);
  42.     }
  43.     @Override
  44.     public void talk(){
  45.         System.out.println("Kud-kud-kud");
  46.  
  47.     }
  48. }
  49.  
  50. import java.util.ArrayList;
  51.  
  52. public class Main {
  53.     public static void main(String[] args) {
  54.         ArrayList<Animal> animals = new ArrayList<>();
  55.         animals.add(new Cat("Lucy"));
  56.         animals.add(new Dog("John"));
  57.         animals.add(new Chicken("Eliza"));
  58.  
  59.         animals.forEach(animal -> {
  60.             System.out.print(animal.getName() + ": ");
  61.             animal.talk();
  62.         });
  63.     }
  64. }
  65.  
  66.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×