Object Oriented Programming and Java
Java, like JavaScript, is an object oriented language. You might ask what is an object (in the context of computer languages)? What makes a language object oriented? Why should a language be object oriented? Or, in other words, so what?
Objects are just another way of defining and manipulating data. The advantage of object oriented programming (OOP) is that data is segregated. If an object spawns two child objects, each child is separate from the other. Data modified in one child does not affect the corresponding data in the other child, although both children inherit from the parent.
Procedural languages, like C++, lack this segmentation. A C++ function can change data outside the scope of the function. Any changes made in a function can affect other parts of the program. The data is, in effect, placed in one big bucket. Great care and extensive testing are necessary to ensure the integrity of the whole. As programs increase in size and complexity, the problems increase exponentially.
Java eliminates this problem. Changes made in an object remain inside the object. At least some of the problems of debugging are eliminated.
OOP will require a shift in mindset. Programmers must look at data in an all new way. It is much the same as purchasing your first calculator that utilizes Reverse Polish Notation (RPN). This is not an ethnic slur. RPN is an altogether different way of performing math. Instead of saying something like three plus four equals seven, you would say three enter four plus. In both cases the display reads 7. RPN is in many areas superior to an algebraic calculator, just as OOP is in many areas superior to procedural languages. Java will not sound the death knell of structured programming. It will, however, give programmers a viable choice.
Classes
Classes are the backbone of Java. Classes are the manner in which objects are created and modified. When you create an instance of a class, you create an object. Creating a class (object) gives you access to the methods of the object (class). Classes have methods and inheritance the same as objects. In many ways classes are merely objects by another name.
To use a class, the class must be declared and constructed. By this I mean the first step in utilizing a class in a Java program is to declare it. Once the class has been declared, you must use the new constructor to fully create the object. I will discuss both procedures more fully.
Declaring a Class
The class declaration is a one-line operation:
public class DragonClock
final class SeniorEnglish
class PaintScreen
abstract class BohemianSpaceCadets
As shown above, classes come in four flavors: public, final, abstract, and protected(no specifier).
Public Classes
A public class is a class which can be accessed, used, or extended by any other object, regardless of package. If public classes were thought of in sexual terms, they would have very loose morals.
Also, be aware that a public class must be defined in a file by itself. The name of the file would be PublicClassName.java
For example, the class:
public class DragonClock would be found in file DragonClock.java, and that is the basis for this tutorial.
Final Classes
A final class is one that cannot be extended or modified. Sometimes you will want to have objects that can be read, but not modified. For instance:
public class BrainFunction
public class ArmMovement
public class StomachDigestion
final class HeartBeat
Abstract Classes
An abstract is one that is unfinished. Suppose you have a case where only a fraction of data will change (or will need to be defined) over several instances. The variable parts could be abstract classes. For instance:
public class RolePlayingCharacterStats
public class RolePlayingCombatSystem
public class RolePlayingMonsterList
abstract class RolePlayingMagicUserAbilities
Protected or Default Classes
A protected class is one created with default properties. Unless otherwise specified, all classes have default properties. That is, only classes in the same package can make use of a protected class. Packages are collections of related classes. For instance, FantasyRolePlayingSystem, AcmeInsurancePolicies, and BrokenPoliticalPromises could be examples of packages. The class TwentyYearLevelTerm would be in the AcmeInsurancePolicies package. More than likely it would be Protected because no one would ever insure a role playing character or political promise!
Putting It All Together
In order to learn how to write an applet, we will create a class that we will declare to be public which we will name DragonClock and will reside in filename DragonClock.java. We do not use the new constructor for this instance of class declaration.
In the next lesson we will begin writing the applet.
Next ->
Java and java Development Kit are trademarks of Sun Microsystems.
All material is copyright of P & G Dragonsites and may not be reproduced in whole or in part in any media without prior permission.
For questions or comments please contact us at pgdragonsites@thecenter.zzn.com
Reprinted with permission from the author. Click here to visit the original version
Download this tutorial (~30 KB)
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|