Thursday, November 20, 2014

Inheritance – “A type of” relation NOT “contains” relation

Many times people confuse inheritance with “contains” relationship, which is not correct at all. Inheritance is “A type of” relationship not “contains” relationship. So a child is a superset of Parent’s abilities.

Example: Each fox is an Animal. But the other way is not always true (Not every Animal is Fox). Hence, Fox is a type of Animal.

class  Animal{

                                    1.   Ability of walking

}

class Fox extends Animal{

                         1.   Inherited abilities: Ability of Animal (walking)

                         2.    It’s own abilities (like hunting)

}

Now let's try to digest few concepts here.
When I write this :  Animal  a = new Fox(). What does it mean?
It means Fox is also an Animal or  every Fox is a type of Animal which makes sense too.

What about :   Fox  f = new Animal() 
It means Animal is a type of Fox or every Animal is a Fox, which is absolutely incorrect.

So "Fox is an Animal" is fine but the reverse doesn't make sense. For example, Apple is a Fruit, Car is a Vehicle etc. Remember inheritance is uni-directional. For example, House is a Building. But Building is not a House.

Let's formulate our finding in terms of coding: A reference to child class object can't refer to the parent class object. Whereas a reference to parent class object can refer to child class object.

Example 1: 
       Animal a =  new Fox();     //True
       Fox  f = new Animal();      //False (Compilation error)

Example 2:
       Animal a = new Animal();
       Fox f = new Fox();
       a = f;               //True
       f = a;               //False (Compilation error)

So we learned few things here: 
A child class object is inherently a parent class object. In simple terms, objects/reference of parent class can hold objects of child class but the reverse is not true.

Example 3:
public class TestMain {

       public static void main(String[] args) {

              Animal a = new Fox();

              a.walk();

              ((Fox)a).hunt(); //need to typecast because reference "a" doesn't hold "hunt" method

       }

}



class Animal{

       void walk(){

              System.out.println("Animal can walk");

       }

}



class Fox extends Animal{

       void hunt(){

              System.out.println("Fox can hunt");

       }
} 

Output:
Animal can walk
Fox can hunt


Few points to mention here:
1.  A subclass can't inherit private members of Superclass obviously.
2. Try to keep all variables private or protected and use public accessor(set/get) methods to access them.
3. On a light note: Initially don't try to learn this concept with "child" and "parent" example as you might get confused. Playing with a dog or a cat is safe and easier. :)

Tuesday, November 18, 2014

Difference between method overloading and method overriding

Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters.  Overloading is a compile time (static) phenomenon because the compiler is aware of exactly which method you are calling based on the number/type of parameters.
 
class Animal{
       void test(){
              System.out.println("Inside Animal class with no aruguments");
       }
      
       void test(int a){
              System.out.println("Inside Animal class with int aruguments");
       }
}

public class TestMain {
       public static void main(String[] args) {
              Animal a = new Animal();
              a.test();
              a.test(5);
       }

Output:
Inside Animal class with no aruguments
Inside Animal class with int aruguments

In this case, the compiler knows exactly which “test” method we are calling, based on the number/type of parameters.

Overriding methods is completely different from overloading methods. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. This would be considered overriding. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method. Overriding is an example of dynamic (run-time) polymorphism. This is due to the fact that the compiler doesn't necessarily know what type of object is being passed in at compile-time.

class Animal{
       void test(){
              System.out.println("Inside Animal class with no aruguments");
       }
      
       void test(int a){
              System.out.println("Inside Animal class with int aruguments");
       }
}

class Dog extends Animal{
       void test(){
              System.out.println("Inside Dog class with no arguments");
       }
}

public class TestMain {
       public static void main(String[] args) {
              Animal a = new Dog();
              a.test();
              a.test(5);
       }

}

Output:
Inside Dog class with no aruguments
Inside Animal class with int aruguments

Here, method dispatching is performed in two steps:
  • The first one is done at compile time with the static information available, the compiler will emit a call for the signature that matches best your current method parameters among the list of overloaded methods in the declared type of the object the method is invoked upon.
  • The second step is performed at run time, given the method signature that should be called (previous step), the JVM will dispatch it to the most concrete overridden version in the actual type of receiver object.