Advertisement
Radfler

polymorphism test

Jun 21st, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. interface Soundable {
  2.    
  3.     void talk();
  4.     String say();
  5. }
  6.  
  7. interface Infoable { // Dat name...
  8.    
  9.     void showInfo();
  10. }
  11.  
  12. interface Special extends Infoable, Soundable { }
  13.  
  14. //-------------------------------------------------------------------------------------------------
  15.  
  16. abstract class Animal implements Special {
  17.  
  18.     protected String sound;
  19.     private int weight, height;
  20.    
  21.     protected Animal(int weight, int height) {
  22.         this.weight = weight;
  23.         this.height = height;
  24.     }
  25.    
  26.     @Override
  27.     public void talk() {
  28.         System.out.println(this.sound);
  29.     }
  30.    
  31.     @Override
  32.     public String say() {
  33.         return this.sound;
  34.     }
  35.    
  36.     public int getWeight() {
  37.         return this.weight;
  38.     }
  39.    
  40.     void setWeight(int weight) {
  41.         this.weight = weight;
  42.     }
  43.    
  44.     public int getHeight() {
  45.         return this.height;
  46.     }
  47.    
  48.     public void setHeight(int height) {
  49.         this.height = height;
  50.     }
  51.    
  52.     @Override
  53.     public void showInfo() {
  54.         System.out.println("Weight: " + this.weight + ", Height: " + this.height + ". " + this.sound);
  55.     }
  56. }
  57.  
  58. class Dog extends Animal {
  59.    
  60.     Dog(int weight, int height) {
  61.         super(weight, height);
  62.         this.sound = "Hau!";
  63.     }
  64.    
  65.     Dog() {
  66.        
  67.         super(0, 0);
  68.         this.sound = "Hau!";
  69.     }
  70. }
  71.  
  72. class Cat extends Dog {
  73.    
  74.     Cat(int weight, int height) {
  75.         super(weight, height);
  76.         this.sound = "Miau!";
  77.     }
  78.    
  79.     Cat() {
  80.        
  81.         super(0, 0);
  82.         this.sound = "Miau!";
  83.     }
  84. }
  85.  
  86. //-------------------------------------------------------------------------------------------------
  87.  
  88. abstract class Vehicle implements Special {
  89.  
  90.     protected String brand, sound;
  91.     private int power;
  92.    
  93.     protected Vehicle(int power) {
  94.         this.power = power;
  95.     }
  96.    
  97.     @Override
  98.     public void talk() {
  99.         System.out.println(this.sound);
  100.     }
  101.  
  102.     @Override
  103.     public String say() {
  104.         return this.sound;
  105.     }
  106.    
  107.     public int getPower() {
  108.         return this.power;
  109.     }
  110.    
  111.     public void setPower(int power) {
  112.         this.power = power;
  113.     }
  114.    
  115.     public void showInfo() {
  116.         System.out.println("Brand: " + this.brand + ". Power: " + this.power + ". Sound: " + this.sound);
  117.     }
  118. }
  119.  
  120. class Car extends Vehicle {
  121.  
  122.     Car(int power) {
  123.         super(power);
  124.         super.sound = "Wrumm!";
  125.     }
  126.    
  127.     Car() {
  128.         super(0);
  129.         super.sound = "Wrumm!";
  130.     }
  131. }
  132.  
  133. class Mercedes extends Car {
  134.    
  135.     Mercedes() {
  136.         super(1500);
  137.         super.sound = "Wrrr!";
  138.     }
  139. }
  140.  
  141. class Truck extends Vehicle {
  142.  
  143.     Truck(int power) {
  144.         super(power);
  145.         super.sound = "Hurrr!";
  146.     }
  147.    
  148.     Truck() {
  149.         super(0);
  150.         super.sound = "Hurrr!";
  151.     }
  152. }
  153.  
  154. //-------------------------------------------------------------------------------------------------
  155.  
  156. public class Program {
  157.  
  158.     public static void main(String[] args) {
  159.        
  160.         Animal anim = new Cat(7, 20);
  161.        
  162.         anim.talk();
  163.         anim.showInfo();
  164.        
  165.         anim = new Dog(15, 30);
  166.         tell(anim);
  167.        
  168.         Vehicle veh = new Mercedes();
  169.         tell(veh);
  170.        
  171.         veh = new Truck(4000);
  172.         veh.talk();
  173.         veh.showInfo();
  174.     }
  175.    
  176.     public static void tell(Special spec) {
  177.         spec.showInfo();       
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement