Method Overriding

 Method Overriding

  • Refer to this code sample.
                      
                      class Monkey {
                      void eat (){
                      // Eat with hands 
                      }
                      void climb(){
                      // Climb with the help of body
                      }
                      }
                      class Man extends Monkey{
                      void eat (){
                      // Eat with spoons, forks in a plate
                      }
                      void climb(){
                       // Use a ladder to climb
                      }
                      }
  • Acording to the above code you can see that Monkey & Man are not equal.

  • changing method body of a method receiving from the super class to the sub class is known as Method Overriding (Method body is the code inside a method). 

      Refer to this code sample.
 
       class Father(){
       String car;
       }
       class Son extends Father{
       }

  • Question -  Do yo think that Father's son has a car accordig to the above code                          sample?

  •     Yes son has a car because son is extended by his father.
-(6th Article)        
                                                                                                    


Thank You!
                                                                                                           


                                                                                                            
                     
                                                                                                       -Article about Java by Hirun Yapa

Comments

Popular posts from this blog

Interfaces

Super Keyword

Abstract Classes