Home » Word Press » The 3 Main Principles of Object Oriented Programming – How to Program With Java

The 3 Main Principles of Object Oriented Programming – How to Program With Java

Object Oriented Programming (or OOP) is in truth categorized by way of 3 major ideas.

1) Encapsulation

2) Inheritance

3) Polymorphism

These seem to be scary phrases however are in truth quite simple ideas to seize. In order to work out how to program with java, you can want to perceive those ideas. So let’s believe our first major idea of OOP, encapsulation. Encapsulation simply manner we wish to restrict the get entry to that any other items of code have to this actual object. So, to illustrate, when you have a Person object, and this Person object has a primary and closing title as attributes. In the development some other bite of code makes an attempt to adjust your Person object’s first title to be say “Frank3”, you must consider of what the primary title is making an attempt to be set to, and take away any digits in order that we’re merely left with “Frank”. Without encapsulation, we will be able to now not have the opportunity to save you “silly programmers” from enhancing the values of our variables to one thing which would not appear good, or worse, wreck the applying. Seem good?

The 2nd idea of OOP, and a very important concept if you want to learn the way to program with Java, is Inheritance. This particular idea refers to a great magnificence (or guardian magnificence) and a sub-magnificence (or kid magnificence) and the easy truth kid magnificence acquires each and every of the attributes of its guardian. You can assume of it in phrases of an actual international circumstance, like an actual guardian and kid. A kid will most definitely inherit positive characteristics from his or her oldsters, like say, eye color or hair color. Allow us to consider but some other instance in phrases of programming, say we now have tremendous magnificence “Vehicle” and sub-categories “Car” and “Motorcycle”. A “Vehicle” possesses tires, due to this fact via inheritance so would a “Car” and a “Motorcycle”, on the other hand a “Car” has doorways, and a “Motorcycle” does now not. So it would not be correct to state “Vehicle” has doorways, as that declaration could be faulty. So you’ll see how lets decide the entire sides which are equivalent relating to a “Car” and a “Motorcycle” and thus establish them within of the “Vehicle” tremendous magnificence.

The third idea of OOP is Polymorphism. This particular idea seems to be one of probably the most scary, however I am in a position to give an explanation for it in easy phrases. Polymorphism signifies that an object (i.e. Animal) can tackle a number of bureaucracy whilst your program is working. Let’s consider you’ve gotten designed an Animal magnificence and outlined the process “Speak”. You then requested 3 of your friends to broaden types of animals and feature them enforce the “Speak” approach. You would possibly not know what kind of animals your mates create, or how their Animals will discuss, until you in truth listen the ones animals discuss. This could be very similar to how Java addresses this factor. It’s referred to as dynamic approach binding, which merely manner, Java would possibly not know the way the real Animal speaks till runtime. So possibly your mates have created a Dog, Cat and Snake. Here are 3 types of Animals, and so they each and every one speaks distinctly. Whenever Java asks the Dog to discuss, it says “woof”. Anytime Java asks the Cat to discuss, it says “meow”. Whenever Java requests the snake to discuss, it hisses. There’s the sweetness of polymorphism, all we did used to be to outline an Animal interface with a Speak approach, and we will make a host of types of animals which discuss in their very own specialised method.

Check Also

Santa Claus Game   Unity Android Game Template (Games)

Santa Claus Game – Unity Android Game Template (Games)

Santa Claus Game – Unity Android Game Template (Games). Build your individual Santa Claus Game. …