Created
October 13, 2016 21:14
-
-
Save skiabox/5c9321e34d840da45f07a12c4a4fb8a2 to your computer and use it in GitHub Desktop.
Inheritance_Polymorphism_Project1
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 09/10/2016. | |
*/ | |
public abstract class Animal { | |
private String picture; | |
private Food food; | |
private int hunger; | |
private int[][] boundaries; | |
private int locationX; | |
private int locationY; | |
//getters and setters | |
public String getPicture() { | |
return picture; | |
} | |
public void setPicture(String picture) { | |
this.picture = picture; | |
} | |
public Food getFood() { | |
return food; | |
} | |
public void setFood(Food food) { | |
this.food = food; | |
} | |
public int getHunger() { | |
return hunger; | |
} | |
public void setHunger(int hunger) { | |
this.hunger = hunger; | |
} | |
public int[][] getBoundaries() { | |
return boundaries; | |
} | |
public void setBoundaries(int[][] boundaries) { | |
this.boundaries = boundaries; | |
} | |
public int getLocationX() { | |
return locationX; | |
} | |
public void setLocationX(int locationX) { | |
this.locationX = locationX; | |
} | |
public int getLocationY() { | |
return locationY; | |
} | |
public void setLocationY(int locationY) { | |
this.locationY = locationY; | |
} | |
//common methods | |
public void sleep() | |
{ | |
System.out.println("The animal sleeps"); | |
} | |
//methods that must be implemented below the hierarchy | |
public void roam() | |
{ | |
System.out.println("The animal is roaming"); | |
} | |
public abstract void makeNoise(); | |
public abstract void eat(); | |
} |
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); | |
} | |
} |
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 abstract class Canine extends Animal{ | |
public void roam() | |
{ | |
System.out.println("The Canines move in packs"); | |
} | |
} |
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 Cat extends Feline{ | |
@Override | |
public void makeNoise() { | |
System.out.println("The cat meows"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The cat eats " + Food.GRASS); | |
} | |
} |
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 Dog extends Canine{ | |
@Override | |
public void makeNoise() { | |
System.out.println("The dog barks"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The dog is eating " + Food.GRASS); | |
} | |
} |
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 09/10/2016. | |
*/ | |
public abstract class Feline extends Animal{ | |
//common Feline methods | |
public void roam() { | |
System.out.println("The Felines avoid each other"); | |
} | |
} |
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 09/10/2016. | |
*/ | |
public enum Food { | |
MEAT, | |
GRASS | |
} |
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 Hippo extends Animal{ | |
@Override | |
public void makeNoise() { | |
System.out.println("The hippo snarls"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The hippo is eating " + Food.GRASS); | |
} | |
} |
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 09/10/2016. | |
*/ | |
public class Lion extends Feline{ | |
//Lion methods | |
@Override | |
public void makeNoise() { | |
System.out.println("The lion is growling"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The lion is eating " + Food.MEAT); | |
} | |
} |
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 Tiger extends Feline{ | |
@Override | |
public void makeNoise() { | |
System.out.println("The tiger is growling"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The tiger is eating" + Food.MEAT); | |
} | |
} |
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 Vet { | |
public void giveShot(Animal a) | |
{ | |
System.out.println("The vet is giving a shot to " + a.getClass().getSimpleName()); | |
//The animal reacts to the shot | |
a.makeNoise(); | |
} | |
} |
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 Wolf extends Canine{ | |
@Override | |
public void makeNoise() { | |
System.out.println("The wolf howls"); | |
} | |
@Override | |
public void eat() { | |
System.out.println("The wolf is eating " + Food.MEAT); | |
} | |
//We can override a method from a superclass and at the same time call it like this | |
public void roam() | |
{ | |
super.roam(); | |
System.out.println(", and now that it is a wolf it is very dangerous for its prey"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment