Welcome!

Eclipse Authors: Pat Romanski, Elizabeth White, Liz McMillan, David H Deans, JP Morgenthal

Related Topics: Java IoT

Java IoT: 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){
            Tax   t = new Tax(); // creating an instance
            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.05;
  }else{
    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:

t.calcTax();

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

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (1)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...