Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Soundable {
- void talk();
- String say();
- }
- interface Infoable { // Dat name...
- void showInfo();
- }
- interface Special extends Infoable, Soundable { }
- //-------------------------------------------------------------------------------------------------
- abstract class Animal implements Special {
- protected String sound;
- private int weight, height;
- protected Animal(int weight, int height) {
- this.weight = weight;
- this.height = height;
- }
- @Override
- public void talk() {
- System.out.println(this.sound);
- }
- @Override
- public String say() {
- return this.sound;
- }
- public int getWeight() {
- return this.weight;
- }
- void setWeight(int weight) {
- this.weight = weight;
- }
- public int getHeight() {
- return this.height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- @Override
- public void showInfo() {
- System.out.println("Weight: " + this.weight + ", Height: " + this.height + ". " + this.sound);
- }
- }
- class Dog extends Animal {
- Dog(int weight, int height) {
- super(weight, height);
- this.sound = "Hau!";
- }
- Dog() {
- super(0, 0);
- this.sound = "Hau!";
- }
- }
- class Cat extends Dog {
- Cat(int weight, int height) {
- super(weight, height);
- this.sound = "Miau!";
- }
- Cat() {
- super(0, 0);
- this.sound = "Miau!";
- }
- }
- //-------------------------------------------------------------------------------------------------
- abstract class Vehicle implements Special {
- protected String brand, sound;
- private int power;
- protected Vehicle(int power) {
- this.power = power;
- }
- @Override
- public void talk() {
- System.out.println(this.sound);
- }
- @Override
- public String say() {
- return this.sound;
- }
- public int getPower() {
- return this.power;
- }
- public void setPower(int power) {
- this.power = power;
- }
- public void showInfo() {
- System.out.println("Brand: " + this.brand + ". Power: " + this.power + ". Sound: " + this.sound);
- }
- }
- class Car extends Vehicle {
- Car(int power) {
- super(power);
- super.sound = "Wrumm!";
- }
- Car() {
- super(0);
- super.sound = "Wrumm!";
- }
- }
- class Mercedes extends Car {
- Mercedes() {
- super(1500);
- super.sound = "Wrrr!";
- }
- }
- class Truck extends Vehicle {
- Truck(int power) {
- super(power);
- super.sound = "Hurrr!";
- }
- Truck() {
- super(0);
- super.sound = "Hurrr!";
- }
- }
- //-------------------------------------------------------------------------------------------------
- public class Program {
- public static void main(String[] args) {
- Animal anim = new Cat(7, 20);
- anim.talk();
- anim.showInfo();
- anim = new Dog(15, 30);
- tell(anim);
- Vehicle veh = new Mercedes();
- tell(veh);
- veh = new Truck(4000);
- veh.talk();
- veh.showInfo();
- }
- public static void tell(Special spec) {
- spec.showInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement