Logout

Object Oriented Programming (OOP) "Template Classes" Part 1

We have used objects already

Object oriented programming is an approach to programming in which programs constantly make use of instances of other classes. These instances are the objects of Object Oriented Programming. So, for example, in reading from the console, we created and used an object of the BufferedReader class; we usuaually call that object "br". And in our GUI applications, we are constantly making and using objects of other pre-made classes, like the JButton class, and the JTextField class; we will have an instance called "entryButton", or "nameTF", for example.

Making our own objects

And in fact, we will not just use pre-made Java classes like BufferedReader and JButton, but will create our own classes which we will use as "templates" for creating objects of our own design. In the example below, we create a class of our own called Team. The Team class file itself has no main method, and so it, itself cannot run. Rather, we make instances of it in our GUI class. You can think of the Team "template" class as being a cookie cutter; it is the model which we use to fashion new instances of Teams based on it's shape, as it were. In the same way that we don't eat the cookie cutter, we don't use the Team class itself in our main method, rather we use the instances of it (the cookies, in our analogy).

Separate class for our object "template"

It could be possible to do all of the programming of all of the kinds of objects we would like to use all in one class. But, for starters, there's no need, since all those other classes have already been made. Furthermore, one big class would be way, way, way too big. A major strategy in computer science is to divide and conquor a big problem. So by making lots of different specialized classes, we can then make instances of those classes (objects) in other classes.

Here is a simple example of a class with a main method that makes instances of another "template class".

TemplateClass.java

/Users/adelaide/Public/Netbeans - All JSR Projects/CEESA Tournament/src/simpleOOPExample/TeamClass.java
 2 package simpleOOPExample;
 3 
 4 
 5 public class Team {
 7     //Attributes
 6     String teamName = 'not set yet';
 7     double averageGoals = -999;
 8     
 9      //Method(s)
10     public Team(char teamNameSent, double averageGoalsSent){
11         teamName = teamNameSent;
12         averageGoals = averageGoalsSent;
13     }
14         
15 }

MainClass.java
/Users/adelaide/Public/Netbeans - All JSR Projects/CEESA Tournament/src/simpleOOPExample/MainClass.java
 5 
 6 package simpleOOPExample;
 7 
 8 
 9 public class MainClass {
10     public static void main(String[] args) {
11         Team example = new Team('Prague',  3.2);
12         Team anotherExample = new Team('Warsaw', 1.6);
13         System.out.println("Team" + example.teamLetter + "has an average of" + example.averageGoals);
14         System.out.println("Team" + anotherExample.teamLetter + "has and average of" + anotherExample.averageGoals);
15     }
16 }
17 
18

Output:

Prague has an average of 3.2
Warsaw has an average of 1.6

In this example, two instances of the Team are made inside the MainClass class. And then their attributes are accessed through dot notation, within a pair of System.out.println lines.

 

Attributes and Methods

All classes contain things and things that can be done with those things. The things are data types, both simple and complex (for example ints, booleans, and arrays). And the things that can be done with those things are the methods. So we can say that classes, at the most fundamental level, are data types and methods. More conventionally we say attributes and methods.

Do remember that the main method is a very special kind of method. It is the method that runs the program. So all of the programs that we have been able to run in Netbeans ran from the main method - even our GUI applications have had a main method, though we ourselves did not code it in, Netbeans autogenerated it.


Object Declaration and Memory

In making an object of a certain class, the main thing we need to do in terms of memory, is find enough available memory to store that object. All objects will have particular memory requirements based on their attributes. So, in our example, 80 bits will be required to store the two attributes (16 bits for the char + 64 bits for the double). It is the new operator that is responsible for finding and allotting that new memory. (new is one of the 50 keywords of Java.)

Anyway, here is a simplified diagram of what is going on at the memory level (there is a bit more to the story, which we shall see later):

Note that another simplification of this first diagram is that it uses only a char letter for the team, rather than a String name.


So, having been place in memory thus, and since they were declared public, they could then be accessed directly with dot notation, as seen in lines 13 and 14 of the ExampleMainClass. (But, again, see the next notes for a better way of doing it, making the attribures private and using get and set methods.)