Friday, January 23, 2015

Override vs Overload Methods in Java

In my previous blog I have written about IS-A and HAS-A relationship in Java which is based on the inheritance in Java.

If class have satisfied IS-A relation with other class than you have opportunity to override and overload class method in your class. Let’s see details in below section.

Overridden Methods

What is override method ? - Define a super class method in sub-class with a specific behavior to a particular subclass. Same method is present in super class as well as in sub class but with some extra code or some additional functionality.

For example    

public class Animal {
       public void eat() {
              System.out.println("Animal Eat Method");
       }
}
class Cat extends Animal {
       public void eat() {
              System.out.println("Cat Eat Method");
       }
}


Rules for Override Method

1) Arguments list must exactly same as overridden method. If they don’t match it is not override 

public class Animal {
       public void eat(String food) {...}
}
class Cat extends Animal {
       @Override
       public void eat(String food) {...}
}


2) Return type is same or subtype of the return type declare in the overridden method in super class

class Animal {
       public Animal eat(String food) {...}
}
class Cat extends Animal {
       @Override
       public Cat eat(String food) {...} // here return type is subtype of the Animal class
}

3)  Access level of override method in sub class can’t be more restrictive than overridden method in super class. It can be less restrictive.
     
            public class Animal {
       protected void eat(String food) {...}
}
class Cat extends Animal {
       @Override
       private void eat(String food) {...} // This is illegal access level

       public void eat(String food) {...} // This is legal access level
}

4)     You cannot override static method. If you do this than it is not override because override is depend on the instance of the class and static method is impendent on the class instance.

5)      Instance methods are override only if class is inherited by subclass.

6)      Subclass can override methods of superclass if methods cannot marked private or final in same package. Subclass in different package can override only non-final method marked with public or protected.

7)      The override method can throw any unchecked / runtime exception whether overridden method define it or not.

class Animal {
       public void eat(String food) { }
}
class Cat extends Animal {
       @Override
       public void eat(String food) throws ClassCastException { }
}

8)      Override method cannot throw Checked exception or broader than that declare in the overridden method. 

For example you can’t declare override method with FileNotFoundException unless overridden method declare exception that is super class of FileNotFoundException (i.e. IOException )

class Animal {
       public void eat(String food) throws IOException { }
}
class Cat extends Animal {
       @Override
       public void eat(String food) throws FileNotFoundException { } // this is llegal because FileNotFoundException is subclass of IOException

       public void eat(String food) throws Exception { } // this is illegal because Exception is super class of the IOException

}

9)   You cannot override final method.

10)  You cannot override method unless you inherited that class, if not that is not override it is simply class method define in same in two difference class.


 Overloaded Methods

Overload method let you define method with same name but with different argument (it may have different return type) in the same class or in subclass.

For Example

class Addition
{
       public void add(int a,int b){}
       public int add(float a,float b){return 0;}
       public void add(int a,double b){}
       protected void add(String a,int b) throws ArithmeticException{}
}

All add method define in class Addition is legal Overload method.

Rules for Overload Method
  1.  Overload Methods name must be same (methods name “add” in above example are same).
  2.  Overload methods are within the same class or in subclass.
  3.  Overload methods must change argument list, it can one or more or none.
  4.  Overload method may change return type.
  5.  Overload method may declare any Exception.

Wednesday, January 21, 2015

What Is IS-A and Hash-A relationship in JAVA?

Before we start to explore this topic in details I hope that you know basic about inheritance in JAVA, If not please go through this link (http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html) or read some article about inheritance first.
This is some of the common question asked in interview and also useful for the design class that follow good OO practice.

IS-A

The concept of the IS-A relationship is based on the Inheritance or Interface implementation. You can define IS-A Relationship as “This thing is type of that thing”. 

For Example:- Car is a type of Vehicles. In OO we can say that “Car IS-A Vehicle
You can express IS-A relationship in Java with keyword “extends” for Inheritance and “implements” for Interface.

For Example:-

public class Vehicle
{
       //Class Code goes here
}

public class Car extends Vehicle
{
       //Car class code goes here
       //This class also inherits Vehicle class Methods and Variables

}


So from above code you can say that Car has IS-A relationship with Vehicle. 
This inheritance tree might be go down also like

public class Vehicle { ... }
public class Car extends Vehicle { ... }
public class Toyota extends Car { ... }

From above code you can say following statements

“Car extends Vehicle” means “Car IS-A Vehicle.”
“Toyota extends Car” means “Toyota IS-A Car.”

You can also say that “Toyota IS-A Vehicle.” Because class Toyota is Instance Of class Vehicle ( Toyota instanceof Vehicle is true )

Below Image illustrate the inheritance tree for Vehicle,Car and Toyota. Arrow moves from subclass to superclass.





HAS-A
HAS-A is not based on inheritance, but it is based on the uses and access of one class to other class’s methods and variables. 

We can say class Car HAS-A Engine if class Car hash an instance of class Engine.
For this code look like this

public class Vehicle { }
public class Car
{
       public Engine engine;
}
public class Engine { }

From above code, Car hash an instance variable of class Engine, so we can say that  “Car HAS-A Engine”. Car has a reference to the class Engine, using this reference Car class invoke methods in class Engine or access variable in class Engine.

It good to use HAS-A for good OO design.

There are some point when too used IS-A or HAS-A relationship in code or project.
  • Don't use IS-A (Inheritance or Interface) just to get code reuse If all you really want is to reuse code and there is no IS-A relationship in sight, use HAS-A relationship.         
  • Don't use Inheritance just to get at polymorphism If all you really want is polymorphism, but there is no natural IS-A relationship, use HAS-A with interfaces implementation.