From the Trenches of the Enterprise Software

Yakov Fain

Subscribe to Yakov Fain: eMailAlertsEmail Alerts
Get Yakov Fain via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Java Developer Magazine

Java Developer : Article

Intro to Object-Oriented Programming with Java

Lesson 2

Java is an object-oriented language and Java programs consist of classes that represent objects in the real world. Classes in Java may have methods and attributes.

Let's create and discuss a class named Car. This class may have one or more methods, which can tell what the objects of this class can do: start the car, stop it, accelerate, lock the doors, and so on.

This class also may have some attributes or properties: color of the car, number of doors, size of engine, and so on.

Our class Car may represent some common features for many different cars: all cars have such properties as color and the number of doors, and all of them perform similar actions. We can be more specific and create another Java class called ToyotaCorolla. It's still a car, but with some properties specific to the model Toyota Corolla.

We will be often using such term as an object, which is an instance of a class. The phrase "to create an instance of a class" means to create a copy of the object in the computer's memory, based on the definition of this class.

Factory specifications of a Toyota Corolla plays a similar role as definition of Java classes. The process of building real cars based on these specs is an equivalent to creating instances of this class in Java.

In many cases, a program can't use the Java class until its instance has been created. Obviously, you can create thousands of cars based on the same Toyota Corolla specifications. Even though they all represent the same class, they may have different values in their properties - some of them are red, some of them have two doors, while others have four, etc. In other words, we may create multiple instances of the class Toyota Corolla:

class ToyotaCorolla{
    String color;
    int numberOfDoors;
    void startEngine {
    void stopEngine {

Data Types

Java variables have to be declared before usage - they must have an assigned data type. There are 8 primitive data types in Java: 4 data types are for integer values, 2 are for values with a decimal point, 1 char, and 1 boolean (allows only the values true or false).

All of these primitives have corresponding wrapper classes that contain useful methods dealing with respective data types.

The data type char allows you to store only one character, while classes String or StringBuffer are used for holding a longer text, i.e.

String lastName="Smith"; char grade = 'A';

Please note that the char data type uses 2 bytes of memory to store the data.

Below are some examples of variable declarations and initializations.

int chairs = 12;                            
boolean cancelJob = false;         
double nationalIncome = 23863494965745.78; 
float hourlyRate = 12.50f;   // add an f at the end of         
                             //float  literals
long totalCars  = 4637283648392l;  // add an l at the end              
                                   // of long literals

The Tax Calculation Program

Let's design and write a program that will calculate the state tax.

First, we need to decide what Java class(es) we need to write. Second, we'll think about properties and methods that our class(es) should have.

Start your text editor and enter the following:

class Tax{

The open and close curly brackets are used in various contexts to enclose the body of a class, a method, and some other Java elements. Every open curly brace must have the closing one. Save your class in the file called Tax.java. You can't go wrong with that. Let's think about the data that this class would need to perform the state tax calculation.

Obviously, you will need to know the gross income of a person for the last year. This is a good candidate for a property of this class. Properties in Java are represented by variables. Before declaring a variable for the gross income, we need to decide what types of data we'll be storing in it. These are some of the Java data types:

int, double, float, char, String...

Let's add the variable grossIncome of the data type double to our class, because this type allows numbers with a decimal point:

 class Tax{   
       double grossIncome;

We also need to know what state the person lives in - different states may have different taxation rules. Possible values of this variable are: "NY", "NJ", "CT", etc. The String data type represents text data:

class Tax{   
       double grossIncome;
       String state;

Let's add one more property for dependents. This will be an integer variable, since a person can not have two and a half dependents.

class Tax{   
       double grossIncome;
       String state;
       int  dependents;

It's time to add some methods to our class. We definitely need to be able to calculate the state tax based on the values of gross income, number of dependents, and state. Let's create a method called calcTax():

class Tax{   
  double grossIncome;
  String state;
  int  dependents;
  public double calcTax() {
    return 234.55;  //  returning a hard-coded value

This method's signature tells us the following:

  • Any external class could access this method (public).
  • This method will return a value of type double.
  • The name of the method is calcTax.
  • This method does not need any values from outside - the empty parentheses mean that the method does not have any arguments.
How do we decide if a method should return a value? If your method performs some calculations and has to give the resulting value back to a calling program, it has to return a value. If, for example, a method just prints some information, it may not need to return any value, but you still need to mention this in a method signature by using a special keyword void:

public void printCustomers() {...}

Java has a return statement and this is how a method returns data contained in a variable myResult to a calling program:

return myResult;

Our class Tax has to be instantiated before we start using it. Let's create one more class called TestTax. This class will just instantiate and use our class Tax. Here's what the class TestTax should do: 1. Create an instance of the class Tax. 2. Assign some values (gross income, state...) to the variables of the class Tax. 3. Call the method calcTax(). 4. Print the result on the screen.

The class TestTax will be stored in a separate file named TestTax.java.

class TestTax{
     public static void main(String[] args){
            t.grossIncome= 50000;  // assigning the values
            t.dependents= 2;
            t.state= "NJ";
            double yourTax = t.calcTax(); //calculating tax 
           // Printing the result 
           System.out.println("Your tax is " + yourTax);

In the code above, we've declared a variable t of type Tax.

The method main() is an entry point to our program. This method creates an instance of the class Tax using the Java operator new. The variable t points to a place in the memory where the Tax object was created. From now on, if we want to work with the class, we will be using the variable t.

The following three lines assign values to the properties of the object Tax.

After that, we'll calculate tax by calling the method calcTax() and the result will be assigned to our variable yourTax. The last line of method main() just displays the result on the system console.

At this point we already have two classes communicating with each other (the TestTax and the Tax).

To make our example more realistic, let's add some code to perform tax calculations in the method calcTax().

Let's say if the gross income was less that $30,000, we will take 5% for state tax. If it's greater than $30,000, we will take 6%.

public double calcTax() {         
  double  stateTax=0;
  if (grossIncome < 30000) {
    stateTax= grossIncome*0.06;
  return stateTax;

Variable Scopes

If you declare a variable inside any method, the variable has a local scope (the variable stateTax is local). This means that it's visible only for the code within this method. When the method is finished, the variable automatically gets destroyed. If a variable has to be visible through more than one method in a class, you should declare it on a class level. In the class Tax, grossIncome, dependents, and state are class variables. These variables are "alive" when the class exists in memory. They could be shared and reused by all methods within the class and they can even be visible from external classes, for example TestTax class is accessing them.

In the previous examples we were using hard-coded values for calculations (values that didn't change). Later on, we'll make sure that our program can perform tax calculations for any income, state, and number of dependents.

Let's introduce some new object-oriented terms. One of them is called inheritance, which is an ability to create a new object, based on an existing object.

We were planning to use the class Tax for all states, but what if the state of New Jersey introduces educational tax deductions? If you have a kid in college, this makes you eligible for an additional $500 deduction from your taxes. In this case, we have to either change the method calcTax(), or create another class that will be based on our class Tax plus have this new functionality to calculate educational deductions.

In real life, every person inherits some features from his or her parents. This similar process exists in Java. The special keyword extends is used to indicate that one class has been inherited from another:

class NJTax extends Tax{

The class NJTax will have all features the class Tax has, plus you can add some new properties and methods to it. The class Tax is called a superclass, and the NJTax is called a subclass. You could also use such terms as ancestor and descendent respectively. This new class will have access to variables and methods of its superclass (unless they have a private access level, but let's worry about that later). It's time to create a method called adjustForStudents() in the class NJTax.

class NJTax extends Tax{
    double adjustForStudents (double stateTax){
       double adjustedTax = stateTax - 500;
       return adjustedTax;

How will the class TestTax create an instance of the class NJTax? Here you go:

NJTax t= new NJTax();

Now you can call methods defined in the class Tax as well as in the NJTax using the variable t, for example:

NJTax t= new NJTax(); double yourTax = t.calcTax();

double totalTax = t. adjustForStudents (yourTax);

Please note that we've added a new functionality to the tax calculation program without changing the code of the class Tax. Another important note is that even though the variable t refers to an instance of the class NJTax, we are calling the method defined in its superclass:


The above code fragment also shows how you can pass a value calculated from one method to another one. We are passing the value of the variable yourTax to the method adjustForStudents() as an argument.

Method Overriding

The next important term of object-oriented programming is method overriding. Imagine that a superclass has 20 methods. Most of them are generic for all states, but there is one method that is not valid for the state of New Jersey. Instead of modifying this method in the superclass, we could create another method in the subclass with the same name and argument list. It's time to modify our previous example. The goal is to change the calcTax() method without modifying the superclass. We'll declare the method calcTax() in the subclass - NJTax. By doing that, we're suppressing the method of the superclass with the same name and signature. Let's try a simple test - create a method calcTax() in NJTax and only write one line:
public double calcTax() {         
  return 1000; 

Compile the code and run the TestTax program - it will print the number 1000 as a result, which proves that only the calcTax() from the subclass has been called.

The advantages of using method overriding as opposed to direct code modification are:

  1. The source code of the superclass may not be available, but you still need to change its functionality.
  2. Somebody else may still need the original version, that's why you can't modify it.

More Stories By Yakov Fain


Comments (1) View Comments

Share your thoughts on this story.

Add your comment


Most Recent Comments
Yuri G. 03/04/04 10:19:44 AM EST

I was always amazed how simple Yakov could explain things that sounded obscurely at first sight. I was attending his classes in NJ and must say they were just cool. His way of delivering a lecture was elaborated and was a fun at the same time. He could quickly turn a Java novice into a decent developer equipped with the latest Java technologies. Thanks him for that.
There are tones of books out there trying to teach Java, but there''s definitely a lack of talented mentors behind them, though. I am lucky, - I know one.