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>

Answers were Sorted based on User's Feedback



Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / esha prasad

Over loading is polymorphism implementation in same class,
where two or more methods can share same name differing in
parameters passed.
Overriding is polymorphism implementation in different
classes having parent child relationship and the
funtionality in parent class is over shadowed by the
funtionality in subclass.

Is This Answer Correct ?    72 Yes 14 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / arthi

overloading means same method name but different parameters
are passed but overriding means same method name with same
parameters passed in different classes.

Is This Answer Correct ?    40 Yes 8 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / rakesh

Overloading class has different returntype.
overloading comes with in class but overriding comes in two
classes.
Overloading is resolved at compiletime
Overriding is resolved at runtime.

Is This Answer Correct ?    39 Yes 18 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / priyanka patil

Function Overloading
class A
{
int show(int a,float b)
float show(int a,int b)//different prototype can valid but
as per parameters they called
}
Function overriding
class A
{
int show(int a)
};
class B : public A
{
int show(int a)//function prototype of base class fun n
derived class function must be same
}

Is This Answer Correct ?    26 Yes 8 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / varun

overloading : can only be done by changing the type or no.
of parameters for eg:a(int a);
a(int a,int b);


while in case of overriding we can give new definition
without changing the parameters.

Is This Answer Correct ?    20 Yes 8 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / ravikant baluni

Method overloading:
-------------------
Method overloading means having two or
more methods with the same name but different signatures in
the same scope. These two methods may exist in the same
class or anoter one in base class and another in derived
class.

Like if we have a method: void sum(int a,int b){---} then
it can be overloaded as:
void sum(int a,int b,int c);
void sum(string s1,string s2);

Method overriding:
------------------
Method overriding means having a
different implementation of the same method in the
inherited class. These two methods would have the same
signature, but different implementation. One of these would
exist in the base class and another in the derived class.
These cannot exist in the same class.

Like if we have a method: int fun(int a,int b){---} then it
can be overrided as:
int fun(int a,int b)
{
return (a+b);
}



int fun(int a,int b)
{
return (a*b);
}


int fun(int a,int b)
{
return (a/b);
}

Is This Answer Correct ?    14 Yes 4 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / kabita

overriding is only possible in case of inheritance,bt
overloading is possible incase of both inheritance & same
class.
class A
{
public int add();
public float sub();
public float multi(float a,float b){}
public double div(double d,double e){}
}
class B extends A
{
public int add(int a,int b){}//must be same as declared in
super class
public float sub(float a,float b){}//must be same name same
returntype same as declared
public float multi(float a,float b,float c){}

}

Is This Answer Correct ?    12 Yes 9 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / ketan gambhir

Overloading:-in overloading,we can use same name of method
and constructor with different parameters in same class.
For Exa:-
\\Both methods has different declaration and definition.
class A
{
public int same(int a int b)
{
return a+b;
}
public void same()
{
System.out.println("ketan");
}
}
Overriding:-In overriding,we can use same method and
constructor with same parameters in different classes.
For Exp:-
class A
{
public void same()
{
System.out.println("same1");
}
}
class B
{
public void same()
{
System.out.println("ketan");
}
}

Is This Answer Correct ?    3 Yes 1 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / mariyappan

overloading is the process of defining the same function
with different parameters.which can be defined within class.
overriding is the process of defining the function name in
the more than one classes.

Is This Answer Correct ?    2 Yes 1 No

Difference between overloading and Overriding. <Giving a confusing example to test the concept...

Answer / 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

More Core Java Interview Questions

What classes of exceptions, thrown by a throw statement?

2 Answers  


Why does it take so much time to access an applet having swing components the first time?

0 Answers  


Linked List reverese program

2 Answers   Bally Technologies,


Why convert an applet to an application?

0 Answers  


i need to know the site which compiles and run the java program completely in online... thank you for your reply in advance

2 Answers  






What is finally and finalize in java?

0 Answers  


What is character in data type?

0 Answers  


Explain about java sdk?

0 Answers  


What are the loops in java?

0 Answers  


What is the exception hierarchy in java?

0 Answers  


What is void data type?

0 Answers  


What is the static import?

0 Answers  


Categories