Student Activity

Java Code Examples.

We are going to look at how Java implements the Object Oriented concepts.

(see http://www.java-made-easy.com/java-inheritance.html or http://examples.javacodegeeks.com/java-basics/java-inheritance-example/)


Java Coding Environment.

If you have not got Java 8 installed, there is a free online Interactive Development Environment : http://www.tutorialspoint.com/compile_java8_online.php

Alternatively, you can download a free IDE: http://www.bluej.org

Inheritance

You can create your Java Class Files and save them to your Directory

We will create a SuperClass: Animal with some attributes and behaviours, then extend them (create Sub Classes) into two different types of Animals: Cat and Dog, with specific attributes and behaviours.

Create Animal.java (SuperClass) as follows:

public class Animal {

public Animal() { System.out.println("A new animal has been created!"); } public void sleep() { System.out.println("An animal sleeps..."); } public void eat() { System.out.println("An animal eats..."); } }

Create Bird.java (Sub Class) as follows:

public class Bird extends Animal {
	public Bird() {
		super();
		System.out.println("A new bird has been created!");
	}
	
	@Override
	public void sleep() {
		System.out.println("A bird sleeps...");
	}
	
	@Override
	public void eat() {
		System.out.println("A bird eats...");
	}
}

Create Dog.java as follows:

public class Dog extends Animal {
	public Dog() {
		super();
		System.out.println("A new dog has been created!");
	}
	
	@Override
	public void sleep() {
		System.out.println("A dog sleeps...");
	}
	
	@Override
	public void eat() {
		System.out.println("A dog eats...");
	}
}

To test these Classes, we will create the following MainClass.java as follows:-

public class MainClass {
	public static void main(String[] args) {
		Animal animal = new Animal();
		Bird bird = new Bird();
		Dog dog = new Dog();
		
		System.out.println();
		
		animal.sleep();
		animal.eat();
		
		bird.sleep();
		bird.eat();
		
		dog.sleep();
		dog.eat();
	}
}
IMPORTANT NOTE
Java SuperClass Constructor always called when SubClass is created

In Java, when we instantiate a SubClass, the SuperClass default Constructor method is always called implicitly first before the SubClass default Constructor Method.
If your SuperClass has a parameters in its Constructor Methods, then you need to explicitly call the Super class's constructor with the parameter list.



Polymorphism

Taster
Each specialised Animal (Dog or Bird) both override the sleep() and eat() methods to demonstrate behaviour specific to them. This is known as Polymorphism.

Specialised Methods

Now we can write code that's specific to Dog objects, let's write a method called bark:



Only Dogs can bark, so the subclass has this method. The superclass has no access to this method whatsoever.

In MainClass.java add code to invoke this new method.



Polymorphism in Detail
Introduction

Java is an object-oriented programming language, so polymorphism in Java is the bread and butter of object-oriented programming in Java. In this tutorial we're going to find out what polymorphism is, why it is so useful, and then show how to use it to create elegant programs. This concept is somewhat difficult to put into practice because it happens to be much more abstract than many of the other concepts. However, if you can master it, you will become that much better of a programmer and you will have a much easier time mastering the Java language.

What is Polymorphism?

Polymorphism is THE concept to master if you want to master object-oriented programming. Because Java is an object-oriented language, it makes sense that you should learn the concepts and power of polymorphism in Java.

Simply put, polymorphism is what allows actions to act differently based on the object performing the action or the object the action is being performed on. Let's just use a super simple, real life example. What is a typical sound that a cat makes? Let's just say it's meow. Let's call this action makeSound() because remember, a method can represent an action. What sound does a dog make? Let's just say a dog goes woof. We can also call this action makeSound(). Let's also just pretend that all animals can makeSound(). Thus, makeSound(), depending on the type of animal, does something different. The action acts differently based on the object.

This concept of polymorphism in Java especially, is actually not hard to understand at all. Oh look, different animals make different sounds, and the same method can be used to make each distinct sound. If you've done the Java inheritance tutorial, you already know how to do this!

One powerful tool for using polymorphic behavior is to use the same method name but in the same class, over and over again to get the desired effects you want. How can we use polymorphism in Java to accomplish this?


Overloaded Methods

Let's use our makeSound() example again. Let's say a dog makes a woof sound, but if the dog is injured, the dog might make a whimper noise instead. How can we use makeSound() to produce both sounds? Take a look at this code snippet:

NOTE: At this point, if you're not sure you understand the code you see, you REALLY should go back to the Intermediate Tutorials and read the tutorial on Methods In Java. Then you can come back to learn about polymorphism in Java once you have a better understanding of methods.

We can see here that depending on the number of parameters we pass to makeSound(), the dog will make a different sound. But wait! Couldn't we have just used an if statement and make this just one method? Yes, we could have done that and that's probably a better way of programming this for this particular example. What if an outside action causes the difference in dog sound though? Something like this:

If the dog did not have the variable to know it was hurt, you would not be able to write that if statement as easily.

You can overload a method as much as you want as long as the number of parameters are different or the types of parameters are different. You could not do this for example:

This is because those are the same number of parameters AND are the same types of parameters.

Overridden Methods

In Java, you can create a method in a superclass (or parent class), then in a subclass ALSO define that method. Let's see an example of this using Animal:

Now, let's say you could actually create Animals. If you could, then calling makeSound() would call the method defined in the superclass. If you create a specific Dog, calling makeSound() will display "woof". Now let's say you created a specific Cat. In that example, Cat does not have a makeSound() method, so when you call makeSound() on Cat, it calls the method in the superclass Animal and does nothing. Remember, Cat and Dog are subclasses of Animal because they extend Animal.

This behavior is called method overriding, and it is EXTREMELY powerful stuff when it comes to polymorphism in Java. Java will let you do this because its possible more specific types of objects have more specific behaviors for actions. How does Java then know which method to actually call? Java will always pick the object's true type for choosing which method to call, the superclass or the subclass. We will see what true type really means in the next section.



Read more: http://www.java-made-easy.com/polymorphism-in-java.html#ixzz4F5kU4NhV








Comments