Polymorphism in Java

These are a skeleton class I made for myself on making polymorphic classes in Java. In a nutshell, polymorphism is creating class from a template class. This classes are often describe as parent and child classes.

Here is the Java docs on Polymorphism.
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

This Animal class is my parent class. In this case an animal is only defined by it’s name ( if it has one ) and the type of food it eats. This class is a generic class object.

package Polymorphism;

//-This Animal class is the parent class
public class Animal {
	private String name;
	private Type type;
	
	public enum Type {
		carnivore,
		omnivor,
		herbivore
	}
	
	// Default type value is omnivor
	// Default name value is null
	public Animal(){
		this.name = null;
		this.type = Type.omnivor;
	}
	
	public Animal( String name ){
		this.name = name;
		this.type = Type.omnivor;
	}
	
	public Animal( Type type ){
		this.name = null;
		this.type = type;
	}
	
	public Animal( String name, Type type ){
		this.name = name;
		this.type = type;
	}
	
	public String getName() { return this.name; }
	public Type   getType() { return this.type; }
}

Now here is the code for the child class. The key words create another animal class is extends and super. By extending the Duck class from the Animal class, the duck class inherits the contents of the animal class. You use the word “super” to pass in the parameters to the Animal constructors.

package Polymorphism;

public class Duck extends Animal {
	private final String talk = "aflac"; 
	
	public Duck(){
		super(Type.omnivor);
	}
	
	public Duck( String name ){
		super( name, Type.omnivor);
	}
	
	public Duck( Type type ){
		super( Type.omnivor);
	}
	
	public Duck( String name, Type type ){
		super( name, type );
	}
	
	public String speak(){ return this.talk; }
	
	@Override
	public String toString(){
		if( getName() != null )
			return( getName() + " : " + getType() );
		else
			return( "No-name : " + getType() );
	}	
}

Here is a test to show how it runs.

package Polymorphism;

public class polyTest {
	
	public static void main( String[] args ){
		Duck myDuck = new Duck();
		System.out.println( myDuck.toString() );
		System.out.println();
		System.out.println("Taking a peak at the heap");
		System.out.println("--------------------------------------------");
		System.out.println("Class name for myDuck: " + myDuck.getClass().getName() );
		System.out.println( "Super class name for myDuck: " + myDuck.getClass().getSuperclass().getName() );
		System.out.println();
		System.out.println("--------------------------------------------");
		
		try {
			System.out.println("Making ducks from ducks.");
			
			Duck evilDuck = myDuck.getClass().newInstance();
			System.out.print(evilDuck.speak() );
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}

Sample Output:

No-name : omnivor

Taking a peak at the heap
——————————————–
Class name for myDuck: Polymorphism.Duck
Super class name for myDuck: Polymorphism.Animal

——————————————–
Making ducks from ducks.
aflac

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s