Classes and Objects
The course is part of this learning path
This course looks at Object-Oriented programming in Java and shows how classes are designed and constructed, and how objects are created from them. Then, we'll complete three projects: creating a bank account class, an ice cream class, and a circle class, as well as tests to make sure that they work in order to reinforce what you've learned.
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous lecture, we created a Rectangle class. We learned about constructing a class from scratch with the help of a UML class diagram. In the process, we learned about instance and static fields and methods. So, what you see is a UML class diagram of the Book class. For the Book class, we want to keep track of the author, title, genre, and number of pages. However, our Book class is going to be immutable. This means that unlike our Rectangle class, once we have set the fields, namely the author, title, genre, and number of pages, we don't allow the client code, which is the code using our class, to modify it. In other words, when a class is immutable, this means it lacks mutators.
So, there will be no setters in our Book class, just getters also known as accessors. Notice that we have a single constructor, and that it is parameterized. So, this means that when an object of type Book is created, the client code must provide author, title, genre, and number of pages as arguments. Let's put our skills to work and create two files, Book.java and BookDemo.java. Again, there's nothing magical about the word "demo" being in the name of the class we used to house our main method. It's just so we can keep track easily of which class has main in it and which class is supposed to represent an entity, in our case, a Book. All right. Let's go over here 'src', 'New', 'Java Class', 'Book'. Right click 'New', 'Java Class', 'BookDemo'. Perfect.
And in BookDemo, we'll just get it started here, public static void main. And we'll work on our Book class. So, inside of the Book, we have the author, the title, the genre, which are all strings, and numPages which is an integer. No static fields this time. Now for the constructor, this.numPages = numPages. Nice. And now, just the getters because we don't use setters. Once they're set with the constructor, we don't modify them. And of course, getnumPages. All right. Awesome. You bring that up. This is the end of the Book class and we'll go over to the Demo, set that up. We've got our main method already. So, let's create three Books.
So, gameOfThrones. It's a new Book ( "George Martin", "Game of Thrones"). That's the title. You'll notice a little tool set that pops up to help us. Now, we have the genre, "Fantasy" we'll say, and 864 pages. Now we have mathBook = new Book, say it's by "James Stewart" who is an author of a very, very famous calculus book. Unfortunately, I think he passed away not too long ago, but he is one of the most famous calculus book authors as far as newer students are concerned. Book("Joel Murach", "Murach's Java Programming") and we want to put that in "Programming" I guess, that's the genre, and 800 pages. Okay. All of these are great books in their respective categories. And let's do a method called printBookDetails.
So, outside of main, I'm going to make a static method, public static void printBookDetails. So, lower that a little bit. All right. We have the end of the BookDemo class, println, we'll say book.getTitle, not bool, book, book.getTitle. And then we'll say System.out.println("by" +book.getAuthor()) say has, space, and then we'll say book.getNumPages. Let me see here what do I want to do, and its genre is, I think I'll put that on the next line right there. There we go. And we'll say book.getGenre, and then just a little extra space at there. Perfect. And that will go back up to main and will actually call the method, printBookDetails. We're going to call it on the gameOfThrones book and then we'll call it on mathBook and we'll call it on the JavaBook. Great. So now, let's run it and see what we get.
Make sure we didn't make any big mistakes. So, 'BookDemo' right click, run. There we go. Game of Thrones by George Martin has, it looks like there's a little bit of a space issue here. You'll notice that happens in each of these. So, we'll just fix that. It's when it prints it. So, I put a little space there before the word pages and we'll run it again. This type of thing happens very frequently. There we go. So now, we've refined it and made it better. So, awesome. Our Book class allows us to create Book objects and to obtain the information from them using the getter methods. But again, since the class does not have any mutator methods, the setters, the class is immutable.
Before we go on to the next lecture though, I have a challenge for you. Continuing with the same files, I want you to refactor our code here so that printBookDetails method exists inside of our Book class, and that it prints two standard output which is again to the Console, all the information that our current static printBookDetails prints inside the BookDemo class. Make sure it's an instance method, not a static method since it will be called on specific objects now. Also, this means it won't need a parameter to tell it what object to use to print out the data since it's called on this particular object.
Change the calls in main as well to reflect that printBookDetails is now a method of the Book class, and you can comment out the static version of the Book class in the BookDemo file. As a hint, since the method will be inside the Book class, you can directly access the private data and don't need to call the methods. However, if you wanted to call the methods, you would just use their names with no object name or dot in front of them. So, pause the video, give this one your best shot. Come back when you're done or if you need some help. How did that go for you? Were you able to finish this challenge? Hopefully it wasn't too difficult.
But regardless, let's work on it together. So, inside the Book class, I'm going to add our new wonderful public void printBookDetails. And you could actually copy over some of the information from the other file over here. I could actually copy this stuff and we can manipulate it, fix it, end printBookDetails. We don't need to do getTitle, because we access the private data, we could just say getTitle, and here we can say by, instead of, we could do getAuthor that would be okay. We wouldn't need an object in front of it because we're in the object. But we're just going to type author because we have access to the private data. We have access to numPages as well. And we also have access to the genre, and we have that new line at the bottom as well. It isn't static,
so make sure you don't make it static. And then once you've got this, we'll head over to the main method. Over here, then file with main in it, and we can comment this out because we don't really need that one. But we do need gameOfThrones.printBookDetails. Okay. And we can do mathBook.printBookDetails. And of course, javaBook.printBookDetails. And we'll delete that extra call here. You don't need that. So now, it's a member of the class. Instead of passing it as a parameter in this class and having this do the work, we're having the class itself do the work. And it's okay because we know we're going to print to the Console and we're having this do the work for us.
So, it probably makes sense that it can print its own data. All right. So, let's run this and test it out to make sure we get the same output, and we do. Same exact output as before. Now, it's just an internal member instead of the static method. So, that looks good to me. Great job everyone. In the next lecture, you'll work on your first project for this section, creating and testing a class that might be useful for banking software. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.