Introduction to Object-Oriented Programming

Java is an Object-Oriented programming (OOP) language; this is the first hurdle I like us to cross, because every now and then you will be creating what we call class and objects of class in Java.

 

WHAT DOES OOP MEANS?
After many searching and researching about the best and easiest way to introduce OOP to a novice; the best material I ever seen on this topic is: Teach Yourself Java in 21 days, by Laura Lemay and Charles L. Perkins, published in 1996. Below is what I am able to get from this rich material.

Object-oriented programming is modeled on how, in the real world, objects are often made up of many kinds of smaller objects. This capability of combining objects, however, is only one very general aspect of object-oriented programming. Object-oriented programming provides several other concepts and features to make creating and using objects easier and more flexible, and the most important of these features is that of classes.

A CLASS is a template for multiple objects with similar features. Classes embody all the features of a particular set of objects.
For example, you might have a Tree class that describes the features of all trees (has leaves and roots, grows, creates chlorophyll). The Tree class serves as an abstract model for the concept of a tree—to reach out and grab, or interact with, or cut down a tree you have to have a concrete instance of that tree. Of course, once you have a tree class, you can create lots of different instances of that tree, and each different tree instance can have different features (short, tall, bushy, drops leaves in Autumn), while still behaving like and being immediately recognizable as a tree.

An instance of a class is another word for an actual object. If classes are an abstract representation of an object, an instance is its concrete representation.

The terms instance and object are often used interchangeably in OOP language. An instance of a tree and a tree object are both the same thing.

In an example closer to the sort of things you might want to do in Java programming, you might create a class for the user interface element called a button. The Button class defines the features of a button (its label, its size, its appearance) and how it behaves (does it need a single click or a double click to activate it, does it change color when it’s clicked, what does it do when it’s activated?). Once you define the Button class, you can then easily create instances of that button—that is, button objects—that all take on the basic features of the button as defined by the class, but may have different appearances and behavior based on what you want that particular button to do. By creating a Button class, you don’t have to keep rewriting the code for each individual button you want to use in your program, and you can reuse the Button class to create different kinds of buttons as you need them in this program and in other programs.


 

When you write a Java program, you design and construct a set of classes. Then, when your program runs, instances of those classes (Objects) are created and discarded as needed. Your task, as a Java programmer, is to create the right set of classes to accomplish what your program needs to accomplish.

 

Fortunately, you don’t have to start from the very beginning: the Java environment comes with a library of classes that implement a lot of the basic behavior you need—not only for basic programming tasks (classes to provide basic math functions, arrays, strings, and so on), but also for graphics and networking behavior. In many cases, the Java class libraries may be enough so that all you have to do in your Java program is create a single class that uses the standard class libraries. For complicated Java programs, you may have to create a whole set of classes with defined interactions between them.

A class library is a set of classes.

 

BEHAVIOR AND ATTRIBUTES
Every class you write in Java is generally made up of two components: attributes and behavior.
In this section, you’ll learn about each one as it applies to a theoretical class called Motorcycle.
To finish up this section, you’ll create the Java code to implement a representation of a motorcycle.

 

ATTRIBUTES
Attributes are the individual things that differentiate one object from another and determine the appearance, state, or other qualities of that object. Let’s create a theoretical class called Motorcycle. The attributes of a motorcycle might include the following:

- Color: red, green, silver, brown
- Style: cruiser, sport bike, standard
- Make: Honda, BMW, Bultaco

Attributes of an object can also include information about its state; for example, you could have features for engine condition (off or on) or current gear selected.
Attributes are defined by variables; in fact, you can consider them analogous to global variables for the entire object. Because each instance of a class can have different values for its variables, each variable is called an instance variable.

Instance variables define the attributes of an object. The class defines the kind of attribute, and each instance stores its own value for that attribute.

 

BEHAVIOR
A class’s behavior determines what instances of that class do when their internal state changes or when that instance is asked to do something by another class or object. Behavior is the way objects can do anything to themselves or have anything done to them. For example, to go back to the theoretical Motorcycle class, here are some behaviors that the Motorcycle class might have:

- Start the engine
- Stop the engine
- Speed up
- Change gear
- Stall

To define an object’s behavior, you create methods, which look and behave just like functions in other languages, but are defined inside a class.

 

SUMMARY
If this is your first encounter with object-oriented programming, One of the biggest hurdles of object-oriented programming is not necessarily the concepts, it’s their names. OOP has lots of jargon surrounding it. Here’s a glossary of terms and concepts you learned or need to know about OOP

Class: A template for an object, which contains variables and methods representing behavior and attributes. Classes can inherit variables and methods from other classes.
Object: A concrete instance of some class. Multiple objects that are instances of the same class have access to the same methods, but often have different values for their instance variables.
Instance: The same thing as an object; each object is an instance of some class.
Superclass: A class further up in the inheritance hierarchy than its child, the subclass.
Subclass: A class lower in the inheritance hierarchy than its parent, the superclass.
When you create a new class, that’s often called subclassing.
Instance method: A method defined in a class, which operates on an instance of that class. Instance methods are usually called just methods.
Class method: A method defined in a class, which can operate on the class itself or on any object.
Instance variable: A variable that is owned by an individual instance and whose value is stored in the instance.
Class variable: A variable that is owned by the class and all its instances as a whole, and is stored in the class.
Interface: A collection of abstract behavior specifications that individual classes can then implement.
Package: A collection of classes and interfaces. Classes from packages other than java.lang must be explicitly imported or referred to by full package name.

 

Next »