Java Tutorial – What is Object Oriented Programming in Java?

Java is known as an Object Oriented language. So, what does Object Oriented mean? It means that the foundations of any kind of program constructed in Java might be imagined in terms of Objects. A good example of this idea should be to have a look at a handful of sample business requirements for a product. Imagine that we are tasked with constructing a software program intended to keep track of an actual public library system. This system must keep track of each of the branches associated with the libraries, the whole set of materials that can be contained in the branches, and additionally all of the people that may need to access books from the library's branch.

The first thing we're able to do is examine those specs and pinpoint all of the keywords which happen to be nouns. For the record, a noun is actually a person, place or thing. Which means, if we analyze our requirements we discern these particular nouns:

1) Library
2) Book
3) Branch
4) Customer

All these words represent Objects in Java. This really is, in essence, Object Oriented programming (generally known as O-O programming). What we can now go about doing, is in fact arrange these four Objects on to some sort of piece of paper, and begin to distinguish what sort of attributes each one of these Objects contains. What do I mean by the attributes? Well, in O-O development this is known as identifying the “has a” relationships. To provide an example, a Branch “has an” address, a Book “has a” title, a Customer “has a” name. We will map out the most important important attributes that each one of these Objects contain, and build ourselves a terrific starting point for the design of our Java application.

Object Oriented development allows developers to think in terms of real life “things” or Objects, and simply solve issues with all those Objects. It's important to remember that Java is actually not the only O-O programming language in existence, as it was initially started nearly five decades ago and plenty of modern programming languages utilize Object Oriented principles. Some of these languages include C++, C#, Objective-C, Python, Ruby, and Visual Basic.

So what would the Objects from our example look like in code? Let's take a look at the Book Object:

public class Book
    private String author;
    private String title;
    private Integer isbn;
    private Integer numberOfPages;

    public String getAuthor()
      return author;
    public void setAuthor(String author)
    { = author;
    public String getTitle()
      return title;
    public void setTitle(String title)
      this.title = title;
    public Integer getIsbn()
      return isbn;
    public void setIsbn(Integer isbn)
      this.isbn = isbn;
    public Integer getNumberOfPages()
      return numberOfPages;
    public void setNumberOfPages(Integer numberOfPages)
      this.numberOfPages = numberOfPages;

Okay, so what can we identify in this code that we are already familiar with? Well, we certainly see the word public a lot sprinkled around here. To refresh your memory, the word public is a modifier that allows for any Java Class to have access to the code within the scope of what it's modifying. So, you see that the first public keyword is placed on the Class name, which in this case is Book. This means that our Book Class will be accessible by all other Classes in our project.

Note: You may have noticed I used the terms Class and Object to describe the same thing. In actual fact, they are very similar, the only difference is that the Class can be considered the blueprint for an Object. An Object is what is physically created when the Java program is running. So if I want to “create” a Book, let's say a “Harry Potter” Book, I would “instantiate” (or “create) a Book Object based on it's Class blueprint. Make sense?

Let's move on to the private modifier that you see on the first four variables (or attributes) of our Book Class. This is kind of strange you might think. If a Book has attributes like a title, an author etc. then why are these marked as private? Wouldn't we want other Classes to have access to a Books title for instance? Yes and no, this approach used here is called encapsulation, and it's one of the fundamental principles in Object Oriented programming. Let's flip over to Wiki for a definition of encapsulation:

In a programming language, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

In our example, we are touching on the first part of this definition, which is restricting access. We don't want just anyone to be able to come in and say, change the title of our Book Object. We want to be able to “screen” or “moderate” what changes are allowed and which are not. So we accomplish this by setting the attributes of our Book to be private and introduce public methods where you'll be able to retrieve and/or change the value of our Book‘s attributes. So, if you want to know what a Book‘s particular title is, you would have to “ask” like this:

String aBooksTitle = book.getTitle();

And just the same, if you wanted to change a particular Book‘s title, you would do this:

book.setTitle("Harry Potter Vol. 2");

If the reasoning behind why we are doing this doesn't seem to make sense at the moment, don't worry. This approach is used over and over in Object Oriented programming languages, so you will become familiar with it and it will make sense in time. The main take-away for this Java tutorial is to know that Object Oriented programming just means that we can create programs in terms of real life Objects, and those Objects have ways of interacting with each other! Piece of cake right? If you need any clarification on anything in this post, or just want to share feedback, please leave a comment below and I'll get right back to you :) Have a great day!


Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.