Page 401 - CITS - Computer Software Application -TT
P. 401

COMPUTER SOFTWARE APPLICATION - CITS
































           Example of method overriding

           In this example, we have defined the run method in the subclass as defined in the parent class but it has some
           specific implementation. The name and parameter of the method are the same, and there is IS-A relationship
           between the classes, so there is method overriding.
           1  //Java Program to illustrate the use of Java Method Overriding
           2  //Creating a parent class.
           3  class Vehicle{
           4  //defining a method

           5  void run(){System.out.println(“Vehicle is running”);}
           6  }
           7  //Creating a child class
           8  class Bike2 extends Vehicle{

           9  //defining the same method as in the parent class
           10 void run(){System.out.println(“Bike is running safely”);}
           11
           12 public static void main(String args[]){
           13 Bike2 obj = new Bike2();//creating object
           14 obj.run();//calling method

           15 }
           16 }
           Output:    Bike is running safely
















                                                           388

                             CITS : IT&ITES - Computer Software Application - Lesson 109 - 115
   396   397   398   399   400   401   402   403   404   405   406