CrazyEngineers
  • overridding in java help me to understand the below code.

    zion

    Member

    Updated: Oct 26, 2024
    Views: 956
    its a program of inheritance and overridding i understood the whole code except(that line is written in bold and i have underlined that line .It is in class area ) as i could not figure how is it useful and its not creating an object then what does it mean
    class fig
    {
    int num1;
    int num2;
    fig(int a, int b)
    {
    num1= a;
    num2= b;
    }
    int area()
    {
    System.out.println("area of fig ");
    return 0;
    }
    }
    class rect extends fig
    {
    rect(int a, int b)
    {
    super(a,b);
    }
    int area()
    {
    System.out.println(" area of rect " );
    return num1*num2;
    }
    }
    class triangle extends fig
    {
    triangle(int a,int b)
    {
    super(a,b);
    }
    int area()
    {
    System.out.println( " area of triangle ");
    return num1*num2/2;
    }
    }
    class area
    {
    public static void main(String args [])
    {
    fig obj= new fig(2,3);
    rect obj1 = new rect(4,5);
    triangle obj2 = new triangle(7,8);
    fig fiig;
    fiig = obj;
    System.out.println( fiig.area());
    fiig = obj1;
    System.out.println( fiig.area());
    fiig = obj2;
    System.out.println( fiig.area());
    }
    }
    //fiig is created in reference sort of something with fig i cant figure out what does fig fiig; means in above lines and how is it useful in program can it be useful in other places..
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Neeraj Sharma

    MemberMay 31, 2012

    fiig is basically deciding which method to execute at runtime... This is also called as DYNAMIC METHOD DISPATCH in java.
    the above code can also be written as
    fig fiig=new rect(4,5);
    in this case fiig.area() executes the area method of rect class.
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 31, 2012

    thanx..😀
    Are you sure? This action cannot be undone.
    Cancel
  • kamba giri

    MemberJun 5, 2012

    nice usage of super class... thanx for your program #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register