7 Languages in 7 Days: Day 1 – Java (1/7)

Intro

Getting Started

Let’s start with the 800lb gorilla in the room, Java. Why Java? Well, let’s start off with the fact that it’s got a ton of community support and documentation everywhere. If you Google a programming problem, chances are within the first three results you will see an example using Java. There is also a plethora of amazing editors available. A good editor is not a replacement for knowing the tools available in a language, but it can help you in learning.


Java is an object oriented (OO) language. It internally embraces the best practices commonly accepted in OO programming. During this article we will be addressing two of the key concepts of OOP, Encapsulation and Inheritance.

So let’s get started. What is the core defining concept of any OO language? The creation of objects! Objects are the building blocks of any program. That’s great and all, but how do you create an object in Java? You start with a class. Let’s create a simple class, just use this code:

public class TestClass {
}

How simple was that? This seems a little boring, so let’s add some cool stuff to it:

public class TestClass {
 
    public static void greet(String name) {
        System.out.println("Hello " + name);
    }
 
    public static void add(int x, int y) {
        System.out.println(x + y);
    }
 
    public static void main(String[] args) {
        add(4, 5);
        greet("John");
    }
}

And this is what happens when you run it:

[cg@h:java] => java TestClass
9
Hello John

Variable Typing

This is the basic structure of any class really. They are made up of methods and variables. Notice the String and int words in front of different variables? These are the data types of the variables. Java is known as a statically typed language. Which means that you must assign a type (say, String) to a variable. Another thing when it comes to the type of a variable is that you cannot take a String x and assign an integer to it. Let’s take a look at what would happen if you did this:

public class TestClass {
    public static void main(String[] args) {
        String y = 8;
    }
}

[cg@h:java] => javac TestClass.java
[cg@h:java] => java TestClass

TestClass.java:4: error: incompatible types: int cannot be converted to String
String y = 8;
^
1 error

The above error that is returned is what keeps you from being able to assign invalid values to a variable. This is important to know as there are some languages that allow you to create variables and assign values without explicitly declaring a type. So, what we’ve done so far is interesting and all, but how do we improve on it? There are two remaining concepts that we will cover. They are, creating objects of a class and inheritance. To create an object we are going to use a very simple example of a vehicle:

Trying Out Encapsulation

public class Vehicle {
 
    private String fuelType;
    private int wheelCount;
 
    public void setFuelType(String fuelType) {
        this.fuelType = fuelType;
    }
 
    public String getFuelType() {
        return this.fuelType;
    }
 
    public void setWheelCount(int wheelCount) {
        this.wheelCount = wheelCount;
    }
 
    public int getWheelCount() {
        return this.wheelCount;
    }
}
 
public class TestClass {
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle();
        vehicle.setWheelCount(4);
        vehicle.setFuelType("Gas");
        System.out.println("Vehicle wheel count: " + vehicle.getWheelCount());
        System.out.println("Vehicle fuel type: " + vehicle.getFuelType());
    }
}

And if we test this we get:

[cg@h:java] => javac TestClass.java
[cg@h:java] => java TestClass

Vehicle wheel count: 4
Vehicle fuel type: Gas

Did you notice the words, public and private in the above code? These are examples of Encapsulation, and in Java this is how you determine the visibility(scope) of a variable or method. Anything other than the Vehicle class will have no idea what wheelCount and fuelType are, they just know that in order to see or edit those values they will need to call the correct methods.

Getting Started with Inheritance

Ok, back to what we were doing. We have a vehicle now. Let’s create a Car class, and give it something specific to a car, say a color attribute and accessor methods. But we want it to have the same methods as the Vehicle class, and we don’t want to repeat our code. For example, we have the Vehicle class and we add Truck, Van, and Semi classes. If we want them to all have some of the same base methods, that’s a minimum of 4 pieces of repeated code! That’s exactly what we don’t want. To remedy this there’s a concept of inheritance. Let’s see how that would work:

public class Car extends Vehicle {
 
    private String color;
 
    public void setColor(String color) {
        this.color = color;
    }
 
    public String getColor() {
        return this.color;
    }
}

Now Car has access to all the same code that’s in the Vehicle class. Isn’t that cool! Let’s test it out and make sure it works like we want:

public class TestClass {
    public static void main(String[] args) {
 
        Car car = new Car();
 
        car.setFuelType("Gas");
        car.setWheelCount(4);
        car.setColor("Green");
 
        System.out.println(car.getFuelType());
        System.out.println(car.getWheelCount());
        System.out.println(car.getColor());
    }
}

And what do we get as output?

[cg@h:java] => javac TestClass.java
[cg@h:java] => java TestClass
Gas
4
Green

See? It works great! This is the general high level concept of an object-oriented program written in Java. The creation of Objects from Classes and extending from those classes and overwriting code as necessary. While this is not a complete tutorial on Java or programming in general, it touches on some of the key concepts of OOP. I hope you enjoyed reading, and see you for Day 2: TypeScript!

You can view the intro here: Here