Java Object-Oriented Programming

Java is an object-oriented programming language. Objects can be used quite effectively to represent real-world entities. It makes it easier to solve problems, which is the point of writing a program in the first place.

Object-oriented programming requires an understanding of the following terms:

  • object
  • attribute
  • method
  • class
  • encapsulation
  • inheritance
  • polymorphism

A software object often represents a real object in our problem domain, such as a bank account. Every object has a state and a set of behaviors. By "state" we mean state of being fundamental characteristics that currently define the object. For example, part of a bank account's state is its current balance. The behaviors of an object are the activities associated with the object. Behaviors associated with a bank account probably include the ability to make deposits and withdrawals. The Transactions class contains a main method that creates a few Account objects and invokes their services.The Account class represents a basic bank account. It contains instance data representing the account number, the account's current balance, and the name of the account's owner.

An object's attributes are the values it stores internally, which may be reprensented as primitive data or as other objects. For example, a bank account object may store a floating point number (a primitive value) that represents tha balance of the accounts.

The methods of an object define its potential behaviors. For example, to define the ability to make a deposit into a bank account, we define a method containing programming statements that will update the account balance accordingly.

A class is the model or blueprint from which an object is created. For example, once we define a class to represent the concept of a bank account, we can create multiple objects that represent specific, individual accounts.

An object should be encapsulated, which means it protects and manages its own information. For example, the RollingDie class contains a main method that instantiates two Die objects (as in singular of dice). The Die class contains two data values. It also contains a constructor called Die and four regular methods. The methods of the Die class should be solely responsible for changing the value of the faceValue variable. We should make it difficult, if not possible, for code outside of a class to "reach in" and change the value of a variable that is declared inside that class. This characteristic is called encapsulation.

Classes can be created from other classes by using inheritance. The definition of one class can be based on another class that already exists. One class can be used to derive several new classes. For example, we might create a hierarchy of classes that represent various types of accounts. Let's look at another example, Words program instantiates an object of class Dictionary, which is derived from a class called Book. In the main method, three methods are invoked through the Dictionary object: two that were declared locally in the Dictionary class and one that was inherited from the Book class.
The Book class is used to derive the Dictionary class using the reserved word extends in the header of Dictionary. The Dictionary class automatically inherits the definition of the setPages and getPages methods, as well as the pages variable.
Although the Book class is needed to create the definition of Dictionary, no Book object is ever instantiated in the program. An instance of a child class does not rely on an instance of the parent class.
Inheritance is a one way street. The Book class cannot use variables or methods tht are declared explicitly in the Dictionary class. For instance, if we created an object from the Book class, it could not be used to invoke the setDefinitions method. This restriction makes sense because a child class is a more specific version of the parent class. A dictionary has pages because all books have pages; but although a dictionary has definitions, not all books do.

Polymorphism is the idea that we can refer to multiple types of related objects over time in consistent ways.
Consider the following line of code:

obj.doIt();

If the reference obj is polymorphic, it can refer to a different types of objects at different times. So if that line of code is in a loop, or if it's in method that is called more than once, that line of code could call a different version of the doIt method each time it is invoked.
Let's explore an example that uses a hierachy to pay a set of employees of various types. The Firm class contains a main driver that creates a Staff of employees and invokes the payday method to pay them all.
The Staff class maintains an array of objects that represent individual employees of various kinds. The staffList array is filled with polymorphic references.
The payday method of the Staff class scans through the list of employees, printing their information and invoking their pay method is polymorphic because each class has its own version of the pay method.
The StaffMember class is abstract. It does not represent a particular type of employee and is not intended to be instantiated. Rather, it serves as the ancestor of all employee classes and contains information that applies to all employees. Each employee has a name, address, and phone number, so variables to store these values are declared in the StaffMember class and are inherited by all descendants.
The Volunteer class represents a person that is not compensated momentariy for his or her work.
The Employee class represents an employee that gets paid at a particular rate each pay period.
The Executive class represents an employee that may earn a bonus in additional to his or her normal pay rate.
The Hourly class represents an employee whose pay rate is applied on an hourly basis.

Go to Top of Page