Java Concepts Explained

javaconceptsBefore you can understand how to program in Java properly, you need to have a basic understanding of object oriented programming concepts and how they relate to Java programming. In this tutorial, you will learn the basic concepts inherent to all object oriented programming languages which can then be applied directly to creating Java applications.

If you have absolutely no programming experience, Programming for Non-Programmers is a great place to start. This course provides insight into many common programming terms that can be applied to Java programming as well as programming in most other modern programming languages.

What is an Object?

Understanding objects is the key to understanding object oriented programming. The best way to understand objects is to compare objects in programming to objects you may find in the real world.

A good example is a car. A car is an object that has two distinct characteristics. It has a state and it has behavior. The state of a car would include properties such as its color, make and model, current speed, current direction, and current gear. The car also has specific behaviors such as accelerating, changing gears, braking, and turning.

An object in the programming world works exactly the same way. They also consist of both a state and related behaviors. An object’s states are stored in fields (or variables) and behaviors are exposed through methods (methods may be known as functions in some other programming languages).

These methods operate on an object’s internal state and allow objects to communicate with one another. This also brings up another important component of object oriented programming. Data encapsulation refers to all of the internal components of an object being obscured from view. Returning to the car example, you know that a car has an engine but you do not need to know how the engine operates in order to operate the vehicle. This is the same concept as data encapsulation in programming.

You can learn more about object states and behaviors in Java for Absolute Beginners.

What is a Class?

If an object is a specific car (i.e. Chevrolet Camaro), then the word “car” is actually a class reference. In other words, your Camaro is an instance of the class of objects known as Cars. A class is basically the blueprint from which individual objects are created.

You know that all cars have four wheels, a gas pedal, a brake pedal, and a variety of other components. With few exceptions, these components are standard among all cars and these values are inherited automatically by every instance of the Car Class. To understand classes a little better, take a look at the following example:

class Car {

 

int speed = 0;

int gear = 1;

 

void changeGear(int newValue) {

gear = newValue;

}

 

void speedUp(int increment) {

speed = speed + increment;  

}

 

void applyBrakes(int decrement) {

speed = speed – decrement;

}

}

In its simplest form, the example above represents what the Car Class might actually look like. Each vehicle is able to speed up, change gear, and slow down. By creating a car class you are able to pass along these attributes to any car instances you create in your programs.

Java Fundamentals I & II explains the concept of classes in more detail.

What is Inheritance?

In the previous section, the word inheritance was mentioned. Inheritance is another key component of object oriented programming. Java allows you to create instances of a class (objects) that inherits specific states and behaviors from the class(es) above it.

As your Java programs become more advanced, the value of inheritance becomes very apparent. It means you do not have to define every single state and behavior for your objects; instead, you only have to define these states and behaviors once in the class definition. When you create a new object, you will only have to define additional states and behaviors specific to that instance.

Not only does this save time, but it also makes modifying existing code much easier. You are able to make changes to the parent class without having to modify each individual object in your program.

What is a Package?

A package is simply a namespace that organizes a set of related classes in a single place. You can think of a package kind of like the a different folders on your computer. You may have a picture folder, a video folder, a music folder, etc. The point is that items with in that folder are related.

Your Java programs could be composed of hundreds or even thousands of individual classes. It only makes sense to keep things organized by placing them into packages. One of the things that makes Java such a powerful programming language is its extensive Application Programming Interface (API). This is a pre made set of classes you can use in your Java programs. For example, the String object contains the states and behaviors for character strings, the Socket object allows for the creation and use of network sockets, and most Graphical User Interface (GUI) objects are also included within the standard Java API library including the Swing Library, which you can learn more about in Java Swing Programming: Beginner to Expert.

Many of these are automatically included in the Java core library. Any special ones you need will have to be imported in your Java application.

Both the classes you define yourself and the classes that come preconfigured in Java are organized into a single application package. This package contains everything your program needs to run successfully on any platform.

Now that you have a better understanding of the basic concepts inherent to object oriented programming in Java, you are able to tackle creating your own programs using this OOP model. Remember that most modern programming languages operate in the exact same way, so it will be very easy for you to transition into other programming languages based on the techniques and concepts described in this tutorial.