JAVA – Abstract Classes and Methods

The definition of a class should not be instantiated since it only represents the abstract concept. For example, the instance of pet can not be seen. What you can see are the instances of cat, dog, pig or horse. The instance of pet can not be seen and does not make sense.

Abstract Classes

As with object-oriented programming, declaring abstract concepts might be desired, but there is no possibility that you can create an instance of same. For example, the abstract concept of numbers is represented by Number class available with the java.lang package. The Number class serves as a model for numbers used in programs but is impossible to create the generic object.

The Number class is parent class of number of other number classes like Integer and double which implements specific types of numbers. The abstracted class represented by the number class, which implements abstract concepts. Abstract classes can be subclassed but it cannot have instance of it.

Continue reading JAVA – Abstract Classes and Methods

JAVA – Static Member classes

The static member class is similar to any other class, but this type of class is nested within another simple class. The static member can be used both by the external classes and the class in which it is defined. The static member class can be used by hierarchical name using name of containing class as prefix.

Some of the features of the static member class are:

  • The static member class is always defined with static modifier. This way static member class becomes equivalent to all other class members which are declared with static modifier.
  • A static member class can use access control modifiers, without any alteration in the meaning of these modifiers.
  • The static member class can access all the static members of containing class (private member as well). ses.
  • The static member class can not be associated instances of the containing class.

JAVA – Initializer blocks

As you have already learnt, the initialization expressions of the fields and the code of the constructor methods are used to initialize the objects. A class contains a class initialization method. Though they are both the same, there is only one difference. The class initialization method needs to be explicitly defined in the body of this method. You can write arbitrary codes for initializing the class fields in the Java programming language. This is done using a construct known as a static initializer.

A static initializer is nothing but the keyword static followed by a block of code enclosed in curly braces. This keyword can appear in a class definition wherever a field or method definition appears. A class can use multiple initializers where the body of each initializer is contained into the class initialization method. The initializer’s body also contains the static field initialization expressions. A static initializer is similar to a class method. However, it cannot make use of the this keyword, any instance fields or instance methods of the class. Continue reading JAVA – Initializer blocks

JAVA – Field Defaults and Initializers

As you have seen earlier, all the fields do not need to be initialized. The member fields of a class are initialized automatically, unlike local variables that need to be explicitly initialized. For example, the primitive data type fields are initialized to a default value of false or zero automatically. Similarly, all fields of reference data types are initialized to null. Java compiler automatically performs this job depending on the default value of a field, and this condition implies to both instance fields and class fields.

Now let’s look at a variable declaration. The variable is declared within a Java statement and is initialized when the Java statement is executed. Unlike variables in a method, the field declarations are not a part of any particular method. These cannot be therefore executed as individual statements.

On the other hand, the Java compiler automatically generates initialization code that is instance-free. This instance-free initialization code is stored in the class constructor or constructors. The order of the constructor in the source code determines the manner in which it appears in the constructor. Accordingly, the field initializer can use all the initial values of the fields declared before it. If a constructor starting with the this() keyword makes a call to another constructor, the field initialization code is not displayed in the first constructor. Conversely, the method initialization is done by the constructor invoked by the this() call.

JAVA – Defining Multiple Constructors

Certain objects can be initialized in many different ways and can also be used in multiple ways. For example, in the Circle class that we all are familiar with, the radius of the circle can be initialized to a specified value or a reasonable default value. Therefore, we can use multiple constructors if there are many instances of fields in a program. Let’s see how we can define two constructors for the Circle class:

public Circle_class() { radius_var1 = 0.54; }

public Circle_class(double radius_var1) {  this.radius_var1 = radius_var1;}

We can define as many constructors as we want for a particular class. However, each constructor should have its own parameter list. The Java compiler selects a constructor on its own based on the type of parameters and the number and type of arguments supplied by you.

Calling One Constructor from Another

The ‘this’ keyword as defined above is used specifically in the case a class has multiple constructors. The ‘this’ keyword is then used to invoke one of the other constructors of the same class. In other words, we can rewrite the two previous Circle constructors as follows:

// This is an example of basic constructor: It is used to initialize the radius

public Circle_class(double radius_var1) { this.radius_var1 = radius_var1; } Continue reading JAVA – Defining Multiple Constructors