Welcome!

Java IoT Authors: Elizabeth White, Pat Romanski, Liz McMillan, Stackify Blog, Progress Blog

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) View Comments

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.


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.

@ThingsExpo Stories
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that B2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. B2Cloud specializes in IoT devices for preventive and predictive maintenance in any kind of equipment retrieving data like Energy consumption, working time, temperature, humidity, pressure, etc.
DevOps at Cloud Expo – being held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real r...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Suzuki Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Suzuki Inc. is a semiconductor-related business, including sales of consuming parts, parts repair, and maintenance for semiconductor manufacturing machines, etc. It is also a health care business providing experimental research for...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
Elon Musk is among the notable industry figures who worries about the power of AI to destroy rather than help society. Mark Zuckerberg, on the other hand, embraces all that is going on. AI is most powerful when deployed across the vast networks being built for Internets of Things in the manufacturing, transportation and logistics, retail, healthcare, government and other sectors. Is AI transforming IoT for the good or the bad? Do we need to worry about its potential destructive power? Or will we...
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.