Classes are the main building blocks of Java code. In this video, see how they can be used to wrap data and behavior into a unit that can then be used as a template. A coding demonstration shows you how to use that template to create multiple instances of a class. Then you shall go forth to create objects of your own.
- [Female Narrator] In this lesson, I'm going to show you how to create an object. The object I'm going to create is going to be a duck. So, objects have two kinds of things in them. The first kind are member variables, and those are the data that's associated with the class. The other kind of thing are methods, and those describe the behavior, or the functionality of the object. So, for my duck, I'm going to make some member variables. I'll start with giving a duck a name. I'm going to also give it a life expectancy. And favorite food. For methods, I'm going to have my ducks know how to waddle and quack. An object in Java is kind of like a little capsule, so it holds both the data that's associated with an object and its functionality. And sometimes we use this word, this big, nice word, encapsulation. That means that we might not want the data associated with the duck to be publicly accessible to the outside world. So, to make sure that happens, we put the private accessor in front of them, like this. These ducks might not want other ducks to know personal details about them, like their favorite food. And now that I've made the duck object, I can use it as a template to make lots of different ducks with different data in them. They'll have different names, different favorite foods, and different life expectancies. And this is the basics of creating an object in Java.
This course was created by Madecraft. We are pleased to host this content in our library.
- What is an object in Java?
- Building Java classes
- Retrieving and updating variables with getters and setters
- Using the static keyword
- Understanding inheritance
- Diagramming code with UML
- Implementing polymorphism