Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Difference between overloading and Overriding. <Giving a
confusing example to test the concept.>
(also asked in PA Consultancy Group,iflex,Value
chain,IBM,CTS,Accenture, Tarang>

Answer Posted / suman

OVERRIDING – when you extend a class and write a method in
the derived class which is exactly similar to the one
present in the base class, it is termed as overriding.


Example:

public class BaseClass{

public void methodToOverride()

{

//Some code here

}

}

public class DerivedClass extends BaseClass{

public void methodToOverride()

{

//Some new code here

}

}

As you can see, in the class DerivedClass, we have
overridden the method present in the BaseClass with a
completely new piece of code in the DerivedClass.

What that effectively means is that if you create an object
of DerivedClass and call the methodToOverride() method, the
code in the derivedClass will be executed. If you hadn’t
overridden the method in the DerivedClass then the method
in the BaseClass would have been called.

OVERLOADING - when you have more than one method with the
same name but different arguments, the methods are said to
be overloaded.


Example:

public class OverLoadingExample{

public void add(int i, int j)

{

int k = i + j;

}

public void add(String s, String t)

{

int k = Integer.parseInt(s) + Integer.parseInt(t);

}

}

As you can see in the example above, we have the same
method add() taking two parameters but with different data
types. Due to overloading we can now call the add method by
either passing it a String or int

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we override private method?

1054


What is hash code collision?

1021


How many days will it take to learn java?

899


How to display names of all components in a Container?

2902


If an application has multiple classes in it, is it okay to have a main method in more than one class?

978


Why can we not override static method?

1080


State the difference between creating string as new () and literal.

1050


What is a class instance variable?

1071


What is a treeset class?

963


Can we write a class without main method in java?

915


Can we use String with switch case?

1132


What are the approaches that you will follow for making a program very efficient?

1029


Is 0 a prime number?

945


What is a local class in java?

1000


What is nan in java?

956