Created
October 12, 2016 19:45
-
-
Save skiabox/ad1ef39d53fef9c5e9a6d81c4a691e86 to your computer and use it in GitHub Desktop.
Main class
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
package com.skiabox.java_apps2; | |
/** | |
* Created by administrator on 10/10/2016. | |
*/ | |
public class App2 { | |
public static void main(String[] args) { | |
//Separator | |
System.out.println("Inheritance in action"); | |
System.out.println("------------------------------------------------------"); | |
Animal w = new Wolf(); | |
w.makeNoise(); //method is called from Wolf class | |
w.roam(); //method is called from Canine class | |
w.eat(); //method is called from Wolf class | |
w.sleep(); //method is called from Animal class | |
System.out.println(); | |
//Separator | |
System.out.println("Polymorphism in action"); | |
System.out.println("------------------------------------------------------"); | |
//Now let's create an array of animals to see polymorphism in action | |
Animal[] animals = new Animal[5]; | |
animals[0] = new Dog(); | |
animals[1] = new Cat(); | |
animals[2] = new Wolf(); //remember that Wolf has its own roam method | |
animals[3] = new Hippo(); | |
animals[4] = new Lion(); | |
for (int i = 0; i < animals.length; i++) | |
{ | |
animals[i].eat(); | |
animals[i].roam(); | |
} | |
System.out.println(); | |
//Separator | |
System.out.println("Polymorphic arguments"); | |
System.out.println("------------------------------------------------------"); | |
//We can have polymorphic arguments | |
Vet newVet = new Vet(); | |
newVet.giveShot(w); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment