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
How do you break a loop?
Can Exception handling we can handle multiple catch blocks?
What super () does in java?
What are the two types of streams offered by java 8?
What is the difference between length and size in java?
What is the use of hashmap in java?
What are the 2 types of java programs?
What is an infinite loop in java? Explain with an example.
What is appletviewer?
What is the use of string and stringbuffer?
What class of exceptions are generated by the java run-time system?
What are the differences between getting and load method?
Does java set allow duplicates?
Write a java program that prints all the values given at command-line.
Is string a class in java?