Tuesday, 23 January 2018

Write a simple java application that creates a Shape class with two double data members. The class should have area methods to calculate the area of shape. Inherit two classes Rectangle and Triangle from Shape class. Demonstrate method overriding & super keyword.



Program:-

class shape
{
            double h,w,ar;
            void get(double x,double y)
            {
                        h=x;
                        w=y;
            }
            void area()
            {
                        ar=w*h;
            }
}

class rectangle extends shape
{
            void rect_area()
            {
                        System.out.println("Area of rectanle is: "+ar);
            }
           
}

class triangle extends shape
{
            void area()
            {
                        super.area();
                        System.out.println("Area of Triangle is: "+(0.5*ar));
            }
}

class u42
{
            public static void main(String args[])
            {
                        rectangle r = new rectangle();
                        r.get(5.0,6.0);
                        r.area();
                        r.rect_area();
                       
                        triangle t = new triangle();
                        t.get(6.0,7.0);
                        t.area();
            }
}

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...