what is overloading and overriding with example?
Answer Posted / sujanya
Overloading means two methods have the same method name and
different argument list.
For example, take the case of a Shape Class where you have
a method with the name DrawShape();
This method has two definitins with different parameters.
1. public void DrawShape(int x1, int y1,int x2,int y2)
{
// draw a rectangle.
}
2. public void DrawShape(int x1,int y1)
{
// draw aline.
}
overriding means i have a super class and sub class,sub
class extends the super class.Two classes containg the same
method name and same arguments sub class overides the super
class method ,this is nothing but method overriding
for example
Class Rectangle
{
publc void DrawRectangle()
{
// this method will draw a rectangle.
}
}
Class RoundRectangle : Rectanlge
{
public void DrawRectangle()
{
//Here the DrawRectangle() method is overridden in the
// derived class to draw a specific implementation to the
//derived class, i.e to draw a rectangle with rounded
corner.
}
}
| Is This Answer Correct ? | 48 Yes | 16 No |
Post New Answer View All Answers
What is meant by oops concept in java?
What is update method called?
What is the size of an array?
Can we print null in java?
What are parameters in a method?
How will you reverse a link list without using recursion?
How would you format a date in java? I.e. In the ddmmyyy format?
What access modifiers can be used for methods?
What is the maximum size of arraylist in java?
a thread is runnable, how does that work? : Java thread
What does next mean in java?
What is the escape character in java?
What is public static void main?
What is the flag in java?
What is the original name of java?