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

What is the main use of generics in java?

512


What is static class

620


How do you convert boolean to boolean?

527


Explain about field hiding in java?

530


What is user defined exception in Java?

615






what is the difference between the methods sleep() and wait()? : Java thread

517


What are the benefits of operations in java?

493


What is a method header?

550


What do you mean by inner class in java? Explain

592


What is constructor in java ?

636


What is the purpose of declaring a variable as final?

490


Can java inner class be static?

562


Does substring start with 0?

560


What is executor memory?

527


Tell me a few examples of final classes defined in Java API?

570