Saturday, 9 March 2013

Java Basics For Oracle Apps Developers 2

Without further ado ... Lets continue ...

Overview: Re-usability, Method Overriding, Method overloading.
Duration: 30 Mins

One of the biggest advantage of Java language is its re-usability. And this is what you will be doing very often in ADF development.

In the last example we saw class called Animal with some methods. If I have to use the same methods I just have to Import that package and simply start using it. As simple as that. (As a matter of fact you don't even have to import it, you can just qualify it with package name; but importing will make your code look good, as you don't have to always write entire package name every time you want to use a method).

Lets see how its done.
Right click on Application Sources and chose Java-> Class Create another Class in a different package. Don't check Constructors and Implement Abstract Methods just as shown below.


Create a main method and start to Animal to create an Object of the Animal class. But since Animal is in a different package system will prompt you to Import the package as shown below (on clicking the Animal).

Press Alt Enter to import. (When you start typing Animal for the first time system may say importing com.tutorial.Animal and will do so without you doing anything)

And when you type "animal." (see screenshot below) in the next line you will have access to all the methods of class "Animal". So, if someone has already written some code, its so easy to import and re-use it. For me this makes Java as cool as it is ;)



So lets go ahead and complete the code as shown below.


Here is the code.

package com.tutorial.catfamily;

public class Lion {
    public static void main(String[] args) {

        Animal animal = new Animal();
        String ani = animal.findAnimalFromSound("roar");
        System.out.println("Reusable from Animal class :" + ani);

    }

}

*Please add "import com.tutorial.Animal" from screeshot above as my system is not allowing to use import for some reason.

Method Overriding

Now lets say that you are not satisfied with one of the method in Animal class (or may be its wrong;). Lets assume findAnimalFromSound should not only return Lion for Roar, it should also return Tiger.

Java enable you to override the existing method.
In Lion class, type extends Animal as shown below.
This will make Lion a Subclass and Animal a Superclass.  Technically, you can even extend Lion (when defining a *cub* class) and use and override all of its methods. This way a Subclass "Inherits" all of the properties of a "Superclass", along with having its own properties (methods and variables).



Create another method findAnimalFromSound with same signature (i.e., 1 String argument and returns String.) This will allow you to "Hide/Shadow" the existing definition of findAnimalFromSound and use your own.


The findAnimalFromSound of this Subclass is said to have "Overridden" the method of the Superclass Animal.
Lets call this method in main and see what it results in. Notice that the function returns "Tiger or Lion".



Now lets turn our attention to Method overloading.
Assuming that you need to find the animal name from Sound, but since both Lion and tiger roar; we need additional attributes to find the Animal, so lets define another method, which will take another argument, lets call it stripes as shown below.

Then try to call it in the main package. It will show Tiger or Lion depending on the argument passed in the second parameter.



To summarize, 
When the method name and signature is same its called Method overriding.
When the method name is same but it has a different signature, its called Method overloading. 

Here is the final code:


package com.tutorial.catfamily;

public class Lion extends Animal {
    public static void main(String[] args) {

        Animal animal = new Animal();
        String ani = animal.findAnimalFromSound("roar");
        System.out.println("Reusable from Animal class :" + ani);

        Lion lion = new Lion();
        System.out.println("Over ridden Roar: " +
                           lion.findAnimalFromSound("Roar"));
        System.out.println("Over loaded Roar: "+lion.findAnimalFromSound("roar", "yes"));
        System.out.println("Over loaded Roar: "+lion.findAnimalFromSound("roar", "no"));
        
    }

    public String findAnimalFromSound(String animalSound) {
        if (animalSound.equalsIgnoreCase("roar")) {
            return "Tiger or Lion";
        } else
            return "Not found";
    }

    public String findAnimalFromSound(String sound, String stripes) {
        if (sound.equalsIgnoreCase("roar") &&
            (stripes.equalsIgnoreCase("yes"))) {
            return "Tiger";

        } else {
            return "Lion";
        }

    }
}


No comments:

Post a Comment