Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Dynamic Binding

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Dynamic Binding

      Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.

      Java Dynamic Binding

      Dynamic binding refers to the process in which linking between method call and method implementation is resolved at run time (or, a process of calling an overridden method at run time). Dynamic binding is also known as run-time polymorphism or late binding. Dynamic binding uses objects to resolve binding.

      Characteristics of Java Dynamic Binding

      • Linking − Linking between method call and method implementation is resolved at run time.
      • Resolve mechanism − Dynamic binding uses object type to resolve binding.
      • Example − Method overriding is the example of Dynamic binding.
      • Type of Methods − Virtual methods use dynamic binding.

      Example of Java Dynamic Binding

      In this example, we've created two classes Animal and Dog where Dog class extends Animal class. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.
      package com.tutorialspoint;
      
      class Animal {
      public void move() {
      System.out.println("Animals can move");
      }
      }
      
      class Dog extends Animal {
      public void move() {
      System.out.println("Dogs can walk and run");
      }
      }
      
      public class Tester {
      
      public static void main(String args[]) {
      Animal a = new Animal(); // Animal reference and object
      // Dynamic Binding
      Animal b = new Dog(); // Animal reference but Dog object
      
      a.move(); // runs the method in Animal class
      b.move(); // runs the method in Dog class
      }
      }

      Output

      Animals can move
      Dogs can walk and run
      In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.
      Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.

      Java Dynamic Binding: Using the super Keyword

      When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding.

      Example: Using the super Keyword

      In this example, we've created two classes Animal and Dog where Dog class extends Animal class. Dog class overrides the move method of its super class Animal. But it calls parent move() method using super keyword so that both move methods are called when child method is called due to dynamic binding. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.
      class Animal {
      public void move() {
      System.out.println("Animals can move");
      }
      }
      
      class Dog extends Animal {
      public void move() {
      super.move(); // invokes the super class method
      System.out.println("Dogs can walk and run");
      }
      }
      
      public class TestDog {
      
      public static void main(String args[]) {
      Animal b = new Dog(); // Animal reference but Dog object
      b.move(); // runs the method in Dog class
      }
      }

      Output

      Animals can move
      Dogs can walk and run