Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------- Base class - Cat --------------------------
- public class Cat {
- protected String name;
- protected int mustLength;
- protected String color;
- public Cat(String name, int mustLength, String color) {
- this.name = name;
- this.mustLength = mustLength;
- this.color = color;
- }
- @Override
- public String toString() {
- return "The cat's name is: " + this.name + ", Its mustache length is: " + this.mustLength
- + ", and its color is: " + this.color + ".";
- }
- }
- //------------------------ StreetCat ------------------------------
- public class StreetCat extends Cat {
- private int numOfFights;
- public StreetCat(String name, int mustLength, String color, int numOfFights) {
- super(name, mustLength, color);
- this.numOfFights = numOfFights;
- }
- @Override
- public String toString() {
- return super.toString() + " In addition it participated in " + this.numOfFights + ".";
- }
- }
- //-------------------- SiamiCat -----------------------------------
- public class SiamiCat extends Cat{
- private String prefFood;
- public SiamiCat(String name, int mustLength, String color, String prefFood) {
- super(name, mustLength, color);
- this.prefFood = prefFood;
- }
- @Override
- public String toString() {
- return super.toString() + " In addition its preferred food is " + this.prefFood + ".";
- }
- }
- //------------------------ Main class --------------------------
- public class CatTypes {
- public static void main(String[] args) {
- Cat c1 = new Cat("baseCat", 10, "Blue");
- StreetCat c2 = new StreetCat("strCat", 15, "Yellow", 7);
- SiamiCat c3 = new SiamiCat("siamCat", 12, "Green", "Milk");
- System.out.println(c1.toString());
- System.out.println(c2.toString());
- System.out.println(c3.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement