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 restrictions are placed on method overriding in java programming?

758


In Java list the methods that can be overridden?

780


What is difference between checked and unchecked exception in java?

801


Why is java so important?

871


What is the replace tool?

772


Write a regular expression to validate a password. A password must start with an alphabet and followed by alphanumeric characters; its length must be in between 8 to 20.

799


What is balanced tree in java?

715


What is difference between pointer and reference?

721


Is void a return type?

737


How will you call an Applet using Java Script Function?

749


What is private static class in java?

749


What is a static method in java?

764


Why do we use string?

779


What are the core java topics?

785


How do I find and replace in word?

734