JAVA – Objects

In the Java programming language, unlike the C language which uses a structure to create a new complex data types, classes are used to define a new data type. For example, when a new class animal is created in Java, it also implicitly creates an animal data type.

The use and assignment of the data item depends on how it was defined. For example, an item of animal data type can be assigned to a variable of type animal. It can also be passed to as a class that accepts animal as an argument.

The object-oriented programming language such as Java does not only use a data type as a simple attribute. In this type of programming, a class is represented as a hierarchy while a subclass can be shown as a special kind or a subtype of its parent class. Not only this, reference types and class are related to each other in some ways. Also, child classes always extend to their parent class and have the same functionality as the parent.

Continuing with the example we saw above, a lion class can extend to the animal class, this new type Lion is considered in the java language as a subtype of the type animal. The objects of the Lion class can also be used in places where the objects of animal class can be used. Therefore, an object of type Lion is assignable to the variable of type Animal. This assignment of classes is known as subtype polymorphism and forms a primary feature of any object-oriented language called Java.

Going back to the Reference types, these types are always accessed by a reference, which is nothing but a handle or the name of an object. The variable of a reference type holds a reference to an object of its type or of a subtype.  Similar to a pointer in C or C++ a reference is a data type which is enforced strictly and can’t be examined directly. These values are static in nature and cannot be changed except when assigned to a particular object. The reference values are automatically dereferenced in any place where they are referenced. Continue reading JAVA – Objects

JAVA – Getter/Accessors and Setters/Modifiers

In Java, robust classes and components are based on the object-oriented principles of encapsulation and information hiding. This approach of information hiding can be further extended by using the appropriate assessor methods.

Accessor is member functions used in java to directly manipulate the value of any field. These accessor methods are of two types:

  • Setters
  • Getters

A setter is used to modify the value of a field. On the other hand, a getter is used to obtain the field’s value.  Execution of assessors involves a minimal overhead to your program’s code. Not only this, if these assessors are not used in your code, the loss of performance is often very small when compared to other factors such as databases not designed well.

However, the use of accessor benefits the code too by hiding the implementation details and by increasing the robustness of your code. A good programmer can increase the maintainability of the code by defining a getter and a setter to access a field. This leads to minimizing the effort for making any changes in the code. The getters and setters enable a programmer to encapsulate important:

  • Business rules
  • Transformation logic
  • Validation logic applicable to the data fields of the code.

The example given below depicts the use of the setter and getter functions for the Address_of_your_Home field of the Employee class Continue reading JAVA – Getter/Accessors and Setters/Modifiers

JAVA – Constructors

Though constructors and methods look similar to each other, they are different both in construct and syntax.

Constructors are used to create instances of a class. Constructors are created in a manner similar to objects, such as:

mari_gold m_var1= new mari_gold();

Some of the key properties that differentiate the constructor and the methods are:

  • Modifiers: Like methods, constructors can also use any of the following three access modifies, such as Public, Private, and non, which is also known as package or friendly. The following types of constructors do not exist: abstract, abstract, static, final, native, or synchronized.

 

  • Return types: Constructors do not have any kind of return types, unlike methods which can have any valid return type or at least a default value as void.
  • Signature: The naming convention of constructors and methods also vary greatly. Constructors get the same name as the class name. On the other hand, methods use names that are not same as class name. If the programmers use the normal Java language conventions, constructors would always start with upper case letters, unlike methods which start with a lower case letter. Not only this, you will also notice that the names of the constructors are nouns, whereas the method names are adjectives as they indicate action.
  • The use of “This” keyword: Both constructors and methods use the keyword “this” in a different manner. The keyword ‘this’ in a method refers to an instance of the class that is using the method. Also, the static methods do not belong to any instance of the class, and therefore the keyword “this” does not refer to anything in such methods. These methods belong to an entire class and not to any particular instance of the class.

Conversely, in the case of constructors, to refer to another constructor in the same class the “this” keyword is used, but having a different list of the parameters associated with it. Continue reading JAVA – Constructors

JAVA – Methods


A method can be defined as a group of instructions that is given a name and can be called up anywhere in the program code by simply calling it by its name. For instance, a statement to draw a straight line can be given a name straight_line. You can then call the straight_line statement thrice to draw a triangle or you could save the method triangle and call it as and when required.

All programming languages support methods presumably by different names such as functions or procedures. The Java language, just like the C++ language from which it originated, supports methods.  Unlike the C++ language where an object can contain methods, the Java programming language declares a program which further contains functions known as methods. As you will notice, the method is declared in a similar manner as the method paint, the only difference being that the method paint is automatically called.

The code snippet below displays a simple method, calculate.

import java.awt.*;

import java.applet.*;

public class calculate_value extends Applet

{ int first_var, answer_var_1;

public void paint_function (Graphics graph_var)

{

First_var = 34;

Calculate_value();

g.draw_String(“Twice 46 is ” + answer_var_1, 50, 75);

}

public void calculate_value ()

{ answer_var_1 = first_var * 8;

}

}

As you can see, the method calculate_value is called by the statement calculate_value();. The syntax of the method requires the use of opening and closing braces after the method name (even they don’t contain anything) followed by a semicolon at the end.

However, it is just not enough to call a method. However, it is important to define a method before it is called. Else, the Java programming language will not know what to do when it encounters a method name.  The method definition always starts with the public or private keyword. Continue reading JAVA – Methods

JAVA – Primitive Types

The Java programming language uses certain fundamental elements such as numbers, characters, and Boolean values. These programming elements are not considered as objects in this language. Java supports the concept of wrapper classes in situations where a primitive value needs to be treated as an object. This helps the Java compiler optimize the performance of these objects.

The java Virtual Machine architecture helps define the precise or exact values of the primitive types. For example, according to this architecture, int is always defined as a 32-bit signed two digit compliment number. Some other types that can be defined in this manner are

  • Boolean
  • char
  • byte
  • short
  • int
  • long
  • float
  • double

These primitive types resemble the scalar types of the C programming language on a 32-bit machine that is byte oriented. The new programmers of Java language can use these data types to learn the language faster and with greater ease. Continue reading JAVA – Primitive Types