3 Principles of Object Oriented Programming – How to Program with Java

Object Oriented Programming (or OOP) is actually classified by three main principles.

1) Packaging
2) Inheritance
3) Polymorphism

These seem to be frightening terms, but they are really fairly easy principles to understand. To learn how to code using Java, you'll need to understand these principles. So let's consider the first major concept of OOP packaging. Encapsulation only means that we want to restrict access to some other piece of code for this specific object. So, for clarity, if you have a person object, and the person object has a first and last name as attributes. If another piece of code tries to modify the first name of the person object to "Frank3", you can notice what the first name tries to assign and remove any numbers so we can simply leave it with "Frank". Without encapsulation, we would not have the ability to prevent "silly programmers" from adjusting the values ​​of our variables to something that does not seem reasonable or worse than breaking the application. Sound reasonable?

The second concept for OOP, the basic principle if you want to know how to code using Java, is inheritance. This specific concept refers to a superclass (or major category) and a subclass (or subclass) and the simple fact that a subclass gets every attribute of its parent. You can think of it in terms of the real world, like a real parent and a child. A child may inherit certain features of his or her parents, such as eye color or hair color. Let us imagine another example in terms of programming, supposing we have a category "vehicle" and subcategory "car" and "motorcycle". The "car" owns the tires, and thus by inheritance, the "car" and "motorbike", but the "car" has doors, and "motorbike" does not. So it wouldn't be accurate to state that a "car" has doors, because this ad will be inaccurate. So you can see how we can identify all the similarities in terms of "car" and "motorcycle" and thus get to know them within the category "super".

The third concept for OOP is polymorphism. This specific concept seems to be one of the most frightening thoughts, but I am able to explain it in simple terms. Polymorphism means that the object (i.e. the animal) can take multiple shapes while running the program. Let's imagine that you designed an animal class and defined the "talking" method. Then I asked three of your companions to develop different types of animals and get them to "talk". You will not know the kind of animals your friends create, or how their animals will speak, unless you actually hear them talking. This is very similar to how Java handles this issue. It is called the dynamic link method, which simply means, Java will not understand how the actual animal speaks until runtime. So your friends might have created a dog, cat and snake. Here are three types of animals, and each speaks clearly. When Java asks the dog to speak, it says "meat." Anytime Java requests that Cat speak, she says "meow". Whenever Java asked the snake to speak, it whispered. There is the beauty of polymorphism, all we did was define the façade of the animal using the speaking method, and we could create a group of animal species that speak their specialized way.

&

    Leave Your Comment

    Your email address will not be published.*