Advertisement
wingman007

Java2014_Animals

Dec 15th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package animals;
  8.  
  9. /**
  10.  *
  11.  * @author fmi
  12.  */
  13. public class Animals {
  14.  
  15.     public interface Calculatable {
  16.         double calculateFactorial(double x);
  17.     }
  18.    
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) {
  23.         // TODO code application logic here
  24.         Animal[] myAnimals = new Animal[5];
  25.         // upcasting
  26.         Animal fluffy = new Cat();
  27.         Animal sharo = new Dog();            
  28.        
  29.         // Cat fluffyStatic = new Cat();
  30.        // Cat.makeNoise();
  31.         // Animal.makeNoise();
  32.        
  33.         // downcasting back to the original type.
  34.         Cat fluffy2 = (Cat)fluffy;
  35.         fluffy2.doOnlyForCat();
  36.         ((Cat) fluffy).doOnlyForCat();
  37.        
  38.         myAnimals[0] = fluffy;
  39.         myAnimals[1] = sharo;
  40.        
  41.         myAnimals[0].makeNoise();
  42.         myAnimals[1].makeNoise();
  43.        
  44.         Dog sharo1 = new Dog();
  45.         System.out.println(sharo1.favoriteFood);
  46.        
  47.        
  48.         double price;
  49.        
  50.         Person stoyan = new Person(50, "Stoyan");
  51.         stoyan.introduceYourSelf();
  52.        
  53.         class LocalClass {
  54.             public String test;
  55.         }
  56.        
  57.         LocalClass myLocalClass = new  LocalClass();
  58.        
  59.         Object myAnonymous = new Object(){
  60.             @Override
  61.             public String toString() {
  62.                 return "{Anonymous" + '}';
  63.             }          
  64.         };
  65.         System.out.println(myAnonymous.toString());
  66.        
  67.         //Calculatable myCalculation = (x) -> return x*x*x;
  68.         // or
  69.         Calculatable myAnonymous1 = new Calculatable() {
  70.             @Override
  71.             public double calculateFactorial(double x) {
  72.                return x;
  73.             }    
  74.         };
  75.     }
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement