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

Object Oriented Programming (or OOP) is actually classified according to three fundamental principles.

1) Encapsulation
2) inheritance
3) polymorphism

These seem like scary terms, but they are actually pretty easy principles to understand. To discover how to program with Java, you’ll need to understand these principles. So, let’s consider our first main object-oriented programming concept, encapsulation. Encapsulation just means that we want to limit the access that some other pieces of code have to this particular object. So, to illustrate, if you have a Person object, and this Person object has a first and last name as attributes. In the event that another piece of code tries to modify your Person object’s given name to say “Frank3″, you can make a note of what name is being set and remove any digits so we’re just left with ” Frank”. Without encapsulation, we won’t have the ability to prevent “dumb programmers” from changing the values ​​of our variables to something that wouldn’t seem sensible or, worse yet, breaking the application. Does it seem sensitive?

The second concept of object-oriented programming, and an essential principle if you want to learn to program with Java, is inheritance. This specific concept refers to a superclass (or parent class) and a subclass (or child class) and the simple fact that a child class acquires each of its parent’s attributes. You can think of it in terms of a real-world circumstance, like a real father and son. A child will likely inherit certain traits from their parents, such as eye or hair color. Let us imagine yet another example in terms of programming, let’s say we have the superclass “Vehicle” and the subclasses “Car” and “Motorcycle”. A “Vehicle” has wheels, so through inheritance so would a “Car” and a “Motorcycle”, however a “Car” has doors and a “Motorcycle” does not. Therefore, it would not be correct to state that a “Vehicle” has doors, as that statement would be inaccurate. So you can see how we can determine all the aspects that are similar regarding a “Car” and a “Motorcycle” and thus identify them within the “Vehicle” super class.

The third concept of object-oriented programming is polymorphism. This specific concept seems to be one of the scariest, but I can explain it in simple terms. Polymorphism means that an object (ie Animal) can take multiple forms while your program is running. Let’s imagine that you have designed an Animal class and defined the “Speak” method. He then asked three of his friends to develop animal types and implement the “Speak” method. You won’t know what kind of animals your friends create, or how their animals will talk, unless you actually hear those animals talk. This is very comparable to how Java approaches this problem. It’s called dynamic method binding, which simply means that Java won’t understand how the actual animal speaks until runtime. So maybe your friends have created a Dog, a Cat and a Snake. There are three varieties of animals here, and each speaks differently. Every time Java asks the Dog to speak, he says “woof”. Every time Java asks the Cat to speak, he says “meow”. Every time Java asks the snake to speak, it hisses. That’s the beauty of polymorphism, all we did was define an Animal interface with a Speak method, and we can make a bunch of animal types that speak in their own specialized way.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *