≡ Menu

Object Oriented Programming – Java Tutorial


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)
      this.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?

Want Free Access to my Best Java Courses?

Alright, so if you’re read this far down the article, then you’re clearly interested in learning how to code. You can currently get access to my 2 best courses on Java for free for 30 days (then it’s just $10/month afterwards, or $97/year).

When you sign up you’ll receive login information to start learning about Java. If you decide you don’t like the courses, you can cancel at any time. If you decide you love the courses then you don’t have to do anything and you can stay subscribed for as long as you like for just $10/month or $97/year.

It’s like Netflix for coders!

Go ahead an click the button below to get started!

Hope to see you on the “inside” of our community of coders 🙂

If you’re not quite ready to jump on board, then perhaps I could interest you in our next Java tutorial?


Comments on this entry are closed.

  • Hein Set July 24, 2012, 1:44 pm

    i would to thank you and i want to be a professional programmer …..

  • Erez August 8, 2012, 11:57 pm

    Thank you!
    This is my first time trying to approach any kind of programming language,
    You make it look fun and interesting… (i wouldn’t say easy..)

    • Trevor Page August 9, 2012, 6:36 am

      Hi Erez,

      You’re right that programming isn’t easy, but I’m glad that you find it interesting. If you ever get stuck on a concept or piece of code, reach out through the comments or email me: info@howtoprogramwithjava.com and I’ll see what I can do to help you out!

  • MM August 9, 2012, 12:43 pm

    Hello there.
    I have a question pertaining to the public/private class identification aspect.
    Why would you want to leave the book’s title in a public class?
    Would that not allow any other class to change it at will?
    The book itself isn’t dynamic (lawl) the title shouldn’t change once its been set right?
    I might be misunderstanding something (i have close to zero experience with programming)
    Could you elaborate please?

    • Trevor Page August 9, 2012, 12:52 pm

      Excellent question here,

      As you’ll notice, the actual bookTitle variable is declared private, but there is a “getter” and “setter” method for the book that are public. So you are indeed correct that once a book’s title is set, it shouldn’t change. This is something I didn’t elaborate on in the code and it could be done is a few different ways in Java, but if we were to implement this, it would be in the “setter” method. For example:

      public void setTitle (String title)
        if (this.title == null)
          this.title = title
          // don't do anything as we don't want the book's title to change.

      That would be one way to accomplish this, other ways could involve setting a constant using the final keyword. But I’ll leave that for another topic to cover.

      All in all, great question Michael!

  • Nam September 3, 2012, 9:07 pm

    Thank you for this attracted website! I’m Vietnamese. My English is quite bad, but I try my best to learn Java in English. It’s lucky your instruction is very clean. Thank you!!

  • Deepak Lakum October 1, 2012, 2:27 am

    Hi, first of all i like to say you thanks,
    i am new for prog. but your teaching style is cool, i learned so many things here with your web, but till this is going too fast i think, some key words like “return”, “this”,”get” is not hard to understand but if you describe it when you use it first time it will more helpful for people like me. i am till guessing what result will get after using this key words in programming.

    • Trevor Page October 1, 2012, 7:15 am

      excellent feedback, thank you for your comment, I’ll write some tutorials on these concepts. please don’t hesitate to let me know what other aspects of programming I need to cover.

  • simmi October 13, 2012, 4:47 am

    i could brush up all concepts clearly n quickly………thanx a tonn……….:):)

  • chirag anand November 6, 2012, 6:30 am

    best tutorials amongst all that I’ve read so far….keep up the good work.

    • Trevor Page November 6, 2012, 10:06 am

      Many thanks, I love to hear that!

  • Bill Fox January 18, 2013, 4:18 am

    Hello – new to Java –
    I have a question in regards to the book class.
    Where is the main method for the book class?
    Where does the book class find main method?

    • Trevor Page January 18, 2013, 6:19 am

      Great question Bill,

      There should only ever be one main method per application that you create. This means that for the vast majority of the Classes you create, you won’t be adding in the main method. The Book class is a very basic Class because all it does is allow its own private properties to be read and change (via getter and setter methods). These kinds of “getter and setter” Objects (also known as Entity Objects) would likely never have a main Class, simply because their functionality is to just represent a real world “thing”, no more and no less.

      To answer your question of how the Book Class finds the main method: it doesn’t need to. When you run your program, Java just needs to locate the main method once at startup (kind of like how you would manually place a record player’s needle on the outside of a record) and then the program will just naturally flow from that starting point through its motions (based on the logic of code flow).

      Does that help answer your questions Bill?

      • Bill Fox March 22, 2013, 3:26 am

        Hello: Trevor
        I did not give up, but continued on with the other assignments in which I understood. I am circling back with this one – I guess it’s bugging me. Kind of answered my question. I guess I am trying to see how a class file talks to main method.
        Anyways if I try and run the book.class file it complains about not having a main type and if I add a main type the IDE starts getting really angry at and and gives me an Exception error in thread “main” – the book class is under helloWorld project.
        Thanks for all your help, listen to the podcast when driving to San Diego. My ultimate goal with Java is to write Map Reduce jobs for Hadoop.
        Thanks – All the best

        • Trevor Page March 23, 2013, 10:54 am

          What’s the exception that you’re getting? You can just copy/paste the whole error in the comments.

  • Durim February 28, 2013, 9:47 am

    Hi Trevor!

    Please, if you can, explain to me with an example(as you know;)), this definition of encapsulation:
    “A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.”

    Thank you!

Next post:

Previous post: