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

COMPUTER SOFTWARE APPLICATION - CITS



               private void m1()
               {

                       System.out.println(“From parent m1()”);
               }
               protected void m2()
               {
                       System.out.println(“From parent m2()”);
               }

           }
           class Child extends Parent {
               // new m1() method
               // unique to Child class

               private void m1()
               {
                       System.out.println(“From child m1()”);
               }
               // overriding method

               // with more accessibility
               @Override public void m2()
               {
                       System.out.println(“From child m2()”);
               }

           }
           // Driver class
           class Main {
               public static void main(String[] args)
               {

                       Parent obj1 = new Parent();
                       obj1.m2();
                       Parent obj2 = new Child();
                       obj2.m2();

               }
           }
           2   Final methods can not be overridden
           If we don’t want a method to be overridden, we declare it as final. Please see Using Final with Inheritance.
           A Java program to demonstrate final methods which cannot be overridden
           class Parent {
               // Can’t be overridden

               final void show() {}
           }


                                                           351

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