Thursday, October 7, 2021

Introduction to Java: An Object-Oriented Programming Language

Java is one of many languages falling into the Object-Oriented Programming (OOP) methodology, others being Python, C++, C#, etc. What exactly is OOP? This methodology involves using classes and objects to design a program. In this context, an object is anything that has state and behavior, otherwise known as variables (or properties) and functions (or methods). For example, a horse is an object because it has states like color, name, breed, etc., and behaviors like neighing, eating, and trotting.

A class, by definition, is a collection of objects and is the blueprint from which individual objects are created. For example, if you have a bicycle, it would be an instance of the class of objects known as bicycles. This is because there are thousands of other bicycles in existence that are the same make and model.

Essential Concepts

The four pillars of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism. The paragraphs below give a basic introduction to these concepts. Please refer back to the first paragraph for terms that are used synonymously.

Encapsulation involves grouping related variables and functions that operate on them into objects. For example, we can have an employee object with three properties (base salary, overtime, and rate) and a method called “GetWage.” In OOP, the “GetWage” function has no parameters because they are modeled as properties of the object. In procedural programming, the functions are on one side, and the variables are on the other side, meaning the function will have three parameters (base salary, overtime, and rate). Encapsulation reduces complexity and increases reusability.

In Abstraction, the internal details are hidden while the functionality is shown. Think of a DVD player. There is a complex logic board on the inside and a few buttons on the outside that you interact with. You press buttons on the outside without caring what is happening on the inside. That complexity is hidden from you, and the same technique is used in objects. We can hide some of the methods and properties from the outside, benefiting from a simpler interface while reducing the impact of change. We can change the inner or private methods, and none of these changes will leak to the outside because we do not have any code that touches these methods outside of their containing object. We can delete a method or change its parameters, but none of these changes will impact the rest of the application’s code. Abstraction reduces complexity and isolates the impact of changes.

In Inheritance, an object acquires all the properties and behaviors of a parent object, allowing code to be reused. In other words, it is a mechanism allowing you to get rid of redundant code. For example, think of humans. We inherit common properties from the human class such as speaking, breathing, eating, and drinking. Instead of defining these elements multiple times, we can create a generic object (human) and have the other objects (individual people) inherit those generic properties and methods. Inheritance eliminates redundant code.

Polymorphism is the ability to perform a task in many different ways. This is a technique that allows you to get rid of lengthy statements. Going back to the human example, those elements are unique to an individual. The way each element is represented is different from the others. We can implement a render method in each of the objects, and the render method will behave differently depending on the type of object we are referencing. We substitute many lines of code for one. Polymorphism refactors long and ugly statements (also known as switch/case statements).

Java installation guidance.

As a beginner to the Java language, the most beneficial resource will be Oracle’s Java Documentation. Here, you can view tutorials and explanations for anything Java-related. The “Hello World!”tutorial is a great starting point, using the NetBeans Integrated Development Environment (IDE). This IDE can be downloaded here, and as of the publication of this post, features the 12.5 release. If you need help with this installation, this video on YouTube explains the process thoroughly. In fact, most answers regarding the Java programming language can be found in tutorials on the open web, especially YouTube. You will also need a version of the Java Development Kit (JDK) before installing an IDE. These downloads can be found on Oracle’s website and, as of this publication, features JDK 17. I recommend scrolling down to JDK 16.0.2 because the new JDK cannot be installed in the IDE initially. If you are interested in using the latest and greatest JDK 17, you can download the kit and follow the Java documentation guidance referencing how to change your Java Platform in the IDE. From there, you are ready to start coding with Java!

No comments:

Post a Comment