Page 451 - Computer science 868 Class 12
P. 451

double findcircumference()
                      { /* Member data r belongs to base class
                       can be accessed  by derived class due to
                        inheritance */
                          return Math.PI*r*r;     }
                       void display()
                       { c=findcircumference();
                         super.display(); // method overriding
                         System.out.println("Circumference ="+c);
                        }}















                 From the above example, it is evident that multiple derived classes (Area and Circumference) can inherit properties
                 (radius r) of the base class (Circle).
                 Now, if we change the access of class Circle to final and try to inherit it from class Area, then we get the error message
                 – 'cannot inherit from final Circle' as shown in the picture.





















                 Let us demonstrate the use of super() keyword with another example. Where the base class default constructor is
                 automatically invoked from derived class.

                    class Base
                    {
                      Base()
                      { System.out.println("Base class constructor is invoked implicitly");
                        System.out.println("Even if we do not write super() statement in derived class");
                    }
                    }

                    class Derived extends Base
                    { Derived()
                      {  System.out.println("============================================================
                         ======");
                        System.out.println("Derived class constructor is executed after base class
                        constructor");
                    }}

                                                                                                                       449
                                                                                  Inheritance, Interfaces and Polymorphism  449
   446   447   448   449   450   451   452   453   454   455   456