Dynamic Binding in Java
“Dynamic” means “run time”, and “binding” means “association”. So the term dynamic binding indicates run time association of objects by java virtual machine. Here we will see how Java achieves dynamic binding in run time, which means before the code’s final running but after compilation.
Syntax: For dynamic binding in Java, you should follow the basic syntax of java with annotations. You may use @Override annotation here to point out which method we want to override specifically.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsHow Dynamic Binding Works in Java?
Runtime polymorphism works in Java by method overriding. Method overriding happens when objects have the same method name and arguments and type as of their parent class but with different functionality. If a child class has that type of method in it, we call it an overridden method.
Why is it called dynamic binding?
Reason being named so, due to the fact that the functionality of the method is dynamically decided in run time as per the object by JVM. It is also referred to as “Run time Polymorphism”. when we call an overridden method of child class through its parent type reference (this phenomenon in java is referred to as “Upcasting”), then the type of the object indicates which method or functionality will be invoked. Making of this decision happens during runtime by JVM after the compilation of code. Hence it is called run time polymorphism. It is also called “Late binding”, because binding of method and object, which means the functionality of which object’s method will be displayed, is decided late, i.e. after compilation.
Rules Regarding Dynamic Binding
- Methods or functions of child and parent class must have the same name.
- Methods or functions of child and parent class must have the same parameter.
- The inheritance relationship is mandatory (IS-A relationship).
Limitations in Dynamic Binding
- You cannot override the private methods of a parent class.
- You cannot override Final methods.
- You cannot override static methods.
Examples to Implement Dynamic Binding
We will discuss some code examples of Dynamic Binding here:
Example #1
In this example, we will show how the method locate () is displaying different messages depending on which type of object it is associated with. When it is associated with the “Continent” type, it is showing messages from a parent class. When it is associated with the “SubContinent” type, it shows messages from the child class.
Code:
class Continent { public void locate () { System.out.println("We are in Continent"); } } class SubContinent extends Continent { @Override public void locate () { System.out.println("We are in SubContinent"); } } public class DynamicBinding { public static void main(String args[]) { Continent superObject = new Continent (); superObject.locate(); //method of super class or parent class is called SubContinent subObject = new SubContinent (); // upcasting subObject.locate();//method of sub class or child class is called by Parent reference, this is called "Dynamic Binding" SubContinent subObject2 = new SubContinent (); subObject2.locate(); //method of sub class or child class is called } }
Output:
Example #2
Let us take an example of dynamic binding in the case of multilevel inheritance. In this example, we have two levels of inheritance is taken into account. In this example, we will show how the method identifies () is displaying different messages depending on which type of object it is associated with. When it is associated with the “Computer” type, it is showing messages from a parent class. When it is associated with the “Desktop” type, it shows messages from its child class. Again in the second level of inheritance, when associated with the “Laptop” type, it shows messages from its child class of its parent, the “Desktop” class.
Code:
class Computer { void identify() { System.out.println("This is Computer"); } } class Desktop extends Computer { void identify (){ System.out.println("This is Desktop"); } } class Laptop extends Desktop { void identify (){ System.out.println("This is Laptop"); } } public class DynamicBinding { public static void main(String args[]){ Computer superObject=new Computer (); Computer subObject=new Desktop (); // // upcasting : first level of heritance Computer babyObject=new Laptop (); // // upcasting : second level of heritance superObject.identify (); subObject.identify (); //run time polymorphism happening in first level of heritance babyObject.identify (); //run time polymorphism happening in second level of heritance } }
Output:
Example #3
Let us take another example of run time polymorphism in the case of multilevel inheritance. In this example, we have three levels of inheritance is taken into account. In this example, we will show how the method feature () is displaying different features depending on which type of object it is associated with. When it is associated with the “Cosmetics” type, it is showing messages from a parent class. When it is associated with the “Perfume” type, it shows messages from its child class. Again in the second level of inheritance, when associated with the “Deo” type, it shows messages from its child class of its parent, the “Perfume” class. Again in the third level of inheritance, when associated with the “DeoStick” type, it shows messages from its child class of its parent, the “Deo” class.
Code:
class Cosmetics{ void feature() { System.out.println("Cosmetics are expensive"); } } class Perfume extends Cosmetics { void feature(){ System.out.println("Perfume is soothing"); } } class Deo extends Cosmetics { void feature(){ System.out.println("Deo is sometimes better than perfume"); } } class DeoStick extends Deo{ void feature(){ System.out.println("DeoStick is very handy"); } } public class RunTimePolymorphism { public static void main(String args[]){ Cosmetics superObject=new Cosmetics (); Cosmetics subObject=new Perfume(); // child object type : first level of heritance Cosmetics sub2Object=new Deo(); // child object type : second level of heritance Cosmetics sub3Object=new DeoStick(); // child object type : third level of heritance superObject.feature(); subObject.feature(); //run time polymorphism happening in first level of heritance sub2Object.feature(); //run time polymorphism happening in second level of heritance sub3Object.feature(); //run time polymorphism happening in third level of heritance } }
Output:
Conclusion
This concludes our learning of the topic “Dynamic Binding in Java”. Write yourself the codes mentioned in the above examples in the java compiler and verify the output. Learning of codes will be incomplete if you will not write code by yourself.
The above is the detailed content of Dynamic Binding in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
