Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it dynamic binding?

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey here is one program


abstract class A
{

abstract void abc();
void xyz(){}
}


class B extends A
{

abstract void abc(){}

}

class Test{
public static void main(String args[])
{
B b =new B();

b.abc();
b.xyz();//DYNAMIC BINDING
}
}



so yesterday one senior software is saying that by calling b.xyz(); the concept is dynamic binding

but i told him that it is a inheritence concept but he is not satisfied at my answer


how can it be a dynamic binding here when i am calling the method b.xyz(); it will be checked at compile time ot at runtime am i right ???


please tell me your views
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A b =new B();

b.abc(); // IS dynamic binding
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The language *is* using dynamic binding to call the method, even though it wouldn't be necessary in this special case.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so yesterday one senior software is saying that by calling b.xyz(); the concept is dynamic binding

but i told him that it is a inheritence concept but he is not satisfied at my answer

You make it sound as if these are two mutually exclusive concepts. They are not. Ofcourse your example code involves inheritance. Java uses dynamic binding to find out which version of a method to call - the version in the superclass or the version in the subclass.

If you do this:

B object = new B();
object.xyz();

then dynamic binding is not really necessary, because the compile-time type of the object is the same as the dynamic (runtime) type of the object. However, if you do this:

A object = new B();
object.xyz();

then the compile-time type of the object (A) is different from the dynamic type (B) - and Java has to determine at runtime (not at compile time) that it should call method xyz() in class B instead of class A. That's dynamic binding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic