Define Overriding. Explain it with an example.
- In overriding, methods have the same signature as the parent class method.
- Overriding is done in a child class for a method that is written in the parent class.
- It changes the existing functionality of the method.
- It happens at runtime.
- To override a method, a new method is defined in the child class with exactly the same signature as the one in the parent class. Due to this the functionality of the parent class becomes inaccessible.
Example:class shape
{
getShape()
{
//code
}
}
class circle
{
getShape()
{
//code
}
}
class square
{
getShape()
{
//code
}
}