top of page

METHOD OVERRIDING IN JAVA

 

If subclass (child class) has the same method name and parameters as declared in the superclass (parent class), it is known as Method Overriding. Method overriding is used to provide the specific implementation of a method that has been declared by its parent class. Method overriding is used for runtime polymorphism.


Rules for Method Overriding:

·       The method must have the same name as in the parent class.

·       The method must have the same parameter as in the parent class.

·       There must be an IS-A relationship (inheritance).


Example of Method Overriding:

In this example, we have defined the color method. The color method in subclass has specific implementation of a method that has been defined in the superclass. There is IS-A relationship between the parent class and sub class, the name and parameter of the method are the same, so it is method overriding.

 

class Flower{  

{     void color()

      {           System.out.println (“Colorful flowers”);           }

 } 

 

class Rose extends Flower

{     void color()

      {           System.out.println (“Rose is red”);  }


public static void main(String[] args)

{        Rose r1 = new Rose();

            r1.run();        

}

 }  

 

OUTPUT:             Rose is red

 
 
 

Recent Posts

See All

Comments


MiIT Logo

Company

Contact Us

+1905-487-4880 

5160 Explorer Dr #34, Mississauga,ON L4W 4T7

+1929-743-3199

4466 Buttonwood Ln Lilburn, GA 30047

262 Chapman Rd, STE 240 Newark DE 19702

+44 20 4525 1214

71-75 Shelton Street, Covent Garden, London, United Kingdom WC2H 9JQ

Stay up to date on the latest from MiIT

  • Instagram
  • Facebook
  • http://linkedin.com/company/miittechnologies/about/
  • Whatsapp

© All Content MiIT Technologies Inc.2019 - 2025. All rights reserved.

bottom of page