Object Oriented Programming (or OOP) is often categorized by three essential concepts.
1) Encapsulation
2) Inheritance
3) Polymorphism
Any of these seem like daunting words but are in actual fact fairly simple concepts to comprehend. If you want to figure out how to program with Java, you\’ll need to fully grasp these concepts. So to begin with let\’s examine the 1st key concept of OOP, encapsulation. Encapsulation just means that we want to restrict the access that other portions of code have to this specific object. So, for instance, if you\’ve got a Person object, and this Person object has a first and last name as attributes. In the event some other chunk of program code tries to change your Person object\’s first name to be say \”Frank3\”, you would have a look at what the first name is trying to be set to, and strip out all digits so that we are simply left with \”Frank\”. Devoid of encapsulation, we would not be able to prevent \”silly developers\” from modifying the values of our variables to something that wouldn\’t seem sensible, or perhaps a whole lot worse, break the application. Seem sensible?
The next principle of OOP, as well as an essential concept if you\’d like to learn how to program with Java, is Inheritance. This specific principle refers to a super class (or parent class) and a sub-class (or child class) and the idea that a child class acquires all of the features of its parent. You can imagine it in regards to a real world circumstance, just like a real parent and child. A child is likely to inherit particular characteristics from their parents, for example, eye color or hair color. Let\’s think about another example in relation to computer programming, let\’s say we have a super class \”Vehicle\” and sub-classes \”Car\” and \”Motorcycle\”. A \”Vehicle\” possesses tires, and so through inheritance so would a \”Car\” and a \”Motorcycle\”, but a \”Car\” has doors, and a \”Motorcycle\” does not. Therefore it would not be appropriate to say that a \”Vehicle\” has doors, because that statement would be wrong. So you can see how we could actually indicate all the aspects that are common regarding a \”Car\” and a \”Motorcycle\” and thus define them inside of the \”Vehicle\” super class.
The 3rd principle of OOP is Polymorphism. This specific principle may seem like the most daunting, but Allow me to demonstrate it in simple terms. Polymorphism suggests that an object (i.e. Animal) can take on numerous forms while your program is operating. Let\’s imagine you have designed an Animal class and outlined the method \”Speak\”. You then asked 3 of your buddies to build varieties of animals and have them implement the \”Speak\” method. You won\’t really know what kind of animals your buddies build, nor how their Animals will speak, unless you actually hear those animals speak. This is very similar to how Java deals with this issue. It\’s referred to as dynamic method binding, which means, Java will not understand how the specific Animal speaks until runtime. So perhaps your buddies have created a Dog, Cat and Snake. These are 3 types of Animals, and each speaks in different ways. Whenever Java requests the Dog to speak, it says \”woof\”. Anytime Java requests the Cat to speak, it says \”meow\”. Any time Java requests the snake to speak, it hisses. So there\’s the best thing about polymorphism, all we did was describe an Animal interface that has a Speak method, and we can make a whole bunch of varieties of animals which speak in their very own unique way.
If you need to learn more about Java, visit How to program with Java, where programmer Trevor Page takes you step by step how to become a Java coder. Trevor offers Java tutorials full of helpful tips for beginners.. Unique version for reprint here: The 3 Key Concepts of Object Oriented Programming – How to Program with Java.
The 3 Key Concepts of Object Oriented Programming - How to Program with Java
No comments:
Post a Comment