Created
June 8, 2020 21:50
-
-
Save asolntsev/29704ebe8d8dd87f4a50c4b1cee6e6bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AnimalsTest { | |
public static void main(String[] args) { | |
Cat cat = new Cat("Tom", 8); | |
Mouse mouse = new Mouse("Jerry", 3); | |
System.out.println("Cat name is: " + cat.getName()); | |
System.out.println("Mouse name is: " + mouse.getName()); | |
System.out.println("Mouse weight is: " + mouse.weight); | |
} | |
} | |
abstract class Animal { | |
private final String name; | |
protected Animal(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
public class Cat extends Animal { | |
public final int age; | |
public Cat(String name, int age) { | |
super(name); | |
this.age = age; | |
} | |
} | |
public class Mouse extends Animal { | |
final int weight; | |
public Mouse(String name, int weight) { | |
super(name); | |
this.weight = weight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment