Previous |
Next |
Java is an Object Oriented (OO) programming language. Object Orientation is the latest in a series of methods to attempt to make programming easier. The heart of programming is managing complexity. There is a joke about how complex it was to program a computer in the early days that goes.
Old Programmer 1
When I was a lad all we had to program with was zeroes and ones
Old Programmer 2
Zeroes and Ones, we DREAMED of zeroes and ones, all we had was zeroes.
The point of the joke is that at the very lowest level ever computer program really is a complex arrangement of zeroes and ones (binary numbers). The very first programmers really did have to write programs in terms of zeroes and ones.
To create a working program involves organising something of awesome complexity and tedium. There is a saying that programming in the early days was like writing out the works of William Shakespeare using a hammer and chisel, you could do it but you would be far better off with a tool that was designed for the job. In the development of programming some of the big steps have been the move from machine code to assembler (hammer and chisel), and from assembler to structured modular programming (quill pen). In my view Object Orientation is like moving to a word processor with spell correction and voice recognition.
Part of the philosophy behind Object Orientation is that programmers should be able to use “off-the shelf” components in the same way that engineers do in the real world. Thus when an electronics designer decides to create a new graphics card, or washing machine controller they don't create every integrated circuit from scratch. They generally use off the shelf components with some custom configuration.
In the world of software design there has been a long and rather unfortunate history of constantly "re-inventing" the wheel. It seems that there are a million ways of performing common programming tasks such as searching, and sorting. If programmers could re-use off-the shelf components they could get on with the real creative part of software creation rather than getting bogged in the low level details.
Object oriented programming languages were not really practical in the early days of computing because computers were not powerful enough to manage the business of dealing with the objects. The programmer had to do more of the thinking and organising for the computer, it couldn't really manage itself. This was not so much of a problem because Computers were very, very expensive and programmers were relatively cheap. Today computers are cheap and very powerful and programmers are relatively expensive.
Object Orientation in Java (and in most OO languages) is performed using the concept of classes, self contained bundles of code and data that have their own “behaviour”. Classes are generally named as a thing rather than an action. Thus Java supplies a class for String and a GUI button. The names of behaviours tend to be named as doing words or verbs. Thus the String class has a method called compareTo which performs the action of comparing to another String.
A class is a template for creating an "instance" or Object based on that template. You can think of classes as a way of creating your own data types. In older languages such as C or Pascal you had predefined types such as int or float. In Java you can define your own types that wrap up both data and code, and you can use classes that come with the Java language or those provided by other programmers. For example when you are creating a graphical interface and you needed to create a button yoyouould create an instance of the JButton class and take advantage of the data and functionality that comes with it. It is important to understand the difference between a class and an instance of a class. If you were creating a system for managing employees you might come up with a Person data type that could be passed around your program, stored and assigned in a similar way that a variable in other programming languages is managed.
If you consider a programmer has written the Java code that will create a Computer class. This class is the blueprint or plan for a Computer, and you might write code that create multiple copies of instance of the Computer. Thus to go back to the plan analogy the designers within Mega Computer Corp create the plans for building its Super Fab Laptop. It is during the manufacturing process that the plan is turned into physical instances of the design. The idea of an instance of the design is used within Java as a very in the form “instantiated”, i.e. A class may involve “instantiating” multiple copies of the String class for each name to be manipulated. Each instance might be slightly different in that they might have different amounts of Ram or a different size hard disk but they are still all created from the same plan. In the same way a Java class can be used to create multiple instances, each instance may have different attributes at runtime.
To compare this with non Object languages, it is like the difference between the data type int and a variable that is of the type int. Thus you never assign a value to int, but you can assign a value to an item, or variable that had the type int. In a similar way if you created a class of type Person you would never assign a value to the name field of Person, but you first have to create an instance or variable with the type Person. (Actually there is an exception to that statement that involves using the keyword static, but that is a slightly unusual exception to the general rule).
The "behaviour" of an object is set up in its methods which are callable chunks of code in a similar way to subroutines in structured programming languages such as C or Pascal. The difference between a method in Java and a function in C is that in Java the method belongs to the instance of a class, whereas in C a function belongs to the currently running program. If you are new to Object Orientation the terminology can be a little confusing at first but it soon becomes second nature. In Java every object is a descendent of a great grandparent class called Object. The use of methods is sometimes referred to as “sending a message to an object”, a terminology I do not find particularly helpful, but it is widely used.
If a method is the means of sending a message, then the contents of the message are the parameters sent to a message. When defining a method the possible parameters are called the arguments. The terms arguments and parameters are easily confused, but parameters are the actual values sent, whilst the arguments are the possible values. When defining a method each argument must be given a data type and a name. Thus the following method has two parameters of type integrated
setSize(int iHeight, int iWidth)
When a program is running this method might be called with the following parameters
setSize(10,10)
You may have used programming languages (e.g. some versions of Visual Basic) that do not require you to give a data type to variables. This can seem easier for the programmer, but in the long making a language “strongly typed” has advantages both for the programmer and the compiler designers.
![]()
|
Java variables must be given a type before they are used, it is a “strongly typed” language |
A language that knows the types of variables can make more efficient use of resources and from a programmer point of view it can help highlight errors before they become a problem. Thus if you have a method that expects a string and you pass it a number this can be highlighted at compile time. Think of it as a little additional safety..
The return type of an object is the way a method brings information back to a calling object. Every method definition must also have a return type. If no data is to be returned from a method then it must be marked as having a void return data type. A method can only have one return type.
The combination of method name, parameters and return types are known as the method signature. The following are some examples of method signatures.
Void amethod(int iage){}
int amethod(int iage, String name){}
Objects as defined by sun http://java.sun.com/docs/books/tutorial/java/concepts/object.html
Inheritance is possibly the most important feature of Object Orientation, the other two being polymorphism and abstraction. Programming language inheritance borrows ideas from the real world to the degree that the terms parent, child and grandparent are used.
The use of inheritance in Object Oriented languages allows you to take an object and create a new version of it that inherits its functionality, and then add your own modifications. The programmer does not need to understand, or even have access to the source code of the object that is being inherited. An analogy might be a custom car designer who uses a circuit board for controlling some aspect of the engine performance. The component can be added in like a "black box" with its external functionality understood, but with no knowledge necessary of exactly how it implements the functionality within the box.
In addition to being able to re-use existing component in your source code, Java development tools allow you to re-use visual components. If you have ever seen a development environment like Visual Basic or NetBeans you will be familiar with idea of a "palette" of tools that can be dragged and dropped onto a form and then the programmer changes properties to suit the current use.
Sun invented a technology it calls JavaBeans to allow this to happen, so programmers can drop a control such as a button or text box onto a form and then modify features such as the size or color without having to look at the underlying code of the Object.
I
llustration
1: NetBeans Tool Palette
The following diagram illustrates the idea of inheritance. Both Canine and Feline are sub classes of Animal. This relationship is called an “is a” relationship, i.e. a Canine “is a” animal (OK for correct English that should be is an animal, but you get the picture. They share common characteristics. Tiger and Lion are sub classes of the class Feline. If you were actually defining an Animal class it might have characteristics such as HeartBeat. When you go to sub-class Animal, the child class would get the HeartBeat characteristic “for free”, i.e. it would be inherited and you wouldn't have to write any code to give it this characteristic.
This style of diagram with an arrow going from the sub class to the parent class is based on UML or Unified Modelling Language. It is the fashionable ObjectOriented successor to diagrams such as flow charts and Data Flow diagrams. I won't go into huge detail about UML, but it is a subject worth knowing as it is getting very common to see job descriptions that ask for a knowledge of UML. You can find out more about UML at http://uml.tutorials.trireme.com/
I
llustration
2: Animal Inheritance Tree
Polymorphism is a key feature of object orientation but it is possible to write a working Java program without understanding it, so I will leave coverage of this topic till chapter 6.work on the assumption that you mainly want to get some programs working.
Other sources
OO Programming according to wikibooks
http://en.wikibooks.org/wiki/Computer_programming/Object_oriented_programming
A history of SmallTalk (an early Object Oriented language)
http://www.smalltalk.org/smalltalk/TheEarlyHistoryOfSmalltalk_Abstract.html
Principles of Object-Oriented Programming
http://cnx.org/content/col10213/latest/
Previous |
Next |