Changing the channel will affect its property. The operation on the behavior will affect the properties. And the behavior can be – we can change the channel, Adjust volumes and settings, switch off, switch on, etc are lots of behavior are there. It’s With and Height, color, type (SmartTV or CRT TV), etc. And in the OOPs, it can be defined in terms of its properties and behavior.įor Example – Consider a Television, It is an object. OOP – Object-Oriented Programming Principle is the strategy or style of developing applications based on objects.Īnything in the world can be defined as an object. Principles of Object-Oriented Programming.Components of Object-Oriented Programming.How Software is Developed in Object-Oriented Programming?.Why OOPs are Popular in Comparison to Other Types of Programming Principles?.Our Class says, "If we wanted to create a tree, this is how it would be represented in our program. Remember it's a blueprint, because we're only defining the attributes and behaviors of a tree. Now we've combined the attributes and behaviors related to a tree into a single unit, the Tree Class. There are many different ways a tree can be represented in code. However, you could use the type attribute, or other data to inform how much the tree grows. To keep things simple, we'll say all trees will grow 10 feet in height, and one inch in diameter when this behavior is used. In the implementation, we'll access the height, and trunk diameter of the tree, and increment them accordingly. Our function will return void, and be called grow. Let's add the grow behavior to finish up our definition of a tree. To use this enum, all we have to do is use the data type TreeType for a given attribute or variable, which is what we've done here. The only TreeTypes that can exist are those listed in the enum Class. By using an enum, no one can create a macaroni tree, or a money tree, which would be the case, if we used a string. These are the only values we can give to a variable with the tree data type. Here, we'll want those constants to be different TreeTypes. An enum is a special type that represents a group of constants. To create an enum, we'll ctrl click on the source folder, select New Java Class, and then Enum. While we could represent the type of Tree with a string, we'll represent it with its own enum here called TreeType. These will have the data type double, meaning they can be represented by decimal values. Starting with the attributes, we'll have a height and trunk diameter. Let's add some code to the Tree Class so that it actually represents a tree. However, most of the time, a given class will have both. It could also just have attributes, or just have behaviors. A Class doesn't have to have attributes or behaviors, it could be completely empty as we see here. And our IDE sets up this Class as an empty Class named Tree. I'm on a Mac, so I'll go ctrl click, and this will allow me to create a new Java Class. Let's create a blueprint for a tree using a Class in Java. It's an action that the tree takes, rather than an attribute representing the current state of the tree. As for the behaviors, growing could be a behavior for the tree. It contains the definition of what a Class should be. That's what makes this Class a blueprint. Every tree has these properties or attributes, but their values might not be the same. The attributes might be height, trunk diameter, and maybe even tree type. Let's design a tree object in code using a class. A blueprint, or class, contains a set of attributes and behaviors that define an object. Classes in Java give us a way to model or represent physical objects in code via a blueprint. Almost anything in real life can be represented in code, but how well something is represented is up to us in what we decide to program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |