Classes and Objects in BlueJ


Collapse Content

This node will cover the basics of using BlueJ and connect it to using Java. You can view a longer BlueJ tutorial on the BlueJ site.

More About BlueJ

BlueJ lets you interact with Objects in your code directly which helps when learning concepts or to try something out quickly. We will see different actions that you can do with BlueJ and also how its done with real code. You can skip the "real code" parts for now, and come back to them to reference later.

Opening a Project

Open up the People Project in BlueJ

  1. Find the folder where BlueJ is installed and open the examples folder inside.
    On Windows, look in C:\Program Files (x86)\BlueJ or C:\Program Files\BlueJ.
    If you cannot find it, download a copy of examples and unzip it.
  2. Open the people folder and click on the BlueJ package.

(You can also open a project from within BlueJ by clicking on Project > Open Project. )

BlueJ Class Diagram

You should then see the following screen (without the blue markup): Blue-J-screen

BlueJ lets you create and view Classes and Objects in a visual manner. The main pane shows a diagram of each Class in a project and their relationships with each other.

Creating Objects

View animation

As mentioned before, Classes are like blueprints to define specific instances (or Objects) of code. To create an actual object, right-click on a Class and click on a "new" menu item. For example,

  1. Right-click on Staff to get a menu.
  2. Click on the second menu item, new Staff().
  3. You'll get a box asking you to name your new object, but you can leave the default suggestion and click OK.

BlueJ_Create_Object

Notice how an object now shows up on the object bench.

BlueJ_Object_Bench

All you needed when creating that object was its name, but sometimes Objects ask for more information so they can start off with certain data. To see this, let's create one more Object, this time a Student:

  1. Right-click on the Student Class
  2. This time, click on new Student(String, int, String)
  3. You will now be given a prompt for both an Object name and parameters. You can copy the values shown below, and then click OK. Note there are two String values, which need to be enclosed in quotes.

BlueJ_object_with_parameters

This time, you were prompted to fill-in the parameters of the Object, so it could set up its initial values.

Real Code

Invoking an Object's Methods

To call or invoke an object's method, right-click the object and click on a method. For example, right-click on the student1 object and click on String getStudentID.

BlueJ_methods

This will call the method getStudentID which returns a String of student1's ID, which you set when creating the instance. BlueJ will show you a box with student1's ID number. (Note again that it's a String of digits, not a number.) You can view the value of the String in the box, or click on "Get" to create a String Object with that value.

The Staff Class sets the room to a default value, since you didn't provide any value. You can change the defined value by clicking on setRoom(). This will popup with another box to take in String parameter for room. Try setting the room to "blue". Try playing around some more in the BlueJ environment to get an idea of how Classes, Objects and methods work.

Real Code

Inspecting Objects

BlueJ also lets you inspect the properties of an object. Right-click on an object on the object bench and click on "inspect". This will show a popup that displays the properties of that Object. For example, inspect student1, and you'll get the following info:

object-inspector

Inheritance

Did you wonder what those small solid arrows are in BlueJ?

BlueJ_inheritance

They show that one class inherits from another class. In this case Student and Staff both inherit from the abstract Person class.

What does it mean when a sub-class inherits from a super-class? As mentioned, it means you can define methods in the super-class that will be available in the sub-class. The Super's methods do not need to be repeated in the sub-class and they are still available to Objects of the sub-class.

For example, click on your staff1 Object, and go to "Inherited from Person". This will show you the methods that Staff inherits from Person.

inherited-methods

If you do the same thing for student1, you'll see it also has the same methods there, since those methods were all defined in Person. Try clicking around on the different methods to see what they return or to set your own values for the Object's data.

Object
Note there's another menu item, "inherited from Object". Every Class in Java automatically inherits from the Super-duper Class Object. It provides a few different methods that all Objects can use, though they are often changed from their default form.

Real Code

Interacting Objects

Objects are useful because they can interact with other Objects. Objects can also be passed as parameters to methods in other objects. For example, if a Truck Object has a method attachCart, it will need to know what Cart it should attach. To do this, it would take a Cart object as a parameter. When you call attachCart(), you will need to pass it the name of an actual Cart object, such as cart1.

Real Code

Task

  1. Create a single Database instance database1.
  2. Call the addPerson method from database1 which will pop up a box for a Person parameter. Type in student1, the name of the object you want to pass it. (As a shortcut, you can just click on the Object on the Object bench.)
  3. Find the method to list all the entries in the Database, and click it. This will cause the Terminal to pop up. It should show you all the output printed by the database.

Challenge

What is the text that the database lists when you listAll students in it? You should have added Alice and she should have the values mentioned above. Copy and paste the text printed out by the database.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

Now try clicking on the method getRoom() on staff1. What String does it return?
(Leave out quotes, but put the rest.)

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • I got Error when attempting task step 2: Cannot find symbol - variable student1

  • @niveoserenity, sorry if it wasn't clear, did you create a Student Object called student1 first? (as described above) ?

  • Task 1 at the end: do I do it by creating a new class for instance: right click -> new class -> and name it database1 or what ? if yes why I dont have there any otpions for adding like addperson, but basic Open editor, compile, remove, inspect? thanks

  • @calo, you need to create an instance (an Object) of the class Database. So right-click on the icon of the Database class to create an instance of it. (See "Creating Objects" above.)

  • When i open the package the first message i see is: "Initialising virtual machine...Done. "

    At this point i still see the diagonal stripes.

    I click on "Compile" and see the message: "Compiling...Done"

    cont...
  • Well one lunch after finally found out why it was only compiling Staff.

    Step 2 from instructions state to go to examples/people and click on the BlueJ package.

    cont...
  • -_- nope didn't work.... scratch last comment.

  • So solution for my last 3 comments was... used Windows 8 rather than Windows 7. = )

  • i dont have these examples

  • Where is the examples folder in linux?

  • where are located these examples in linux

  • @Jake, I added a download link to the examples folder above.

  • When I create student it create a student like student() and not like new Student(String name, int yearOfBirth, String studentID)

  • The only thing I am getting when I bring up a new database is "inherited from object" and 2 void methods - help!

  • Please help someone, i cant find the listAll anywere!??

    When i compile al previous steps are gone..... And when i dont i cant find listAll either. What to do?

    step for step please?

  • I cant figure this out!

    It does not word for me, i cant find listAll please help. step by step?

  • Dr. Rayyan Wilson, MDSc, PhD, specializing in Acupuncture and Anatomy in the United States of America.I am committed to promoting wellness through holistic practices, offering comprehensive care

    cont...
Contact Us
Sign in or email us at [email protected]