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


Please Help Members By Posting Answers For Below Questions

How can I right-justify a string?

867


Is static variable stored in heap?

717


What is the purpose of using java.lang.class class?

756


What are the different types of sorting in java?

747


What is the use of bufferedreader?

768


Is hashset ordered?

775


Explain hashset and its features?

846


What is object-oriented programming?

805


Explain the selection sort algorithm and state its time complexity?

829


What are the features of junit?

837


Can sleep() method causes another thread to sleep?

799


Explain, java is compatible with all servers but not all browsers?

828


What is a protected void?

712


What is unmodifiable list in java?

729


Is array synchronized in java?

814