Define overriding. Explain it with an example

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
   }
}
Difference between overloading and overriding
Difference between overloading and overriding - Overloading and overriding are different aspects of polymorphism...
Java - Explain the use of ‘super’ keyword by giving an example
Use of ‘super’ keyword - super() / super(parameters) invokes the respective constructor or a method from the super class...
Define a final class in java. Explain the purpose of final class
Define a final class in java - A class declared as final cannot be extended further by any other class....
Post your comment