Object Oriented Programming - JAVA- Method Overriding

Object Oriented Programming - JAVA👽

Sometimes we have to fetch the same method name more than once. It is not possible in the programming process. But we can use method override to solve that error. So, in this blog we will discuss Method Overriding.


Method overriding ...

In a method inherited from a super class to a subclass, the process of changing the body is called as method overriding.

So, To overriding the classes should inherit with one class to another class.

Ex:
  class Monkey{
  void climb(){
  sout ("Monkey climbs with their legs on jumping");
  }
   }

  class Man extends Monkey{
  void climb(){
  sout ("Man climbs with using ladder");
  }
  }

Here, above the subclass is the method of the superclass, and above that is the method of the subclass.
That means, according to the example code, 
  • The method of Man class is at the top
  • The method of Monkey class is next
  • The Man class is below the method of Monkey class
In method overriding , the highest one is run.

How method override?

  • To overriding the classes should inherit with one class to another class.
  • Then subclass gets all the things of superclass.
  • And then, subclass has same method twice.
  • One is the method of superclass and another is the method of subclass.
  • Here, always get to run the method that is in top.
  • We can't run super class method in method overriding with out special keyword "Super".

Comments

Popular posts from this blog

Sorting Algorithms - JAVA👽

Object Oriented Programming - JAVA- class boundary, variable types, modifiers, java main method