OOP Intro

From WikiEducator
Jump to: navigation, search
Road Works.svg Work in progress, expect frequent changes. Help and feedback is welcome. See discussion page. Road Works.svg

The Code Editor

This editor is part of the IDE (BlueJ is an example of an Integrated Development Environment). A nice thing about it is the way it colour codes the different types of text for us. For example, the nice blues and greys are comments.

We will delete most of this code for now.

BlueJ is useful for teaching as it does allow us to create our own templates similar to this one, but we won't do that just yet.

Let's first change the comments and then enter the Java code as follows:

  public class Person 
    String name = "Richard"

This is a class – the basis of pretty much everything in Java. It isn't very useful yet.

You can cut and paste the code from the workbook if you like, making sure to replace all of the existing code . Or you can replace the bits that are different.

We will break everything down and define some jargon. Getting the jargon right is important as otherwise we can't communicate when we need help from an expert. Different experts have slightly different jargon, unfortunately, but we will try to be consistent.

Compilers and interpreters

SB machinecode.jpg

These are computer programs that turn ordinary text into machine code (the stuff that computers actually run on the hardware).

The compiler in Java translates the source code (our Person file) into bytecodes - a compact form – which is, in turn, sent to an interpreter to be converted to machine code run on the computer.

If the compiler cannot turn the source code into bytecodes, an error is given (you will see a lot of these). This is known as a syntax error.

To help you out a bit, we will put definitions of these terms in a word list every now and then. With that background, let's examine the code:

A class

  public class Person

A class is a template (or blueprint) for object instances. Classes are generally public and, by convention, start with a capital letter (upper case), followed by small letters (lowercase). Public means that other classes can create instances from the template – you can actually do something with an instance.

The entire class code must appear between two braces, one opening and one closing:


It is conventional to line them up vertically (or sometimes horizontally). You will notice that after the opening brace, the source code is indented. This makes it more readable for humans.

Main menu: home | next | previous