We have so far covered some of the most important topics in Java. I hope you guys enjoyed them a lot. In the previous article, we discussed ways to initialize instance variables and static variables through some blocks. If you missed reading that article, you can read here again=> Instance Initializer block in Java. Well, today we are going to discuss one of the basic but important topics of Java, that is, Covariant Return type, Static binding, and Dynamic binding in Java.
Let’s start with the Covariant Return Type.
What is the Covariant Return Type in Java?
When the return type of overriding method is in the direction of child class then it is called Covariant Return Type.
Some of the important points related to Covariant Return Type are as below:
- It became possible after Java 5
- Java restricts return type based overloading
- JVM permits return type based overloading
Sample Program on Covariant Return Type
package Test; class ParentClass{ ParentClass getObject(){ return this; } } public class ChildClass extends ParentClass { ChildClass getObject(){ return this; } void display(){ System.out.println("This is Covariant return type."); } public static void main(String[] args) { new ChildClass().getObject().display(); } }
What is binding in Java?
Binding in general means that establishing a connection between a method call and method body.
Types of binding in Java
There are mainly two types of binding in Java; those are listed as below:
- Static Binding
- Dynamic Binding
Static binding is also known as ‘Early binding’ and Dynamic binding is known as ‘Late binding’.
How do we determine the type of object in Java?
Variable Type
float num = 2.0;
The above num variable is a type of float.
Reference Type
class Cat{ public static void main(String args[]){ Cat cObj; } }
Above cObj is a type of Cat.
Object Type
class Mammal{ System.out.println("Mammal Class"); } class Cat extends Mammal{ public static void main(String args[]){ Cat cObj=new Cat(); } }
Above cObj is an instance of Cat class; along with the instance of Mammal class. This is applicable in the case of Inheritance.
Static Binding in Java
When there is the presence of a private, static, and final keyword in any method of the class is indicated as having Static binding; hence, the Static binding of any object of the class is determined by the compiler, therefore, it is defined at compile time.
Sample Example
class Cat{ private void mew(){ System.out.println("cat is mewing..."); } public static void main(String args[]){ Cat cObj=new cat(); cObj.mew(); } }
Dynamic Binding in Java
When the type of the object is defined at runtime then it is called dynamic binding in Java.
Sample Example
class Mammal{ void walk(){ System.out.println("mammal is walking..."); } } class Cat extends Mammal{ void walk(){ System.out.println("cat is walking..."); } public static void main(String args[]){ Mammal m=new Cat(); m.walk(); } }
I think you got the idea of Covariant return type, Static and Dynamic binding in Java. Feel free to ask your doubts by using the form below. Don’t miss to join our Facebook group on Automation. Till then take care and be safe from Corona. Happy Learning and Please spread the knowledge.