Pro - 6

Create a base class called shape. It contains two methods getxyvalue() and showxyvalue() for accepting co-ordinates and to display the same. Create the subclass called Rectangle which contains a method to display the length and breadth of the rectangle called showxyvalue().Use overriding concept. 

  1. class Shape
  2. {  
  3.        int x, y;
  4.        void getxyvalue()
  5.        {
  6.                  x = 10;
  7.                  y = 20;
  8.        }  
  9.        void showxyvalue()
  10.        {
  11.                   System.out.println("Inside Shape -> x = " +x + " y = "+y);
  12.        }  
  13. }  
  14.  
  15. class Rectangle extends Shape
  16. {  
  17.        int x=40, y=50;
  18.        void showxyvalue()
  19.        {
  20.                    System.out.println("Inside Rectangle -> x = " +x + " y = "  + y);
  21.        }  
  22. }  
  23. class Pro6
  24. {  
  25.           public static void main(String args[])
  26.         {  
  27.                     Shape s=new Shape();  
  28.                     Rectangle r=new Rectangle();  
  29.                     s.getxyvalue();
  30.                     System.out.println(s.showxyvalue(););  
  31.                     System.out.println(r.getxyvalue(););  
  32.           }  
  33. }  
Output:
Inside Shape -> x = 10     y = 20
Inside Rectangle -> x = 40   y = 50

No comments:

Post a Comment