overridding in java help me to understand the below code.

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..

Replies

  • Neeraj Sharma
    Neeraj Sharma
    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.
  • zion
    zion
    thanx..😀
  • kamba giri
    kamba giri
    nice usage of super class... thanx for your program #-Link-Snipped-#

You are reading an archived discussion.

Related Posts

can you mail me a list of questions in civil engg so that i can prepare for an interview? regards, gopal
Hello world, I'd give you more about who I am, but honestly? You probably don't care all too much. Well,to begin with...I am an engineering graduate with majors in ELectrical...
In an enhancement of a design of a CPU, the speed of a floating point until has been increased by 20% and the speed of a fixed point unit has...
😁 i Am from India, This is my second year in graduation,I am perfect in networking,programming,webscripting(CSS,PHP,HTML,XML),C#,C,JAVA,Python,VB,SQL but not in C++,i have done a Three year Computer Diploma Course from NIIT.I...
how we know the owner of mobile no. on it's mobile no.