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.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

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.

Latest Stories
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
Data center, on-premise, public-cloud, private-cloud, multi-cloud, hybrid-cloud, IoT, AI, edge, SaaS, PaaS... it's an availability, security, performance and integration nightmare even for the best of the best IT experts. Organizations realize the tremendous benefits of everything the digital transformation has to offer. Cloud adoption rates are increasing significantly, and IT budgets are morphing to follow suit. But distributing applications and infrastructure around increases risk, introdu...
Today's workforce is trading their cubicles and corporate desktops in favor of an any-location, any-device work style. And as digital natives make up more and more of the modern workforce, the appetite for user-friendly, cloud-based services grows. The center of work is shifting to the user and to the cloud. But managing a proliferation of SaaS, web, and mobile apps running on any number of clouds and devices is unwieldy and increases security risks. PJ Hough, Citrix Executive Vice President and...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. 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 over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
Artifex Software began 25-years ago with Ghostscript, a page description language (PDL) interpreter software prevalent in printing and related applications requiring rendering and/or conversion from one software language to another. Founded by renowned computer scientist Dr. L. Peter Deutsch, our company has thrived on the basis of our sharp focus on this area of expertise, a zealous commitment to quality and a strong customer service orientation. Over 100 OEM partners representing some of th...
SUSE is a German-based, multinational, open-source software company that develops and sells Linux products to business customers. Founded in 1992, it was the first company to market Linux for the enterprise. Founded in 1992, SUSE is the world's first provider of an Enterprise Linux distribution.
Cloud-Native thinking and Serverless Computing are now the norm in financial services, manufacturing, telco, healthcare, transportation, energy, media, entertainment, retail and other consumer industries, as well as the public sector. 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 for long development cycles that pro...
ShieldX's CEO and Founder, Ratinder Ahuja, believes that traditional security solutions are not designed to be effective in the cloud. The role of Data Loss Prevention must evolve in order to combat the challenges of changing infrastructure associated with modernized cloud environments. Ratinder will call out the notion that security processes and controls must be equally dynamic and able to adapt for the cloud. Utilizing four key factors of automation, enterprises can remediate issues and impro...
Is your enterprise growing the right skills to fight the digital transformation (DX) battles? With 69% of enterprises describing the DX skill drought as being soft skills, rather than technology skills, are you ready to survive against disrupters? The next wave of business disruption is already crashing on your enterprise as AI, Blockchain and IoT change the nature and location of business. Now is the time to prepare. Drawing on experiences with large and midsize enterprises, Marco Coulter t...
Alan Hase is Vice President of Engineering and Chief Development Officer at Big Switch. Alan has more than 20 years of experience in the networking industry and leading global engineering teams which have delivered industry leading innovation in high end routing, security, fabric and wireless technologies. Alan joined Big Switch from Extreme Networks where he was responsible for product strategy for its secure campus switching, intelligent mobility and campus orchestration products. Prior to Ext...
FinTech is a disruptive innovation that denotes the adoption of technologies that have changed how traditional financial services work. While FinTech is now embedded deeply into the financial services ecosystem, the rise of digital age has paved way to FinTech 2.0 - which is rolling out innovative solutions through emerging technologies at a disruptive pace while maintaining the tenets of security and compliances. Blockchain as a technology has started seeing pilot adoption in FinTech around ...
In an age of borderless networks, security for the cloud and security for the corporate network can no longer be separated. Security teams are now presented with the challenge of monitoring and controlling access to these cloud environments, as they represent yet another frontier for cyber-attacks. Complete visibility has never been more important-or more difficult. Powered by AI, Darktrace's Enterprise Immune System technology is the only solution to offer real-time visibility and insight into ...
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation. DX encompasses the continuing technology revolution, and is addressing society's most important issues throughout the entire $78 trillion 21st-century global economy. DXWorldEXPO® has organized these issues along 10 tracks, 22 keynotes and general sessions, and a faculty of 222 of the world's top speakers.