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

COMPUTER SOFTWARE APPLICATION - CITS



               {
                       System.out.println(

                              “From child “
                              + “non - static(instance) m2() “);
               }
           }
           // Driver class

           class Main {
               public static void main(String[] args)
               {
                       Parent obj1 = new Child();

                       // As per overriding rules this
                       // should call to class Child static
                       // overridden method. Since static
                       // method can not be overridden, it
                       // calls Parent’s m1()
                       obj1.m1();

                       // Here overriding works
                       // and Child’s m2() is called
                       obj1.m2();
               }

           }
           4  Private methods can not be overridden
           Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override
           private methods in a subclass.
           class SuperClass {
               private void privateMethod()
               {
                       System.out.println(

                              “This is a private method in SuperClass”);
               }
               public void publicMethod()
               {
                       System.out.println(

                              “This is a public method in SuperClass”);
                       privateMethod();
               }
           }
           class SubClass extends SuperClass {

               // This is a new method with the same name as the


                                                           353

                             CITS : IT&ITES - Computer Software Application - Lesson 94 - 100
   361   362   363   364   365   366   367   368   369   370   371