Java Lamp

If you don’t know what Java is, it’s an object oriented programming language. I’d like to discuss some of the broad strokes of Java and OOP (Object Oriented Programming).

Plato, the philosopher, championed the idea of platonic forms. These can be thought of the ideal, most basic state, to mean the definition of a thing. When we say chair, we each have an image in our minds of what a chair must be. A fork, for instance, can be sat upon, but it is not a chair. Why? Because it doesn’t mean the criteria to be classified as a chair. This brings us to the first most important concept in Java. Everything comes from one platonic, most basic Ideal: Objects. Objects are the supreme abstract concept of what anything can be. It has behavior of it’s own, but is so abstract, as to be useless on it’s own. So what utility can we get from Objects? We extend them.

In Java, everything is done with classes, and all classes are extensions of Objects, the top most class in the parent child chain. Objects are sort of the Eve (from the story) of all classes. A class might not be a direct descendant of an Object, but go back far enough and an Object is what you get. A class is just an Object with some additional characteristics, like variables and methods. So let’s take our platonic ideal example and run with it. What is an object in the real world? An object can be almost anything in the real world. If we never differentiated between different types of objects, none of us would know what anyone else is talking about. So we classify things differently (class). How about a lamp? That’s an object. It has its own characteristics, like size, style, etc. So if we made a lamp in Java it would look something like this.

public class Lamp {

int size;

public Lamp(s) {

size = s

}

}

Anytime we want a lamp, we create a new instance of it and there it is, with any variables or methods we give to the lamp class. So if we wanted to add a method like ‘illuminate’, we would add an instance variable, like isOn, which would be defaulted to false, and in the illuminate method, we would check if isOn is true or false. if isOn is true, we do nothing, if it’s false, we set isOn to true. This is an example of instance methods and variables, which are methods and variables which only exist as long as the Object (or Lamp) in question is being referenced.

When I say referenced, what do I mean? Well you don’t really interact with Object’s directly, you interact with references to those Objects. A good way of thinking about this with remote controls. Imagine if there were no buttons on your TV. You still want to use it so you would use a remote, which allows you to tell your TV to do all the things a TV is supposed to be able to do(on/off, volume up/down, etc.). This is a remote that never runs out of batteries. It will only stop working if you tell it to signaling to a particular TV, but once you tell it to stop referencing that TV, if no other remotes reference it, the TV is gone for good. It’s dead, Jim. In Java, you could set a pointer from one TV to another, and the first TV is now available for garbage collection, kinda like how in real life, when you get a new TV you throw your old one out. You can also set Object references to null, and this will also allow that an Object to be garbage collected.

We’ve covered Objects, and Classes, but now it’s time for subclasses. Let’s say I was a lamp manufacturer and I was cranking out a steady supply of nondescript lamps. Someone contacts me and want’s a different kind of lamp, a so called “Lava Lamp”. Well all Lava Lamps are Lamps, but not all Lamps are Lava Lamps. This would be an example of a subclass. A subclass inherits the behavior and properties of the superclass, in this case lamp, but has it’s own unique characteristics as well. Maybe the illuminate method is inherited, but the size variable is defaulted differently. Or maybe it has its own methods in addition, or maybe its illuminate method is different from the lamp superclass illuminate method. That’s right, subclasses can overwrite superclass characteristics, just not the other way around. Think of it as parent to child.

What about something that can inherit from two things simultaneously? For instance let’s say we had a dog subclass. Dogs are animals and dogs are also pets, but not all animals are pets, and not all pets are animals (robot dog). Interfaces give us a handy work around for this inheritance issue. A subclass can extend a superclass, and implement an interface at the same time. Interfaces allow us to define characteristics for a class that inherits from someplace else.

Another interesting part of Java is static variables. These are variables shared throughout all instances. Back to our manufacturing example, let’s say I was keeping count of all the lamps I manufactured. That number would be independent of any individual characteristic of a particular Lamp. The creation of any Lamp would affect the static variable across all Lamps. There can be static variables as well as static methods. When a method is static, this means it doesn’t rely on instance variables. In a Java program, at least one class must have the following line for anything to happen:

public static void main(String[] args)

See the static? You also probably see the keyword void, which just means this method does not return anything. Java is a typed (as in type declaration) and an easy way to think of it is with pool (or billiards for the old school). You have to say what you’re going to do before you actually do it. You have to say which ball you’re going to hit, and into which pocket you’re going to hit it. In Java you have to say what type of thing a variable is, and what type of variable a method returns. Void is the equivalent of saying scratch. If you want to return a number, you would say, int, short, byte, long, or to add decimals, double or float. For words, you would say String. For true or false, boolean, and so on. The important take away is that you have to say it or you cant do it. There are a few ways around this. One of the ways is by using type ‘any’, but this sort of defeats the purpose. Another way is using generics. Generics are a way of saying that you method will take a variable input, and whatever type that variable is, is the type of variable that will be returned. This is useful if you want to pass different class objects to a method, but don’t want to be too specific.

I hope this information was helpful, let me know if you have any burning question or comments and I’ll address them in my next post. Thanks!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.