Tuesday, 23 January 2018

Write a simple java application that creates a Shape class with two double data members. The class should have one abstract method area to calculate the area of shape. Inherit two classes Rectangle and Triangle from Shape class. Demonstrate run time polymorphism.


Program:-
abstract class shape
{
            double x,y;
            void get(double h,double w)
            {
                        x=h;
                        y=w;
            }
            abstract void area();
}

class rectangle extends shape
{
            void area()
            {
                        System.out.println("Area of rectanle is: "+(x*y));
            }
           
}

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

class u43
{
            public static void main(String args[])
            {
                        shape s;
                       
                        s=new rectangle();
                        s.get(5.0,6.0);
                        s.area();
                       
                        s=new triangle();
                        s.get(5.0,6.0);
                        s.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...