Last Updated:

OOP using Java technology

Introduction

Java technology covers a large area, but the language itself is not very large. However, describing it is not an easy task. This section of the guide does not cover the language completely. Instead, it will cover everything you need to know to get started with the language, and what you are most likely to encounter as a novice programmer. Other tutorials cover various aspects of the Java language, additional useful libraries from Sun (and other sources), and even different IDEs.

We'll provide you with enough information here, with explanations and code examples, to get you started writing Java programs and learn how to properly apply OOP in the Java environment. Now it depends only on practice and training.

Most beginner's guides read like reference books on language specifications. First you see all the syntax rules, then some usage examples, then more complex topics like objects. We will not stick to this pattern. We do this because the main reason for poor object-oriented code in Java programs is the lack of immersion of novice programmers in objects from the very beginning of training. Objects are considered as an additional feature, or an auxiliary theme. Instead, we are compatible with learning Java syntax with learning Java OO. Thus, at the end of the training, you will have a holistic picture of the use of language in the context of an object-oriented approach.

Java Object Structure

Remember that an object is an encapsulated entity that knows about itself and can perform any actions when requested. Each language has its own rules for determining the object. In Java, objects typically look like the following listing, although they may not have all of these parts:

There are a few concepts here, which we'll discuss in the following sections.

Packages

The package declaration comes first when defining the class:

Every Java object exists in a package. If you don't explicitly specify which one it belongs to, Java will put it in the default package. A package is simply a collection of objects, each of which is (usually) related to other objects in the package. A package references the path to a file in your file system. Package names use a schema with dots to convert this path to the file into a suitable view for the Java platform. Each part of the package name is called a node.

For example, in the java.util.ArrayList package, java is a node, util is a node, and ArrayList is also a node. The last node references the ArrayList.java .

import statements

In the class definition, the following are the import expressions:

When your object uses objects from other packages, the Java compiler needs to know where to find them. Import statements tell the compiler the location of the classes you are using. For example, if I wanted to use the ArrayList class from the java.util package, I would have to write the following expression:

Each import expression ends with a semicolon, as do most expressions in java. You can have as many import statements as you want to specify the Java location of the classes you use. For example, if I wanted to use the ArrayList class from the java.util package and the BigInteger class from the java.math package, I could write the following expressions:

If you are importing more than one class from the same package, you can use a shortened entry. For example, if I wanted to use ArrayList and HashMap, both from the java.util package, I could import them as follows:

You must specify an import statement for each unique package from which you are importing.

Class Declaration

Next is the definition of the class:

You define a Java object as a class. Think of the class as a template for an object, much like a cookie form. The class defines the type of object you can create with it. You can stamp any number of objects of this type. When you do this, you create an instance of the class. Note: The word object is typically used interchangeably for both a class reference and a class instance reference.

The accessSpecifier for a class can have multiple values, but it is most often set to public, and we will cover only that in this tutorial. You can name a class however you want, but by convention, class names begin with a capital letter, and every subsequent word in the name also begins with a capital letter.

Classes have two types of members: variables (or member data) and methods. All class members are defined in the body of the class, which is inside a single set of curly braces for the class.

Variables

The values of class variables are how each instance of the class is different. This is why they are often referred to as instance variables. The variable has an access specifier, data type, name, and (optionally) an initial value. Here is a list of access specifiers and their meanings:

  • public: Any object in any package can see a variable.
  • protected: Any instance of a class, any subclass in the same package, and any non-subclass of the same package can see the variable. Subclasses in other packages cannot see the variable.
  • private: No object except a specific instance of this class can see a variable, not even a subclass.
  • No specifier (or p ackage protected): Only classes in the same package as the class that contains the variable can see it.

If you try to access a variable that is not available to you, the compiler will warn you that the variable is not visible to you. The circumstances in which you should use an access specifier will be discussed later.

Methods

The methods of a class determine what it can do. There are two types of methods in the Java language:

  • Constructors
  • Other methods

Both types of methods have access specifiers (which determine which other objects can use them), bodies (between curly braces), and contain one or more operators. In addition, their form and function are different. We'll look at each of them in turn in the next two sections.

Constructors

Constructors allow you to specify how to instantiate a class. The constructor is declared as follows:

You always have a default constructor (with no arguments) for each class you create. You don't even have to define it. Constructors differ from other methods in that they do not have a return data type because the returned data type is actually a class. The constructor is called as follows:

When calling the constructor, the new keyword is used. Constructors can have or have no parameters (the default constructor does not). In a strict sense, constructors are not methods or members of a class. This is a special beast in the Java language. In practice, they most often look and work as methods, and many mix them together. Just remember that they are special.

Other methods

The rest of the methods in the Java language you will use most often. They are declared as follows:

Each method has a return data type, but not every method returns anything. If the method returns nothing, the void keyword is used as the return type. You can name a method however you want if the name is a valid identifier (for example, it cannot begin with a period ( . )), but by convention the names of the methods are:

  • Use letters
  • Start with a small letter
  • Consecutive words begin with capital letters

The methods are called as follows:

Here, we call the methodName() object instanceOfSomeClass and pass a few arguments. The differences between parameters and arguments are not great, but they are there. The method takes parameters. When you pass specific values to the method being called, those values are the arguments to the call.