Static Binding and Dynamic Binding and Final Keyword

Last updated on Dec 29 2022
Prabhas Ramanathan

java 118

Connecting a method call to the method body is known as binding.
There are two types of binding
1. Static Binding (also known as Early Binding).
2. Dynamic Binding (also known as Late Binding).

java 119

Table of Contents

Understanding Type

Let’s understand the type of instance.

1) variables have a type

Each variable has a type, it may be primitive and non-primitive.
1. int data=30;
Here data variable is a type of int.
2) References have a type

1. class Dog{ 
2. public static void main(String args[]){ 
3. Dog d1;//Here d1 is a type of Dog 
4. } 
5. }

3) Objects have a type

An object is an instance of particular java class,but it is also an instance of its superclass.

1. class Animal{} 
2. 
3. class Dog extends Animal{ 
4. public static void main(String args[]){ 
5. Dog d1=new Dog(); 
6. } 
7. }

Here d1 is an instance of Dog class, but it is also an instance of Animal.

static binding

When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
Example of static binding

1. class Dog{ 
2. private void eat(){System.out.println("dog is eating...");} 
3. 
4. public static void main(String args[]){ 
5. Dog d1=new Dog(); 
6. d1.eat(); 
7. } 
8. }

Dynamic binding

When type of the object is determined at run-time, it is known as dynamic binding.
Example of dynamic binding

1. class Animal{ 
2. void eat(){System.out.println("animal is eating...");} 
3. } 
4. 
5. class Dog extends Animal{ 
6. void eat(){System.out.println("dog is eating...");} 
7. 
8. public static void main(String args[]){ 
9. Animal a=new Dog(); 
10. a.eat(); 
11. } 
12. }

 

Output:dog is eating…
In the above example object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal.So compiler doesn’t know its type, only its base type.

Final Keyword In Java

The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let’s first learn the basics of final keyword.

java 120

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can’t be changed because final variable once assigned a value can never be changed.

1. class Bike9{ 
2. final int speedlimit=90;//final variable 
3. void run(){ 
4. speedlimit=400; 
5. } 
6. public static void main(String args[]){ 
7. Bike9 obj=new Bike9(); 
8. obj.run(); 
9. } 
10. }//end of class

Output:Compile Time Error

2) Java final method

If you make any method as final, you cannot override it.
Example of final method

1. class Bike{ 
2. final void run(){System.out.println("running");} 
3. } 
4. 
5. class Honda extends Bike{ 
6. void run(){System.out.println("running safely with 100kmph");} 
7. 
8. public static void main(String args[]){ 
9. Honda honda= new Honda(); 
10. honda.run(); 
11. } 
12. }

 

Output:Compile Time Error

3) Java final class

If you make any class as final, you cannot extend it.
Example of final class

1. final class Bike{} 
2. 
3. class Honda1 extends Bike{ 
4. void run(){System.out.println("running safely with 100kmph");} 
5. 
6. public static void main(String args[]){ 
7. Honda1 honda= new Honda1(); 
8. honda.run(); 
9. } 
10. }

 

Output:Compile Time Error

 

Q) Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:

1. class Bike{ 
2. final void run(){System.out.println("running...");} 
3. } 
4. class Honda2 extends Bike{ 
5. public static void main(String args[]){ 
6. new Honda2().run(); 
7. } 
8. }

 

Output:running…

 

Q) What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is known as blank final variable.
If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in constructor.

Example of blank final variable

1. class Student{ 
2. int id; 
3. String name; 
4. final String PAN_CARD_NUMBER; 
5. ... 
6. }

Que) Can we initialize blank final variable?

Yes, but only in constructor. For example:

1. class Bike10{ 
2. final int speedlimit;//blank final variable 
3. 
4. Bike10(){ 
5. speedlimit=70; 
6. System.out.println(speedlimit); 
7. } 
8. 
9. public static void main(String args[]){ 
10. new Bike10(); 
11. } 
12. }

 

Output: 70

 

static blank final variable

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable

1. class A{ 
2. static final int data;//static blank final variable 
3. static{ data=50;} 
4. public static void main(String args[]){ 
5. System.out.println(A.data); 
6. } 
7. }

 

Q) What is final parameter?

If you declare any parameter as final, you cannot change the value of it.

1. class Bike11{ 
2. int cube(final int n){ 
3. n=n+2;//can't be changed as n is final 
4. n*n*n; 
5. } 
6. public static void main(String args[]){ 
7. Bike11 b=new Bike11(); 
8. b.cube(5); 
9. } 
10. }

 

Output: Compile Time Error

 

Q) Can we declare a constructor final?

No, because constructor is never inherited.

So, this brings us to the end of blog. This Tecklearn ‘Static Binding and Dynamic Binding and Final Keyword’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

.Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Static Binding and Dynamic Binding and Final Keyword"

Leave a Message

Your email address will not be published. Required fields are marked *