Advertisement
eranseg

Cats

Aug 28th, 2019
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. //-------------------- Base class - Cat --------------------------
  2. public class Cat {
  3.  
  4.     protected String name;
  5.     protected int mustLength;
  6.     protected String color;
  7.  
  8.     public Cat(String name, int mustLength, String color) {
  9.         this.name = name;
  10.         this.mustLength = mustLength;
  11.         this.color = color;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return "The cat's name is: " + this.name + ", Its mustache length is: " + this.mustLength
  17.         + ", and its color is: " + this.color + ".";
  18.     }
  19. }
  20.  
  21. //------------------------ StreetCat ------------------------------
  22.  
  23. public class StreetCat extends Cat {
  24.  
  25.     private int numOfFights;
  26.  
  27.     public StreetCat(String name, int mustLength, String color, int numOfFights) {
  28.         super(name, mustLength, color);
  29.         this.numOfFights = numOfFights;
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         return super.toString() + " In addition it participated in " + this.numOfFights + ".";
  35.     }
  36. }
  37.  
  38. //-------------------- SiamiCat -----------------------------------
  39.  
  40. public class SiamiCat extends Cat{
  41.  
  42.     private String prefFood;
  43.  
  44.     public SiamiCat(String name, int mustLength, String color, String prefFood) {
  45.         super(name, mustLength, color);
  46.         this.prefFood = prefFood;
  47.     }
  48.  
  49.     @Override
  50.     public String toString() {
  51.         return super.toString() + " In addition its preferred food is " + this.prefFood + ".";
  52.     }
  53. }
  54.  
  55. //------------------------ Main class --------------------------
  56.  
  57. public class CatTypes {
  58.     public static void main(String[] args) {
  59.         Cat c1 = new Cat("baseCat", 10, "Blue");
  60.         StreetCat c2 = new StreetCat("strCat", 15, "Yellow", 7);
  61.         SiamiCat c3 = new SiamiCat("siamCat", 12, "Green", "Milk");
  62.         System.out.println(c1.toString());
  63.         System.out.println(c2.toString());
  64.         System.out.println(c3.toString());
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement