Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MAIN
- class Main {
- public static void main(String[] args) {
- Dog dog = new Dog("Milo", 2, "Brown");
- Cat cat = new Cat("Whiskey", 3, "Brown");
- Duck duck = new Duck("Do", 1, "White");
- Pig pig = new Pig("Po", 10, "Pink");
- dog.printInfo();
- cat.printInfo();
- duck.printInfo();
- pig.printInfo();
- }
- }
- # ANIMAL
- public class Animal {
- private String name;
- private int age;
- private String color;
- public Animal(String name, int age, String color) {
- System.out.println("In animal constructor");
- this.name = name;
- this.age = age;
- this.color = color;
- }
- public String greetings() {
- return "Error";
- }
- public void printInfo() {
- System.out.println(getClass().getName() + " " + name + " is " + age + " years old and is of " + color + " color " + greetings());
- }
- }
- # DOG
- public class Dog extends Animal {
- public Dog(String name, int age, String color) {
- super(name, age, color);
- System.out.println("In dog constructor");
- }
- public String greetings() {
- return "Woof woof";
- }
- }
- # CAT
- public class Cat extends Animal {
- public Cat(String name, int age, String color) {
- super(name, age, color);
- System.out.println("In cat constructor");
- }
- public String greetings() {
- return "Meow";
- }
- }
- # DUCK
- public class Duck extends Animal {
- public Duck(String name, int age, String color) {
- super(name, age, color);
- System.out.println("In duck constructor");
- }
- public String greetings() {
- return "Quack";
- }
- }
- # PIG
- public class Pig extends Animal {
- public Pig(String name, int age, String color) {
- super(name, age, color);
- System.out.println("In pig constructor");
- }
- public String greetings() {
- return "Oink";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement