Tuesday, January 13, 2015

Method OverLaoding and OverRiding.

This is once of the most simple topic in java,however a lot of people does not have a clear understanding of method overloading and overriding.After reading this post you will have a sound knowledge of the above concept.

First i will start with Method Overriding.

Method overriding will be initiated only when super class inherited method and subclass method has the same name and same list of argument.
We will take once simple example to understand the concept, i personally think that one example is sufficient to understand the concept.

Overriding has mainly two concept involved.
1. Static bound.
2 .Run time bound.
I will explain them in detail don't worry. :-)

First see the  code.

class Person {
int id = 10;
public void test() {
System.out.println("Test:Person");
}

}

class Employee extends Person {
int id = 20;
public void test() {
System.out.println("Test:Employee");
}
}


public class OverridingDemo {
public static void main(String[] args) {
Person p = new Employee();
p.test();
System.out.println(p.id);
}
}

output:-
Test:Employee
10

Person p = new Employee();
p.test();

During Compile time compiler will check test() method in Person class,because compile time will only check the data type of the reference variable, not the object stored in the reference variable.

So during compile time p.test() will link to the Person class text() method, This is called the STATIC BOUND.

During run time, run-time environment will again see if the test() method is overridden in any of the subclass or not, if it is overridden in any of the subclass, run-time will break the previously established STATIC BOUND and establish a new RUN TIME BOUND.  



as in the above code, p.test() will be linked to the Person class text() method during compile time,run time will again check if the test() method is overridden in the Employee class, if yes then calls the Employee class test() method by breaking the static bound.

so p.test() will call the different test() method depending upon whether is has been overridden or not this is nothing but polymorphic behaviour.

Please Note only method can be overridden not the instance variable.
p.id will still call the id of the Person class.


Method Overloading
Method overloading depends on the signature,and signature depends on 3 things
1. Number of argument.
2. Data type of argument.
3. Order of argument.

One point to remember is that Method Overloading does not depend upon the return types, will give compile time error.

public class Demo{

public static void main(String[] args) {
int i= calculateIntrest(0);
}

static int calculateIntrest(int r){
System.out.println("one");
}

static void calculateIntrest(int p){
System.out.println("two");
}
}

will be a compile time error because of  duplicate method,

What will be the output of the following code.

public class Demo{

public static void main(String[] args)
{
int i= calculateIntrest(0);   //int
}

static int calculateIntrest(double r)   //double
{
System.out.println("one");
return 10;
}

static int calculateIntrest(long p)   //long
{
System.out.println("two");
return 20;
}

}

output:- two

compiler will consider 0 as int and look for the match in following sequence .
int-->long-->float-->double 
long is before double, so it will execute the calculateIntrest which takes "long as a parameter".



No comments:

Post a Comment

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...