testinganswers.com

Year: 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

11 Constructors & Enum

11.1 | Constructors   Constructor declarations are similar to method declarations. However, the constructors’ names must be the same as the class name and it cannot return a value. The main objective of a constructor is to set the initial state of an object. When the object is created by using the new operator. The following code shows how to declare constructors with and without input parameters.       11.2 | ENUM   Enum is a Java keyword used to represent a fixed number of well-known values. For example, the number of days in a week, the number of Read More

10 Class and Methods

10.1 | Class A class is a template from which objects are created. A class declaration typically contains a set of attributes (instance variables) and functions (methods).     10.2 | Methods In Java, functions, and procedures are called methods. Methods can include zero or more input parameters and zero or one return parameter. The following code shows some method declarations. An arithmetic class has two methods printSum and getSum. method printSum has two input parameters both of integer type and no return parameter. while method getSum has two integer type input parameters and one integer type return parameter.       Read More

09.3 Control Flow

9.3 | Transfer Statement Java provides six language constructs to transfer control or exit loops in a program. break continue return try …catch …finally throw assert   break …statement   break …statement terminates a loop.   For example, the following code showed how to terminate a while and for a loop.   continue …statement continue …statement exits the current iteration and starts executing the next iteration. For example, the following code prints all numbers except number 3. return …statement return …statement stops code execution of a method and transfer control back to the calling code. try …catch …finally …statement try Read More

09.2 Control Flow

9.2 | Looping Statements Java supports four looping statements. for…statement for each…statement while…statement do-while…statement   for …statement   It executes a block of code a specified number of times.      For example – the code below prints numbers from 1 to 9.   for…each statement   It executes a block of code for each item in a collection or each element in an array.   For example – the code below prints all numbers in an array.     while…statement   It executes a block of code while or until the condition is true.   For example – The Read More

09.1 Control Flow

Java, like any other programming language, supports both conditional and loop statements to control code flow.   9.1 | Conditional Statements   In Java, we have four conditional statements: 1. if(condition=true){}: Execute a set of code when the specified condition is true.  2. if(condition=true){}else{}: Execute first block of code when the specified condition is true else executes the second block of code.  3. if(condition1=true){}elseif{condition2=true}{}else{}: Execute the first block of code for which the condition is true. If no condition is found true and else block exists, then this block of code is executed. If no condition is found true and Read More

08 Arrays

8.1 | Arrays An array is a data structure that stores a collection of values of the same data type. Values stored in the array are accessible through the array index //Declaring an array of integers int arr1[];   //Creating an integer array with values int[] arr0 = {2,3,4,5,6,7};   //Creating an integer array which can hold 100 integers int arr2[] = new int[100]; //Or, int arraySize = 100; int arr3[] = new int[arraySize]; //Assigning values to the array arr3[0] = 1; arr3[1] = 2; //Creating a string array which can hold 100 values String arr4[] = new String[100];   Read More