Java Working With Class
Java Working With Class
Written by Ian Elliot   
Thursday, 18 August 2016
Article Index
Java Working With Class
Hands-on example
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

covericon

Contents

  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    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.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. 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 work 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 but 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. 

What's in a Class?

A class definition looks a lot like the sort of Java code we have been writing up to this point. This is a good thing because it looks familiar but it is also a bad thing because you need to keep in mind that you can't acutely run the code in a class unless you first create an instance of it.

That is a class definition is a template for a chunk of program. It can include variables and functions but when you write it the program doesn’t actually exist anywhere all you have is a recipe for making the program when you want to.

For example, if you write a simple class called Point which is going to be used in our programs to represent a 2D point with an x and y co-ordinate. That is a point object is going to store two numbers representing x and y as properties of the object, its class definition would be -

public class Point {
    public int x;
    public int y;   
}

 

Don’t worry for the moment about the use of public, all this means is that class and variables are accessible from outside of the class. 

Concentrate on the fact that what you have defined here is a template that can be used to “stamp out” multiple copies of this code object.

At the moment there is no variable called x and no variable called y allocated in memory waiting for you to use them. To make something happen you have to take the class definition and use it to create an example, or an "instance", of the class or an object. 

Unfortunately in Java creating an object is a bit more complicated than creating a simple variable but the process is more or less the same once you have untangled it. Firstly you need a variable of the right type to store the new object. This you create just as you would any other variable.

For example,

Point Current;

declares a variable called Current suitable for storing a point object i.e an instance of the Point class. 

Compare this with

int x;

which declares a variable suitable for storing an integer.

After the variable has been declared it doesn’t actually contain an example of the class - just in the same way x doesn't contain an particular integer. It’s just ready and waiting to store a new class and the keyword “new”  is exactly how you go about making an instance of the class.

For example

Current=new Point();

creates an instance of the class and stores it in Current

Properties

We have already met properties as part of our look at Swing components but there is a more basic form of property that we need to master first.

We have just created an instance of the Point class. Now there are two integer variables, called x and y, waiting for you to store data in them but you don’t refer to them just as x and y. To gain access to any of the components or “members” of an instance of a class you have to specify the particular instance name as well as the member name.

Put more directly to store something in x you would write

Current.x=10;

In general when you create an instance called myobject you refer to any of its members using a fully qualified name:

myobject.myproperty

If you are wondering why it isn't

Point.x=10;

then you need to remember that it is the instances you create from the Point class that are objects you can work with. Point is simply a template that you can use to create Point objects.

Point is a class not an object.



Last Updated ( Thursday, 18 August 2016 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2016 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.