Java Working With Class
Written by Ian Elliot   
Article Index
Java Working With Class
What's in a Class
Adding a Class
Why class and objects?
MathExample

The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

We have encountered objects in Java in earlier chapters. When you drag a button onto a form you are working with a Java object. However, a lot of the work is done for you by the NetBeans IDE.

When it comes to creating and working with your own objects you can't expect the same sort of support.

First we need to discuss some of the reasons why object oriented programming is a good idea. 

Why Objects?

Back in the early days of programming there were no objects. We simply wrote code to do a job. A program was simply something that took some inputs and processed these with a combination of if statements and loops. Later we invented functions as ways of grouping together standard tasks. You already know what a function is in the context of objects. In Java we call a function a method but this is getting into some subtle distinctions between things that are very similar. 

For example, you might group together a few lines of code - a loop and an if statement - to find the maximum value of something and call it max(list) (note this isn't properJava but its close).  With max(list) defined you can now avoid having to write the same code ever again. When ever you want to find the maximum of a list you can write max(mynewlist). In a sense a function is a way that you can extend the language - you have added a new verb that finds the maximum. 

This approach to programming was dominant for a long time and programming was about creating lots of functions that would do lots of things. This is the era of modular programming and it still is but today we add objects into the mix.

What happened was that some one noticed that every time you use a function like max you had to say which list it was that needed to be sorted. Not a huge problem but if you think about it for a while you start to realize that a list is a data structure for which sorting is "natural". You have a list and you probably want to sort it. So why not equip every list with a function that sorts it. Now you can simply say mylist.sort() you don't need to say which list to sort - the list knows how to do it. You might think that this is hopelessly inefficient but there are ways to make it just as efficient as basic functions. If you think about it you shold be able to see that

mylist.sort() 

is just another way of writing

sort(mylist)

- from an implementation point of view there is little extra to do. 

At this point you might say that the difference is trivial but once you start to think of bundling data and functions, or methods as they are known in this context then the idea starts to take off and get much bigger than this simple beginning. A method like max works on the object it belongs to and has access to all of the properties of its object so you don't have to tell it things like how long the list is. What you find is that by putting the functions with the data you don't have to specify all of the things you might have to if the function wasn't with the data. It simplifies the whole packaging and using of functions. 

It also creates a more powerful mindset. Now objects are groups of data that know how to do things that are appropriate to them. Lists can find their maximum and sort themselves and so on. Software objects are also like objects in the real world - they have properties and they do things.You can even use this to find ways of implementing software that is based on the real world of objects - software objects like a button can mimic the behaviour of real world object - like buttons. 

The one negative effect of the introduction of objects into programming is that it makes it more difficult to get started. At the core of programming is still the art of putting together instructions, if statements and loops, to make things happen but today you can't just write a function to do the job you have to create an object which combines the data and the many functions that relate to it. This takes time to master and it sees confusing at first. 

Our next problem is finding out how we create an object.  

Class, object and instance

How do you make an object? 

We need to make a clear distinction between the specification for an object and an actual object. 

In particular, we have to make the distinction between a class and an object.

A class is a like a recipe or a blue print for an object.

Consider the button that sits in the Netbean's toolbox. This isn't actually a button you can't click it say. It is a template for a button that you can use to stamp out real buttons on a form. You can think of the button in the toolbar as being a button class and the real buttons that it can create are objects or instances of the class.

We need to learn some jargon.

A class is a specification for an object but it isn't an object.

Using a class to create an object is described as creating and instance of the class.

The act of creating an object from a class is called instantiation.

You also need to know that while buttons and similar are good examples of class and object not all classes and objects have a visual representation. Not all objects are user interface components. So while it helps to think about user interface components when you are learning about class and object things aren't 100% the same.

One big difference is that to create our own objects we first have to create our own class. So let's see how this works.