encapsulation is the hiding of data implementation by restricting access to accessors and mutators.
An accessor is a method that is Object Oriented Programming used to ask an object about itself. In OOP, these are usually in the form of properties, which have, under normal conditions, a get method, which is an accessor method.
Mutators are public methods that are used to modify the state of an object, while hiding the implementation of exactly how the data gets modified.
All these seem like intimidating words but are really fairly simple concepts to comprehend. If you wish to learn how to program with java, you will need to know these concepts. So let’s have a look at our first main principle of OOP, encapsulation. Encapsulation just means that we wish to limit the access that other pieces of code have to our particular object. So, for example, 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 change your Person object’s first name to be say “Frank3”, you could have a look at what the first name is trying to be set to, and strip out any digits so we’re just left with “Frank”. Without having encapsulation, we wouldn’t be able to stop “silly programmers” from changing the values of our variables to something that wouldn’t make sense, or even worse, break the application.
The second principle of OOP, and a very important concept if you wish to learn how to program with Java, is Inheritance.
This principle refers to a super class (or parent class) and a sub-class (or child class) and the idea that a child class gains all the attributes of its parent. You can think of it in terms of a real world situation, just like a real parent and child.
A child has a tendency to inherit certain characteristics from his or her parents, like say, eye colour or hair colour. Let’s think about another example in terms of programming, say we have super class “Vehicle” and sub-classes “Car” and “Motorcycle”. A “Vehicle” has wheels, and therefore through inheritence so would a “Car” and a “Motorcycle”, but a “Car” has doors, and a “Motorcycle” does not. So it wouldn’t be accurate to say that a “Vehicle” has doors, as that statement would be incorrect. So you can see how we could determine all the aspects that are common between a “Car” and a “Motorcycle” and define them inside of the “Vehicle” super class.
The 3rd principle of OOP is Polymorphism. This principle sounds the most intimidating, but I can explain it in simple terms. Polymorphism means that an object (i.e. Animal) can take on several forms while your program is running.
Let’s imagine you have created an Animal class and defined the method “Speak”. You then asked three of your friends to make types of animals and have them implement the “Speak” method. You won’t know what type of animals your friends create, nor how their Animals will speak, until you actually hear those animals speak.
This is very much like how Java handles this problem. It’s known as dynamic method binding, which simply means, Java won’t know how the specific Animal speaks until runtime. So perhaps your friends have created a Dog, Cat and Snake. These are three kinds of Animals, and they each speak differently. When Java asks the Dog to speak, it says “woof”. Whenever Java asks the Cat to speak, it says “meow”. Whenever Java asks the snake to speak, it hisses. There’s the advantage of polymorphism, all we did was define an Animal interface with a Speak method, and we can create a whole bunch of types of animals that speak in their own distinctive way.