<p>What is the difference between early and late binding?</p>

[{'answer_id': 10581, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 3, 'selected': False, 'text': '<p>Taken directly from <a href="http://word.mvps.org/fAQs/InterDev/EarlyvsLateBinding.htm" rel="nofollow noreferrer">http://word.mvps.org/fAQs/InterDev/EarlyvsLateBinding.htm</a></p>\n\n<blockquote>\n  <p>There are two ways to use Automation (or OLE Automation) to\n  programmatically control another application.</p>\n  \n  <p>Late binding uses CreateObject to create and instance of the\n  application object, which you can then control. For example, to create\n  a new instance of Excel using late binding:</p>\n\n<pre><code> Dim oXL As Object\n Set oXL = CreateObject("Excel.Application")\n</code></pre>\n  \n  <p>On the other hand, to manipulate an existing instance of Excel (if\n  Excel is already open) you would use GetObject (regardless whether\n  you\'re using early or late binding):</p>\n\n<pre><code> Dim oXL As Object\n Set oXL = GetObject(, "Excel.Application")\n</code></pre>\n  \n  <p>To use early binding, you first need to set a reference in your\n  project to the application you want to manipulate. In the VB Editor of\n  any Office application, or in VB itself, you do this by selecting\n  Tools + References, and selecting the application you want from the\n  list (e.g. “Microsoft Excel 8.0 Object Library”).</p>\n  \n  <p>To create a new instance of Excel using early binding:</p>\n\n<pre><code> Dim oXL As Excel.Application\n Set oXL = New Excel.Application\n</code></pre>\n  \n  <p>In either case, incidentally, you can first try to get an existing\n  instance of Excel, and if that returns an error, you can create a new\n  instance in your error handler.</p>\n</blockquote>\n'}, {'answer_id': 10582, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 4, 'selected': False, 'text': '<p>In compiled languages, the difference is stark.</p>\n\n<p>Java:</p>\n\n<pre class="lang-java prettyprint-override"><code>//early binding:\npublic create_a_foo(*args) {\n return new Foo(args)\n}\nmy_foo = create_a_foo();\n\n//late binding:\npublic create_something(Class klass, *args) {\n  klass.new_instance(args)\n}\nmy_foo = create_something(Foo);\n</code></pre>\n\n<p>In the first example, the compiler can do all sorts of neat stuff at compile time.  In the second, you just have to hope that whoever uses the method does so responsibly.  (Of course, newer JVMs support the <code>Class&lt;? extends Foo&gt; klass</code> structure, which can greatly reduce this risk.)</p>\n\n<p>Another benefit is that IDEs can hotlink to the class definition, since it\'s declared right there in the method.  The call to create_something(Foo) might be <em>very</em> far from the method definition, and if you\'re looking at the method definition, it might be nice to see the implementation.</p>\n\n<p>The major advantage of late binding is that it makes things like inversion-of-control easier, as well as certain other uses of polymorphism and duck-typing (if your language supports such things).</p>\n'}, {'answer_id': 10584, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': "<p>In interpreted languages, the difference is a little more subtle.</p>\n\n<p>Ruby:</p>\n\n<pre><code># early binding:\ndef create_a_foo(*args)\n  Foo.new(*args)\nend\nmy_foo = create_a_foo\n\n# late binding:\ndef create_something(klass, *args)\n  klass.new(*args)\nend\nmy_foo = create_something(Foo)\n</code></pre>\n\n<p>Because Ruby is (generally) not compiled, there isn't a compiler to do the nifty up-front stuff.  The growth of JRuby means that more Ruby is compiled these days, though, making it act more like Java, above.</p>\n\n<p>The issue with IDEs still stands: a platform like Eclipse can look up class definitions if you hard-code them, but cannot if you leave them up to the caller.</p>\n\n<p>Inversion-of-control is not terribly popular in Ruby, probably because of its extreme runtime flexibility, but Rails makes great use of late binding to reduce the amount of configuration necessary to get your application going.</p>\n"}, {'answer_id': 10587, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 7, 'selected': True, 'text': '<p>The short answer is that early (or static) binding refers to compile time binding and late (or dynamic) binding refers to runtime binding (for example when you use reflection).</p>\n'}, {'answer_id': 30407708, 'author': 'Pancuz Kumar', 'author_id': 4264224, 'author_profile': 'https://Stackoverflow.com/users/4264224', 'pm_score': 3, 'selected': False, 'text': '<p>Similar but more detailed answer from Herbert Schildt C++ book:-</p>\n\n<p>Early binding refers to events that occur at compile time. In essence, early binding occurs when all information needed to call a function is known at compile time. (Put differently, early binding means that an object and a function call are bound during compilation.) Examples of early binding include normal function calls (including standard library functions), overloaded function calls, and overloaded operators. The main advantage to early binding is efficiency. Because all information necessary to call a function is determined at compile time, these types of function calls are very fast.</p>\n\n<p>The opposite of early binding is late binding. Late binding refers\nto function calls that are not resolved until run time. Virtual functions are used to achieve late binding. As you know, when access is via a base pointer or reference, the virtual function actually called is determined by the type of object pointed to by the pointer. Because in most cases this cannot be determined at compile time, the object and the function are not linked until run time. The main advantage to late binding is flexibility. Unlike early binding, late binding allows you to create programs that can respond to events occurring while the program executes without having to create a\nlarge amount of "contingency code." Keep in mind that because a function call is not resolved until run time, late binding can make for somewhat slower execution times.\nHowever today, fast computers have significantly reduced the execution times related to late binding.</p>\n'}, {'answer_id': 34935823, 'author': 'JBOND', 'author_id': 5823364, 'author_profile': 'https://Stackoverflow.com/users/5823364', 'pm_score': 0, 'selected': False, 'text': '<pre><code>public class child()\n{    public void method1()\n     {     System.out.println("child1");\n     }\n    public void method2()\n     {     System.out.println("child2");\n     }\n\n}\npublic class teenager extends child()\n{    public void method3()\n     {      System.out.println("teenager3");\n     }\n}\npublic class adult extends teenager()\n{     \n    public void method1()\n    {    System.out.println("adult1);\n         super.method1();\n     }\n}\n\n\n//In java\npublic static void main(String []args)\n{    ((teenager)var).method1();\n}\n</code></pre>\n\n<p>This will print out</p>\n\n<pre><code>adult1\nchild1\n</code></pre>\n\n<p>In early binding the compiler will have access to all of the methods \nin child and teenager\nbut in late binding (at runtime), it will check for methods that are overridden\nat runtime.</p>\n\n<p>Hence method1(from child -- early binding) will be overridden by the method1 from adult at runtime(late binding)\nThen it will implement method1 from child since there is no method1 in method1 in teenager.</p>\n\n<p>Note that if child did not have a method1 then the code in the main would not compile.</p>\n'}, {'answer_id': 43421909, 'author': 'Ghousepeer', 'author_id': 7869967, 'author_profile': 'https://Stackoverflow.com/users/7869967', 'pm_score': 0, 'selected': False, 'text': '<p>The compile time polymorphism also called as the overloading or early binding or static binding when we have  the same method name with different behaviors. By implementing the multiple prototype of the same method and different behavior occurs in it. Early binding refers first compilation of the program .\nBut in late binding object is runtime occurs in program. Also called as Dynamic binding or overriding or Runtime polymorphism.</p>\n'}, {'answer_id': 66262725, 'author': 'Michu93', 'author_id': 4952262, 'author_profile': 'https://Stackoverflow.com/users/4952262', 'pm_score': 0, 'selected': False, 'text': '<p>The easiest example in java:</p>\n<p>Early (static or overloading) binding:</p>\n<pre><code>public class Duck {\n    public static void quack(){\n        System.out.println(&quot;Quack&quot;);\n    }\n}\n\npublic class RubberDuck extends Duck {\n    public static void quack(){\n        System.out.println(&quot;Piiiiiiiiii&quot;);\n    }\n}\n\npublic class EarlyTest {\n    public static void main(String[] args) {\n        Duck duck = new Duck();\n        Duck rubberduck = new RubberDuck();\n\n        duck.quack();\n        rubberduck.quack(); //early binding - compile time\n    }\n}\n</code></pre>\n<p>Result is:</p>\n<pre><code>Quack\nQuack\n</code></pre>\n<p>while for Late (dynamic or overriding) binding:</p>\n<pre><code>public class Duck {\n    public void quack(){\n        System.out.println(&quot;Quack&quot;);\n    }\n}\n\npublic class RubberDuck extends Duck {\n    public void quack(){\n        System.out.println(&quot;Piiiiiiiiii&quot;);\n    }\n}\n\npublic class LateTest {\n    public static void main(String[] args){\n        Duck duck = new Duck();\n        Duck rubberduck = new RubberDuck();\n\n        duck.quack();\n        rubberduck.quack(); //late binding - runtime\n    }\n}\n</code></pre>\n<p>result is:</p>\n<pre><code>Quack\nPiiiiiiiiii\n</code></pre>\n<p>Early binding happens in compile time, while late binding during runtime.</p>\n'}]
['https://Stackoverflow.com/questions/10580', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']