1) What is the difference between method overriding and method overloading when working with java interfaces?
Question 2:
Why should you use Stringbuffer objects instead of String objects in a program that makes lot of changes to strings?
Write a code snippet to demonstrate auto boxinng.
Expert Answer
1)Answer:
difference between method overriding and method overloading :
method overriding:
->When a super class method is modified in the sub class, then we call this as method overriding
->Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
->Method overriding occurs in two classes that have IS-A (inheritance) relationship.
->In case of method overriding, parameter must be same.
->Return type must be same or covariant in method overriding.
->Overridden methods must have same method signature. I.e. you must not change the method name, types of arguments, number of arguments and order of arguments while overriding a super class method.
->example is run time polymorphism.
->You can’t override a static method.
method overloading :
->When a class has more than one method with same name but with different arguments, then we call it as method overloading.
->Method overloading is used to increase the readability of the program.
->Overloaded methods must have different method signatures. That means they should differ at least in any one of these three things – Number of arguments, Types of arguments and order of arguments. But, they must have same name.
->Overloaded methods can be static or not static. It does not affect the method overloadinhg
Method overloading is performed within class. ->In case of method overloading, parameter must be different. ->in java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. ->Method overloading is the example of compile time polymorphism |