The difference between reflection and late binding in Java with real-time examples

While studying Java tutorials, Reflection and Late Binding confused me. In some textbooks, they wrote that they are the same, and that there is no difference between Reflection and Late Binding. But other textbooks say there is a difference.

I'm confused, so someone can explain what Reflection and Late Binding are in Java, and if possible, please give me some examples in the real world.

Thanks..

+6
source share
5 answers

Late binding (also known as dynamic sending) does not need to be analyzed - it still needs to know which member is dynamically bound to at compile time (i.e. the signature of the element is known at compile-time), although binding to overridden members occurs during fulfillment.

When you reflect, you don’t even know which member you are using (even the name is not known at compile time, not to mention the signature) - everything happens at run time, so it is much slower.

+2
source

Java uses late binding to support polymorphism; which means that the decision about which of the many methods should be used is deferred to runtime.

Take the case of N classes that implement an abstract interface method (or abstract class, fwiw).

public interface IMyInterface { public void doSomething(); } public class MyClassA implements IMyInterface { public void doSomething(){ ... } } public class MyClassB implements IMyInterface { public void doSomething(){ ... } } public class Caller { public void doCall(IMyInterface i){ // which implementation of doSomething is called? // it depends on the type of i: this is late binding i.doSomething(); } } 

Reflection is used instead of describing code that is capable of checking other code, i.e. Know what methods or attributes are available in a class, call a method (or load a class) by name, and do many very interesting things at run time.

A very nice explanation of reflection here: What is reflection and why is it useful?

+3
source

Real world examples:

If you create your project using jdesktop 0.8, but submit it with jdesktop 0.9, your code will still use functions 0.9 because it takes advantage of late binding, that is, the code that calls your code is the version the class is loading , regardless of the version with which it was compiled. (This is in contrast to linkers, which insert a version of code with code at compile time into the application.)

For reflection, suppose you are trying to target Java 1.5 and 1.6, but want to use the tab components in 1.6, if available, then check their presence using reflection in the JTabbedPane class to find setTabComponentAt . In this case, you are creating against Java 1.5, which does not have these functions at all, so you cannot call them directly or compile the compilation. However, if you find yourself working against 1.6 on the end-user system (the last action comes into play here), you can use reflection to invoke methods that weren't in 1.5.

They are connected; many applications of reflection rely on late binding to be useful, but they are fundamentally different aspects of the language and its implementation.

+2
source

One of the important issues discussed in Late Binding is polymorphism, that is, calling the appropriate override method along the hierarchy of your class is determined at run time, not at compile time. Reflection is a function of collecting and processing information about your objects at runtime. For instance. you can get all the attributes or method names of an object using its "Class" attribute at runtime, and call these methods or manage its attributes.

In the following code, you can dynamically create a new object using reflection (see how the constructor is retrieved and accessed using the class instead of just using something like obj = new MyClass ("MyInstance")). Similarly, you can access other design forms, methods, and attributes. For more information about reflection in a java visit: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

 ... in some method of some class ... Class c = getClass(); Constructor ctor = c.getConstructor( String.class ); Object obj = ctor.newInstance( "MyInstance" );
... in some method of some class ... Class c = getClass(); Constructor ctor = c.getConstructor( String.class ); Object obj = ctor.newInstance( "MyInstance" ); 
+1
source

I disagree with most of the answers here -

Everyone calls what Java does in terms of nulling when implementing a method at runtime as late binding, but in my opinion, it is incorrect to use the term late binding for what Java does.

The last binding implies absolutely no method invocation checks during compilation and compilation errors if the method does not exist.

Java, however, will throw a compilation error if the method does not exist somewhere in the type hierarchy of the type qualifying the method call (being somewhat approximate when describing the behavior here). This is not pure traditional late binding. What Java does in a regular non-private non final non static method call is better called dynamic dispatch.
However, if we use reflection in Java, then Java performs a clean final binding, as the compiler simply cannot check whether the called method exists or not. Here is an example:

 class A { public void foo() { System.out.println("Foo from A"); } } class B extends A { public void foo() { System.out.println("Foo from B"); } } public class C { public static void main(String [] args) { A a=new A(); B b=new B(); A ref=null; Class ref1 = null; ref1 = b.getClass(); ref.foo1();//will not compile because Java in this normal method //call does some compile time checks for method and method //signature existence. NOT late binding in its pure form. try { ref1.getMethod("foo1").invoke(null); //will throw a //NoSuchMethodException at runtime, but compiles perfectly even //though foo1 does not exist. This is pure late binding. } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
0
source

Source: https://habr.com/ru/post/906744/


All Articles