Article

Home » Server-side Coding » Java and J2EE » Object Oriented Concepts in Java - Part 1

About the Author

Kevin Yank

author_kev1 Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote Build your own Database Driven Website using PHP and MySQL, a practical step-by-step guide published by SitePoint, and he's co-author of the SitePoint Tech Times, a bi-weekly newsletter for technically-minded web developers. Kev believes that any good webmaster should have seen at least one episode of MacGyver.

View all articles by Kevin Yank...

Object Oriented Concepts in Java - Part 1

By Kevin Yank

May 11th, 2001

Reader Rating: 9

Page: 1 2 3 4 5 6 7 Next

A strong grounding in Java allows a developer to do more in less time than he or she could do using any other single programming language out there today. With Java, you can build complete applications featuring everything from accelerated 3D graphics and other multimedia features to strong cryptography and network connectivity. On the Web, Java can be used on the client side to create applets (once over-hyped but still useful, these small programs can run right inside a Web page with the full power that Java has to offer), and on the server side to create dynamic Web pages using Servlets and JavaServer Pages, which you'll be seeing a lot more of later in this series.

To take full advantage of the Java language, you need a good appreciation of object oriented programming concepts. This article is the first of two parts that will teach you everything you need to know about object oriented programming in Java. Buckle your seatbelts and get set for a wild ride, because not only are these some of the most difficult concepts of the Java language to grasp, but they're also some of the most exciting!

This article is the third in a series of articles aimed at teaching you the Java language with an eye towards the development of dynamic Web sites using Java technologies on the server side, and picks up right where the previous article, Java Language Basics, left off. If you have not read the two articles that came before this one, I would suggest going back and starting from the beginning, because subjects like compiling and running Java programs and basic features of the language such as variables and control structures are assumed knowledge from here on in!

But First, Some Jargon

As I have said before, writing programs (or Web pages) in Java is all about constructing a web of interrelated software components that work together to get the job done. These components are called Java Objects.

There are many different kinds of Java Objects, and in fact a big part of programming in Java is creating your own types of Objects. To create a new type of object that you can use in your Java programs, you have to provide a blueprint of sorts that Java will use to create new Objects of this type. This blueprint is called a Java class.

Fig. 1: Instantiating two Trees from the Tree classLet's look at a conceptual example to help these ideas take hold. Say your program needed to keep track of a group of trees in a forest; specifically, say it needed to keep track of the heights of these trees. Fig. 1 shows an example of the class and objects that you might create as a Java programmer working on this program.

On the left we have a class called Tree. This class defines a type of Java Object -- a Tree -- that will serve as the blueprint from which all Tree Objects will be created. The class itself is not a Tree; it is merely a description of what a Tree is, or what all Trees have in common. In this example, our Tree class indicates that all Trees have a property called 'height'.

On the right, we have two actual Tree Objects. These are Trees, and they were created based on the blueprint provided by the Tree class. These Objects are said to be instances of the Tree class, and the process of creating them is called instantiation. Thus, we can say that by instantiating the Tree class twice, we have created two instances of the Tree class, two Objects based on the Tree class, or just two Trees. Notice that in creating these Objects we have assigned a value to their height property. The first Tree is 2 meters high and the second is 5 meters high. Although the values of these properties differ, this does not change the fact that both objects are Trees. They are simply Trees with different heights.

Classes don't only define properties of Objects; they also define operations that may be performed by those Objects. Such operations are called methods in object-oriented languages like Java. Continuing with our Tree example, we could define a method called 'grow' in the Tree class. The result of this would be that every Tree Object would then be able to perform the grow operation as defined in the class. For instance, performing the grow operation on a Tree might increase its height property by one meter.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links