• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static binding with function overloading in java and memory space use with function overload ?

 
shyam ji gautam
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class OverloadDemo
{
void triangleArea(float base, float height)
{
float area;
area = base * height / 2.0f;
System.out.println(“Area = “ + Area);
}

void triangleArea(float side1, float side2, float side3)
{
float area,s;
s = (side1 + side2 + side3) / 2.0;
area = Math.sqrt(s*(s-side1) * (s-side2) * (s-side3) );
System.out.println(“Area = “ + area);
}
}



class MainOverloadDemo
{
public static void main(String args[])
{
OverloadDemo ovrldDemo = new OverloadDemo();
ovrldDemo.triangleArea(20.12,58.36);

ovrldDemo triangleArea(63.12,54.26,95.24);
}
}

my first question is
is it static binding or not ?
according to me here compiler is aware at compile time that need to call void triangleArea(float base, float height)
Because no of parameter different and the JVM invokes void triangleArea(float base, float height) in the class OverloadDemo at run time


but i also knows that All the instance method calls are always resolved at runtime, and here void triangleArea(float base, float height) is also a instance method so i am confuse which statement is right ?


and third doubt is that when we use method overloading then memory space uitilization is improve how with refernce to java ?

 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shyam ji gautam wrote:my first question is

Shyman: Please UseCodeTags (←click). I've also locked your other (duplicate) thread.

Winston
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

When you are compiling your code, compiler is statisfied by comparing the method calls with the
method definition ,this is static binding.

When your are running this code JVM will decide which method is called based upon the number
and datatype of parameters to arguments, this is dynamic binding.

method overloading is not used from the prospective of memory improvement, it fits in different usage
like sum can be operated on two integers also and two floats also So more towards funtionality.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using float arithmetic? Avoid it as much as possible; use doubles, or (if possible) BigDecimal. I don’t think you can use BigDecimal here, however.
Yes, you are overloading. Yes, you can call that static binding, though maybe compile‑time binding is a better term. You can actually see what is happening by compiling those classes and inspecting their bytecode.You should see which methods are called, and that the compiler has chosen which method to invoke. They will be different.
You can do the same for overridingNow create Cat, Dog and Horse classes, like this, and override the eat() methodThat is runtime binding. If you print the bytecode, you will see all those calls are the Animal#eat() method. But when you execute that code, you will see the Doc Cat or Horse versions are actually called. That is how you implement polymorphism.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shyam ji gautam wrote:
my first question is
is it static binding or not ?
according to me here compiler is aware at compile time that need to call void triangleArea(float base, float height)
Because no of parameter different and the JVM invokes void triangleArea(float base, float height) in the class OverloadDemo at run time

Yes it is static binding for overloaded methods.
shyam ji gautam wrote:
but i also knows that All the instance method calls are always resolved at runtime, and here void triangleArea(float base, float height) is also a instance method so i am confuse which statement is right ?

No. Overloaded methods are resolved at compile time.

shyam ji gautam wrote:
and third doubt is that when we use method overloading then memory space uitilization is improve how with refernce to java ?

in which way???
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
subhash kumar wrote: . . . When your are running this code JVM will decide which method is called based upon the number and datatype of parameters to arguments, this is dynamic binding. . . .
Gaurangkumar Khalasi has correctly answered that point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic