Dynamic Polymorphism or Run Time Polymorphism
Here, the call to an overridden method is fixed at run time. Method overriding is one method to achieve dynamic polymorphism.
Coercion, operator overloading, and polymorphic parameters are the other features of polymorphism in Java.
Coercion polymorphism refers to implicit type conversion. What is implicit type conversion? It is the conversion of a data type which is performed automatically by the compiler. This is done to prevent type errors when the programmer doesn’t intervene.
In operator overloading, the operator has varied meanings as per the context.
In polymorphic parameters, you can have a name of a parameter or method in a class to be linked with different types.
Java Tutorial: Handing exceptions
Sometimes when you execute a program you may get an undesired or unexpected event. This event which occurs at run time disturbs the normal flow of the program’s instructions. This powerful event is called an exception.
Exception handling is a process to deal with run-time errors. The major benefit of exception handling is to keep up the normal flow of the application.
Java Tutorial: Types of Java Exceptions
There are three types of exceptions: checked, unchecked, and error.
Checked exception: Also referred as compile-time exception, this exception is checked by the compiler during compilation time. As a programmer, one should diligently handle these exceptions.
Unchecked exception: Also known as run time exceptions, this exception takes place during the time of execution. These exceptions are ignored while compiling.
You might be wondering why errors come here. The issue is not within the control of the programmer. Errors are generally ignored in your code since you cannot mostly do anything with these errors..
“Throwable” acts as the basis for Java’s error and exception hierarchy. Keep in mind that an error takes place when there is inadequacy of system resources while an exception is caused when a code consists of some issue. The program will be terminated in an abnormal manner in the case of error. However, when there is an exception, the program will throw an exception. You can apply the try and catch block to take care of this.
Java Tutorial: Exception handling
Exception handling in Java is handled through five keywords: try, catch, finally, throw, throws
Try: The “try” block comprises a group of statements where an exception can take place. It will be typically followed by a “catch” block. The “catch” block takes care of the exception that takes place in the relevant try block. Even a “finally” block can follow the “try” block.
catch: A “catch” block handles the exception. The “try” block must take place before the “catch” block. So you would realize by now that the “catch” block cannot be used in isolation. A “finally” block can follow it.
finally: A “finally” block can be applied only with try-catch block. You may consider the “finally” block optional but it is good practice to use it. This block is a last chance for performing any type of cleanup before the method returns. For example, you can use “finally” block for closing a file.
Throw: When you “throw” an exception explicitly, then it is called as “throw” exception. It throws from a method or block code.
Throws: Suppose a method is capable of causing exception, it should give a list of all the exceptions possible at the time of its execution. The advantage of this is that the user calling that method gets knowledge beforehand regarding the exceptions to be handled.
In this Java tutorial, I have covered all the essential concepts of Java. I hope you are clear about everything discussed in the tutorial. Go through the concepts diligently and write the programs many times. The topic covered in this tutorial are the basics of Java and will be very useful when you work as a Java programmer. All the best!