Month: September 2018

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 the try-catch-finally construct. Exceptions thrown by the try block of code are 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 the default exception handler. The catch construct is not executed if Read More

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().   The following steps show class Nissan which implements the 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 Read More

13 Abstract class

A class that 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 contains 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 Read More

12 Inheritance

Inheritance is a mechanism that 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 the developer class, while specific attributes of testers can be defined in the tester class. Both developer and tester are subclasses while an employee is Read More