Suppose you have 3 Java classes: A, B, and C. Class B extends A, and class C extends B. Think of the class inheritance hierarchy alphabetically – with A at the top, then B in the middle, and finally C on the bottom. All 3 classes implement the instance method void doIt(). A reference variable is instantiated as "A x = new B();" and then x.doIt() is executed. What version of the doIt() method is actually executed and why? | |
Yikes! This a long question, and could be confusing when you see it the first time – but it’s really not that bad at all. The best way to solve something like this is to write out the actual code. With that in mind here is what the Java code would look like:
class A
{
public doIt( )
{
//this does something
}
}
class B extends A
{
public doIt( )
{
//this does something
}
}
class C extends B
{
public doIt( )
{
//this does something
}
}
public static void main(String[] args) {
// this is a legal Java statement:
A x = new B( );
/*
What version of the doIt( ) method
will get executed by the statement below
- the one that belongs to class A, B, or C?
*/
x.doIt( );
}
So, given the code above the question is which version of the doIt( ) method will be executed – the one in class A, B, or C?
The statement that causes a lot of confusion is the “A x = new B();” statement. What exactly is going on here? Well, although the variable x is an object of type A, it is instantiated as an object of class B – because of the “= new B( );” part of the statement. The Java runtime will basically look at this statement and say “even though x is clearly declared as type A, it is instantiated as an object of class B, so I will run the version of the doIt() method that is defined in class B.”
Understanding the example of dynamic binding in the code above
The version of the doIt() method that’s executed by the object x is the one in class B because of what is known as dynamic binding in Java – the code above can be considered to be an example of dynamic binding. Dynamic binding basically means that the method implementation that is actually called is determined at run-time, and not at compile-time. And that’s why it’s called dynamic binding – because the method that will be run is chosen at run time. Dynamic binding is also known as late binding.
|
Subscribe to:
Post Comments (Atom)
social counter
Popular Posts
-
/* Pyramid 5 Example This Java Pyramid example shows how to generate pyramid or triangle like given below using for loop. 12345 1234 ...
-
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform...
-
First Way First of all you need to enable SSL for your server. For Tomcat you need to generate an openSSL keystore and add the follow...
-
There are a number of features in Java that have no equivalent in C++. Perhaps the three most important are multithreading, packages, and ...
-
There are some primary differences between http and https, however, beginning with the default port, which is 80 for http and 443 for ...
recent posts
category
popular
-
There are some primary differences between http and https, however, beginning with the default port, which is 80 for http and 443 for ...
-
First Way First of all you need to enable SSL for your server. For Tomcat you need to generate an openSSL keystore and add the follow...
-
Since objects are dynamically allocated by using the new operator, you might be wondering how such objects are destroyed and their memory ...
-
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform...
-
The increased security that HTTPS brings might suggest that it should be used as a matter of course, but there are drawbacks that must be ...
-
In case you need Oracle Java instead of OpenJDK in Ubuntu, here's how you can install it via a PPA: sudo add-apt-repository ppa:...
-
There are a number of features in Java that have no equivalent in C++. Perhaps the three most important are multithreading, packages, and ...
-
Following example demonstrates how to view current classpath using echo command. C:> echo %CLASSPATH% Result: The above code s...
-
This example fill (initialize all the elements of the array in one short) an array by using Array.fill(arrayname,value) method and Array...
-
Following example demonstrates how to set multiple classpath. Multiple class paths are separated by a semicolon. c:> java -classpath ...

No comments: