Welcome!

Java IoT Authors: Liz McMillan, Pat Romanski, Automic Blog, Elizabeth White, Roger Strukhoff

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
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
"Digital transformation - what we knew about it in the past has been redefined. Automation is going to play such a huge role in that because the culture, the technology, and the business operations are being shifted now," stated Brian Boeggeman, VP of Alliances & Partnerships at Ayehu, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
SYS-CON Events announced today that Evatronix 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. Evatronix SA offers comprehensive solutions in the design and implementation of electronic systems, in CAD / CAM deployment, and also is a designer and manufacturer of advanced 3D scanners for professional applications.
SYS-CON Events announced today that Synametrics Technologies will exhibit at SYS-CON's 22nd International Cloud Expo®, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Synametrics Technologies is a privately held company based in Plainsboro, New Jersey that has been providing solutions for the developer community since 1997. Based on the success of its initial product offerings such as WinSQL, Xeams, SynaMan and Syncrify, Synametrics continues to create and hone inn...
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – 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 results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis 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. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.