What is Object-Oriented Programming Core Java


A class is the template or blueprint from which objects are made. Thinking about classesas cookie cutters. Objects are the cookies them selves. When you construct an object froma class, you are said to have created an instance of the class.

As you have seen, all code that you write in Java is inside a class. The standard Java library supplies several thousand classes for such diverse purposes as user interface design, dates and calendars, and network programming. Nonetheless, you still have to create your own classes in Java to describe the objects of the problem domains of your applications.

Encapsulation (sometimes called information hiding) is a key concept in working withobjects. Formally, encapsulation is nothing more than combining data and behavior in onepackage and hiding the implementation details from the user of the object. The data in an object are called its instance fields, and the procedures that operate on the data are called its methods. A specific object that is an instance of a class will have specific values for itsinstance fields. The set of those values is the current state of the object. Whenever youinvoke a method on an object, its state may change.

The key to making encapsulation work is to have methods never directly access instance fields in a class other than their own. Programs should interact with object data onlythrough the object’s methods. Encapsulation is the way to give the object its “black box”behavior, which is the key to reuse and reliability. This means a class may totally changehow it stores its data, but as long as it continues to use the same methods to manipulatethe data, no other object will know or care.

When you do start writing your own classes in Java, another tenet of OOP makes thiseasier: classes can be built by extending other classes. Java, in fact, comes with a “cosmicsuperclass” called Object. All other classes extend this class.

When you extend an existing class, the new class has all the properties and methods ofthe class that you extend. You supply new methods and data fields that apply to yournew class only. The concept of extending a class to obtain another class is called inheritance.See the next chapter for details on inheritance.


To work with OOP, you should be able to identify three key characteristics of objects:

  • The object’s behavior—What can you do with this object, or what methods can youapply to it?
  • The object’s state—How does the object react when you apply those methods?
  • The object’s identity—How is the object distinguished from others that may have thesame behavior and state?

All objects that are instances of the same class share a family resemblance by supporting the same behavior. The behavior of an object is defined by the methods that you can call.

Next, each object stores information about what it currently looks like. This is theobject’s state. An object’s state may change over time, but not spontaneously. A changein the state of an object must be a consequence of method calls. (If the object state changed without a method call on that object, someone broke encapsulation.)

However, the state of an object does not completely describe it, because each object has adistinct identity. For example, in an order-processing system, two orders are distincteven if they request identical items. Notice that the individual objects that are instancesof a class always differ in their identity and usually differ in their state.

These key characteristics can influence each other. For example, the state of an object caninfluence its behavior. (If an order is “shipped” or “paid,” it may reject a method callthat asks it to add or remove items. Conversely, if an order is “empty,” that is, no items have yet been ordered, it should not allow itself to be shipped.)

Identifying Classes

In a traditional procedural program, you start the process at the top, with the main function. When designing an object -oriented system, there is no “top,” and newcomers toOOP often wonder where to begin. The answer is, you first find classes and then youadd methods to each class.

A simple rule of thumb in identifying classes is to look for nouns in the problem analysis .Methods, on the other hand, correspond to verbs.

For example, in an order-processing system, some of these nouns are

  • Item
  • Order
  • Shipping address
  • Payment
  • Account

These nouns may lead to the classes Item, Order, and so on.

Next, look for verbs. Items are added to orders. Orders are shipped or canceled. Paymentsare applied to orders. With each verb, such as “add,” “ship,” “cancel,” and “apply,” you identify the one object that has the major responsibility for carrying it out. For example,when a new item is added to an order, the order object should be the one in chargebecause it knows how it stores and sorts items. That is, add should be a method of theOrder class that takes an Item object as a parameter.

Of course, the “noun and verb” rule is only a rule of thumb, and only experience canhelp you decide which nouns and verbs are the important ones when building your classes.

Relationships between Classes

The most common relationships between classes are

  • Dependence (“uses–a”)
  • Aggregation (“has–a”)
  • Inheritance (“is–a”)

The dependence, or “uses–a” relationship, is the most obvious and also the most general. For example, the Order class uses the Account class because Order objects need to access Account objects to check for credit status. But the Item class does not depend on the Accountclass, because Item objects never need to worry about customer accounts. Thus, a classdepends on another class if its methods use or manipulate objects of that class.

Try to minimize the number of classes that depend on each other. The point is, if a classA is unaware of the existence of a class B, it is also unconcerned about any changes to B! (And this means that changes to B do not introduce bugs into A.) In software engineering terminology, you want to minimize the coupling between classes.

The aggregation, or “has–a” relationship, is easy to understand because it is concrete; forexample, an Order object contains Item objects. Containment means that objects of class Acontain objects of class B.

NOTE:Some methodologists view the concept of aggregation with disdain and prefer to use a more general “association” relationship. From the point of view of modeling, that is understandable. But for programmers, the “has–a” relationship makes a lot of sense. We like to use aggregation for a second reason—the standard notation for associations is less clear.

The inheritance, or “is–a” relationship, expresses a relationship between a more specialand a more general class. For example, a RushOrder class inherits from an Order class. Thespecialized RushOrder class has special methods for priority handling and a different method for computing shipping charges, but its other methods, such as adding itemsand billing, are inherited from the Order class. In general, if class A extends class B, class Ainherits methods from class B but has more capabilities. (We describe inheritance morefully in the next chapter, in which we discuss this important notion at some length.)

Many programmers use the UML (Unified Modeling Language) notation to draw classdiagrams that describe the relationships between classes. You can see an example of sucha diagram. You draw classes as rectangles, and relationships as arrowswith various adornments. Table below shows the most common UML arrow styles.

A class diagram

A class diagram

NOTE: A number of tools are available for drawing UML diagrams. Several vendors offer high-powered (and high-priced) tools that aim to be the focal point of your development process. Among them are Rational Rose and To gether. Another choice is the open source program ArgoUML. A commercially supported version is available from GentleWare. If you just want to draw a simple diagrams with a minimum of fuss, try out Violet.

UML Notation for Class Relationships

UML Notation for Class Relationships

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics