Page 469 - Computer science 868 Class 12
P. 469

Program 8     An abstract base class Side is defined as follows:
                                 Class name                     :  Side
                                 Data Members
                                 int s                          :  To store side of a square
                                 Member Methods
                                 Side(int ss)                   :  Constructor to initialise data member
                                 abstract void area()           :  Abstract method
                                 void display()                 :  To display side of the square
                                 A derived class Area is defined to find and display the area of a square. The details of the
                                 class is given below.
                                 Class name                     :  Area
                                 Data Member
                                 int a                          :  To store area of a square
                                 Member Methods
                                 Area(int ss)                   :   Constructor to initialise data member of both base and
                                                                   derived classes
                                 void area()                    :  To calculate area of square
                                 void display()                 :  To display side of the square and area respectively
                                 Also define the main method to create object of the derived class to implement the above.

                   1       abstract class Side //abstract base class
                   2       { int s;

                   3         Side(int ss) // constructor
                   4         { s=ss;}

                   5         abstract void area(); // abstract method
                   6         void display() // non abstract method

                   7         { System.out.println("Side of the square="+s+" cm");}
                   8       }

                   9       class Area extends Side
                   10      { int a;

                   11        Area(int ss)
                   12        { super(ss); //invoking super class constructor

                   13          a=0;}
                   14        void area() // implementaion of abstract method of base class

                   15        {  a=s*s;}
                   16        void display()

                   17        {   super.display();
                   18            System.out.println("Area ="+a+" cm^2");
                   19          }

                   20        public static void main(int sz)




                                                                                                                       467
                                                                                  Inheritance, Interfaces and Polymorphism  467
   464   465   466   467   468   469   470   471   472   473   474