Skip to main content


Showing posts from June, 2021

Covariant Return Type in Java

In this tutorial, we'll discuss covariant return type in Java which was added in Java 5. Method overriding in Java is said to be covariant with respect to return type which means that the return type is allowed to vary in the same direction as the subclass. What is covariant return type in Java First let’s try to understand what exactly does this covariant return type mean? Before Java 5 it was not possible to change the return type of the overridden method in the sub-class meaning the method in the parent class and the overridden method in the child class should have the same name, same number and type of arguments and same return type. Java 5 onward, because of this covariant return type feature, it is possible for the overridden method to have different return type from the method in the parent class. There is one restriction though, the return type of the sub-class method must be a subtype of the return type of the parent class method . For example - In parent class the

Shallow Copy Vs Deep Copy in Java Object Cloning

In this post we’ll see the differences between shallow copy and deep copy in Java object cloning. To understand the differences it is very important to understand the concept of shallow copy and deep copy while cloning an object so first we’ll try to understand the concept using examples of both shallow copy and deep copy. To know more about object cloning in Java please refer this post- Object Cloning in Java Using clone() Method Table of contents Shallow copy in Java object cloning Shallow copy in Java example Deep copy in Java object cloning Deep copy in Java example Shallow copy Vs Deep Copy in Java Shallow copy in Java object cloning When an object is cloned using clone() method a bit wise copy happens where each field’s value in the original object is copied to the cloned object’s corresponding field. This way of object cloning in Java using the default clone() method creates a Shallow copy . This is known as shallow copy because this process of

Object Cloning in Java Using clone() Method

Object cloning in Java is the process of creating a copy of an object. To clone an object in Java clone() method of the Object class is used. Table of contents clone() method Cloning an object in Java using clone() method Examples of Java object cloning Advantages of object cloning Disadvantages of object cloning Object cloning - Shallow copy Java Object cloning - Deep copy clone() method clone() method is defined in the Object class as a protected method. protected Object clone() throws CloneNotSupportedException If you are going to write a clone() method in your class to override the one in Object class then it has to be written as a public method. public Object clone() throws CloneNotSupportedException Cloning an object in Java using clone() method One of the requirement for cloning an object is that the class whose objects are to be cloned must implement Cloneable interface. Cloneable interface is a marker interface and defines no me

Java Exception Handling Best Practices

In this post some of the exception handling best practices in Java are listed. Following these best practices in your Java code will help you in writing robust code. Java Exception handling best practices 1. Don’t ignore exception - Exception handling especially the checked exception gives you a chance to recover from the thrown exception. Thus having a catch block that is empty defeats the very purpose of exception handling. You need to avoid code like this- try { ... ... } catch( IOException e ) { } Even if you are pretty sure that there won’t be any exception in the block of code, still log the error at least. In the rare case exception is thrown in the block at least you will be having some log message to find out what went wrong. try { ... ... } catch( IOException e ) { logger.error(“Exception caught ” + e.getMessage()); } 2. Always clean up resources in finally block - If you are using resources like I/O streams, DB connection, socket connection in your c

Merging PDFs in Java Using OpenPDF

In this post we’ll see a Java program to merge PDFs using OpenPDF library. OpenPDF is open source software with a LGPL and MPL license. To know more about OpenPDF library and PDF examples check this post- Generating PDF in Java Using OpenPDF Tutorial Merging PDFs using OpenPDF To merge documents you need to use PDFCopy class which makes copies of PDF documents. Using PDFReader open the source PDFs and get pages from the PDF using getImportedPage() method of the PDFCopy class. Following Java program shows how two PDF documents can be merged using OpenPDF. import; import; import java.util.Arrays; import java.util.List; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; public class PDFMerge { public static final String MERGED_PDF = "F://knpcode//result//OpenPDF//Merged.pdf