11 Constructors & Enum

11.1 | Constructors

Constructor declarations are similar to method declarations. However, constructors name 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.

Constructor Class

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 planets in the solar system, etc.

Example 11.2.1 Simple enum of weekdays.

Enum Class

Example 11.2.2 Simple enum of weekdays with weekday number

Enum Class

Example 11.2.3 An enum defining weekday full name and weekday number

Enum Class

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 parameter and one integer type return parameter.

10.2.1 | Method Overloading

In Java, method overloading occurs when two or more methods in the same class have the same name but different parameters. Two methods can be considered overloaded if any of the below conditions are true:
  • The number of parameters is different for the methods.
  • The parameter types (input or return) are different.
Class ArithmeticClass below has two methods with same name getSum. these two methods are overloaded as the number of input parameters is different of both.

Arithmetic Class

10.2.2 | Method Overriding

Overriding a method involves defining a method in a subclass that has the same signature (input and return parameter) as a method in a superclass. Then, when that method is called, the method in the subclass is found and executed instead of the one in the superclass. The following code overriding method print. The code below shows the classes SuperClass and SubClass. SubClass overrides the print method of its superclass. MainClass shows different ways of calling the overridden method of a subclass as well as the original method of the superclass.

Super Class

10.2.3 | Variable and Method Scope

Java has reserved keywords to define the scope of variables methods and classes. 

private keyword is used to declare variables and methods that are to be accessible only within the class. 

protected keyword is used to declare variables and methods that are to be accessible only within the class or any class that extends to this class.

public keyword is used to declare variables and methods that are to be accessible within the class and as well as outside of class.

09.3 Control Flow

9.3 | Transfer Statement

Java provides six language constructs to tranfer control or exit loops in a program.

  1. break
  2. continue
  3. return
  4. try ...catch ...finally
  5. throw
  6. assert

break ...statement

break ...statement terminates a loop.

For example, the following code showed how to terminate a while and for a loop.

Break Statement by testinganswers.com

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 ...catch ...finally ...statement is used for handling exceptions. (Further, discuss in section 'Exception Handling'.)

throws ...statement

throws ...statement is used for handling exceptions. (Further, discuss in section 'Exception Handling'.)

assert ...statement

assert ...statements are used to validate the assumptions made about the program. Assertions are expected to be true when assert statement are executed. In case it is false, the Java Virtual Machine (JVM)  throws a special error of AssertionError class. Assertion error are not handle but allowed to propagate to the top level.

Note: Assertion facility can be enabled or disabled at run-time. If disabled, assert statements are not executed during run-time.