Friday, August 2, 2024

Method Overriding

 

Method Overriding:

FAQ :What is method overriding ?

·        In  general overriding plan means changing plan.

                          Method over riding -->       changing method  code /  body

Def:

Writing same method from parent class as it is in child class with different code or logic

Writing same method name, return type, and parameters from parent class into child class

               //    don’t’ change return type also

·        //  With out inheritance , Method over riding is not possible

·        Method overriding is only possible when there is inheritance between the classes.

 

package InheritanceConstrctor;

class A

{

               void M1()

               {

                              System.out.println("M1 uses logic -1");

               }

}

class B extends A

{              // child class -A ,   parent class -B -->

              

//                                                                                                                     void M1()

//                                                                                                                     {

//                                                                                                                                    System.out.println("M1 uses logic -1");

//                                                                                                                     }

//                                                                                                                    

               // over ride parent class M1()

               void M1()

               {

                              System.out.println("M1 uses logic -2");

                              System.out.println("M1 uses logic -2");

                              System.out.println("M1 uses logic -2");                 

               }             

}

public class TestMethodOverRiding

{

               public static void main(String[] args) {

                              // create obj for class-B

                             

                              B bref = new B();

                              // call M1()

                              bref.M1();

                             

                              A aref = new A();

                              aref.M1();

}

}

ex2:  car class - startCar()

 

package InheritanceConstrctor;

class car

{

               // Define startcar()

               void startCar()

               {

                              System.out.println("insert Keys and Turn to Right side");

               }

}

class Maruthi extends car

{             

//                                                                                                                                                  

//                                                                                                                                                   void startCar()

//                                                                                                                                    {

//                                                                                                                                                   System.out.println("insert Keys and Turn to Right side");

//                                                                                                                                    }

               // over ride startcar() with diff code

               //  Press   'Start' button

               void startCar()

               {

                              System.out.println("To start the car - Press start button");

               }

}

class Tesla extends car

{             

                                                                                                         //            void startCar()

                                                                                                         //            {

                                                                                                         //                           System.out.println("insert Keys and Turn to Right side");

                                                                                                         //            }

              

               // over ride startcar() with diff code

               //   USe advanced AI --  self Driving cars

               void startCar()

               {

                              System.out.println("To start the car-Voice cmds/ Recoginisation technique");

               }

}

public class testMethodOverRiding2 {

               public static void main(String[] args) {

                              // create obj for class- Maruthi

                              Maruthi mcar  = new Maruthi();

                              // call startCar();

                              mcar.startCar();

                              //                           // create obj for class- Tesla 

                              Tesla tcar =  new Tesla();

                              // call startCar();

                              tcar.startCar();

                             

                              // create obj for car class

                              car cref = new car();

                              cref.startCar();

               }

}

o/p:

To start the car - Press start button

To start the car-Voice cmds/ Recoginisation technique

insert Keys and Turn to Right side

FAQ : when should we go for Method overriding ?

When we want to change parent class method’s code inside child class, then we will go method over riding.

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...