Coders Packet

How to use Dynamic Binding In Java

By Akash Ramesh Ramkar

In this tutorial, we are learning how we can implement dynamic binding in our java program by the concept of method overriding.

What Actually Dynamic Binding is?

Dynamic Binding is one of the Important Concept in Object Oriented Programming Paradigm. Dynamic Binding let you help to execute same method lately at the the compile time.

How to Implement Dynamic Binding in Java?

Firstly we need to create an abstract class. After that we need to define abstract methods inside it so that it can be override by the class that going to extend it. Here we are defining abstract class Base.

abstract class Base {
    double l, b, area;

    void setDimentions() {
        double length, height;
        System.out.println(" Enter the length : ");
        Scanner s = new Scanner(;
        length = s.nextDouble();
        System.out.println(" Enter the height : ");
        height = s.nextDouble();
        this.l = length;
        this.b = height;

    abstract void computeArea();    //this method is Abstract

here in this class we defined a setDimentions Function which setting length and breadth.and now we have to extend our Base class with our Triangle And Rectangle classes so that we can access base class members and methods.


class Triangle extends Base {
     void computeArea() {
        area = (l * b) / 2;
        System.out.println("Area of triangle is : " + this.area);


 Here we have overridden computeArea() method so that we can calculate the area of Triangle.

similarly we have to do it for Rectangle class so that we can calculate area of Rectangle by same computeArea() method.

class Rectangle extends Base {
void computeArea() {
area = (l * b);
System.out.println("Area of rectangle is : " + this.area);


Now we have to create object of our Triangle and Rectangle class so that we can access its methods and members .


public class DynamicBInding {
    public static void main(String[] args) {
        Triangle t = new Triangle();
        Rectangle r = new Rectangle();
        System.out.println("calculating Area of Triangle");
        t.setDimentions();  //setting values of l and b for triangle
        System.out.println("calcularing Area of Rectangle");
        r.setDimentions();  ///setting values of l and b for rectangle


Here you can see we have used same method computeArea() to calculate two different formules using dynamic binding by overriding method defined in abstract class Base. 



Download Complete Code


No comments yet