15 Exception Handling

Exceptions in Java are objects. All exceptions are derived from the java.lang.Throwable class. Exceptions can be handled in Java using try-catch-finally construct. Exceptions thrown by the try block of code is caught (handled) by the catch block.

The following code shows an example of the try-catch-finally construct.





The output of the above example




Whenever an exception is thrown by try block of code, it looks for catch construct which handles that exception. If no catch construct is found which handles the exception, then the exception is handled by default exception handler. Catch construct is not executed if no exception is thrown or if the respective catch construct does not handle the specific exception. For example, in the code above, the catch construct only catches ArithmeticException. Any other exception, say FileNotFound will not be handled by this catch construct. Finally, the code block is executed, no matter an exception is thrown or not. An example for use of finally construct can be to write a code that closes DB connections. This will ensure that DB connection is closed, no matter an error happens or not during data retrieval from DB.



14 Interfaces

An interface is a group of related methods with empty bodies. In other words, it is a collection of abstract methods. An interface is not a class. A class implements an interface to inherit the abstract method of the interface.


The following code shows an interface Cars, that has method make() and price().


Cars Interface



The following steps show class Nissan which implements Cars interface and define its abstract method make() and price().



14.1 | Create a new class in Eclipse


1. Create a new class in Eclipse as defined in the previous post.

2. Provide reference of Cars interface as shown in below screen.
3. Click on Finish button.


Nissan Class implements Cars interface




14.2 | Created Nissan Class


Created Nissan class looks like below screen





The following code shows class Nissan which implements Cars interface and defines its abstract method make() and price().


Nissan Class



The below screen is the output of the above codes.








13 Abstract class

A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods. If a class contain any abstract method then the class is declared as an abstract class. An abstract class is never instantiated. It is used to provide abstraction.

abstract class className {

 }

Before learning the abstract class, let's understand the abstraction in Java first.


13.1 | Abstraction

Abstraction is a process of hiding the implementation details and showing only functionality to the user. 

let's take an example, We send an email to others, where you type the mail body & receiver email id and send it, without knowing the internal processing of the email delivery.


13.2 | Abstract Method

A method, which is declared as abstract and does not have implementation is known as an abstract method. The method body will be defined by its subclass.

Example-

abstract return_type method_name ();  //no method body with abstract keyword


13.3 | Non-Abstract Method

A method, which is declared with a body and without abstract keyword is known as a non-abstract method. We also know the non-abstract method as a normal method and concrete method.


abstract return_type method_name () {
         
      ... 
       method body define here 
        ...

}


Example of the Abstract Class:

Abstract Class Example


The output of the above example







12 Inheritance

Inheritance is a mechanism which allows classes to inherit the attributes of an existing class. It is used in situations where the subclass (which inherit attributes) is a subset of the superclass, whose attributes are Inherited. For example, suppose there are three classes of employee, developer, and tester. In this case, both developers and testers are employees. Here, the employee class can define generic attributes of employees. Specific attributes of developers can be defined in developer class, while specific attributes of testers can be defined in tester class. Both developer and tester are subclasses while an employee is a superclass. A class can inherit other class attributes using the keyword 'extends'.


12.1 | Employee.java

The following code shows the inherent attributes of another class.

Employee superclass


12.2 | Developer.java

Developer subclass


Similar to class Developer.java, a separate class Tester.java can be defined for defining tester attributes.

The class below 'Company.java' shows how to use the above-defined classes for adding and viewing employee details.


12.3 | Company.java

Company main class

The below screen is the output of the above codes.

output of the Company main class