Skip to content
Related Articles

Related Articles

Improve Article

Backwards Compatibility in a Software System with Systematic Reference to Java

  • Last Updated : 23 Jun, 2021
Geek Week

Here we will be discussing a way to achieve “Backwards Compatibility” while developing new features in any software system. So let us do a brief about backward compatibility in brief. Backward compatibility is a property of a system, product, or technology that allows for integration with an older legacy system or with an input designed for such a system. Modifying a system in a way that does not allow backward compatibility is called “Breaking Change.” While the current era of agile software development demands fast-paced implementation of features to cater to rapidly changing requirements, it is extremely necessary to have backward compatibility in the software being developed.

Here we will be discussing out how java has achieved backward compatibility while developing new features, which will serve as a potential reference while updating existing code. 

Now let us understand the very important concept of ‘widening‘ and how it is retained while developing new features like Autoboxing in Java 5. In a class that has overloaded methods, one of the compiler’s jobs is to determine which method to use whenever it finds an invocation for the overloaded method.

Example 1:

Java




// Java Program to Without Involving Concepts 
// of Java 5 Features
  
// Importing required classes
import java.io.*;
  
// Main class
// To test method overloading
class GFG {
  
    // Method 1
    static void print(int i)
    {
        // Print statement when method 1 is called
        System.out.println("int = " + i);
    }
  
    // Method 2
    static void print(long l)
    {
        // Print statement when method 2 is called
        System.out.println("long = " + l);
    }
  
    // Method 3
    static void print(double d)
    {
        // Print statement when method 3 is called
        System.out.println("double = " + d);
    }
  
    // Method 4
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;
  
        // Calling the methods
        print(b);
        print(s);
        print(l);
        print(f);
    }
}
Output



int = 5
int = 5
long = 5
double = 5.0

When a method is invoked with byte and the short arguments, the Java compiler implicitly widened it to match the appropriate version of the print() method that accepts an int argument. Further, as can be seen, when a print method is invoked with long, it uses respective versions of print method print(long l) and, the method call that uses a float is matched to the method that takes a double and hence, in turn, explaining the behavior of widening.


Note: When an exact match isn’t found, the JVM uses the method with the smallest possible argument that is wider than the parameter being passed.

Example 2: Here we will be having only one method that accepts double as an argument. When that is invoked with byte, short, long, and float, it will match all four invocations of print() method

Java




// Main class
// To test Method Overloading
class TestMethodOverloading {
      
    static void print(double d)
    {
        System.out.println("double = " + d);
    }
    public static void main(String[] args)
    {
        byte b = 5;
        short s = 5;
        long l = 5;
        float f = 5.0f;
        print(b);
        print(s);
        print(l);
        print(f);
    }
}
Output
double = 5.0
double = 5.0
double = 5.0
double = 5.0

Now let us do discuss our feature in Java5 that is autoboxing. Autoboxing was one of the key features that were introduced in Java 5. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. So bring the Java 5 concept of backward compatibility into play. With reference to Autoboxing consider the following example that will help us to understand how Java maintains backward compatibility while introducing new features.

Example

Java




// Java Program Illustrating Maintainence of Backward
// Compatibility
  
// Importing required classes
import java.io.*;
  
// Main class
// To test backwards compatibility
class GFG {
  
    // Method 1
    static void print(Integer i)
    {
        // Print statement whenever method 1 is called
        System.out.println("Integer = " + i);
    }
  
    // Method 2
    static void print(long l)
    {
        // Print statement whenever method 1 is called
        System.out.println("long = " + l);
    }
  
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        int i = 5;
  
        // Calling method over above custom input
        print(i);
    }
}
Output
long = 5

Learnings from the above outputs are as follows:

  • If the only version of the print() method was there that accepts an Integer, then with reference to Java 5’s Autoboxing capability, invocation of print() would be successful.Similarly, if only the long version existed, the compiler invokes the same.
  • What if both methods exist, which one will be used? So in order to establish backwoods compatibility, the compiler thinks that widening a primitive parameter is more desirable than performing an Autoboxing operation, so the compiler will choose widening over boxing and the output will be

Conclusion: In this article, we have seen how to maintain and things to consider achieving”Backwards Compatibility” while developing new features with example. Java designers gave preference to the existing feature ‘Widening’ with the introduction of the new ‘Autoboxing’ feature in Java 5. An important rule is preexisting code should function the way it used to so the compiler will always choose the older style before it chooses the newer style (Widening over Autoboxing).

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :