OOP’s principles in Java
All the object-oriented programming languages can use this three OOP’s principles and Java is one of the main object oriented programming language.
Three OOP’s principles
Encapsulation is the mechanism of holding the code and the data together and keeping safe from the outside interference and misuse. Its like a protection to the code and the data which prevents other programs to access until and unless it gives the permission . In java, a class is some kind of encapsulation , class holds the data and methods(code) and these are protected by the respective class until and unless the permission is granted. And the permission can be done by specifying the access modifiers which are nothing but public and private. You will learn more about access modifiers in the next chapters.
Inheritance is the process of accessing the information from one object to the other object. Inheritance is an important concept in java because it defines the concept of hierarchical classification. As we learnt earlier encapsulations binds and protects the code and data by the use of the access modifiers like public , private, protected, then why is the concept of inheritance?
The concept of inheritance came into existence to make the program short and simple. For example there is a class called Animals , it is a public class. In Animals class there are three objects named Lions, Elephants and Hippos , one class can have n number of objects to store the data . And there is another class named WildLife and this class objects stores the data regarding the animals in the wildlife. So the WildLife class should create each object for each animal but in the previous case there are already created objects for elephant , lion and hippos. So in order to reduce the code and to improve the performance , WildLife class will use the Animals class data by using inheritance concept. This is how inheritance works in object oriented programming language , if you want learn how java uses this inheritance in the programming please visit our inheritance page.
Polymorphism is a Greek word poly means many , it states that one interface can be used by many different methods in different classes which is simply classified as “one interface , multiple methods” this helps in reducing the complexity and time by allowing the same interface to be used by the different methods.
For example taking a dog analogy , a dog’s sense of smell is polymorphic. If the dog smells a cat it will bark and run after it. If the dog smells it’s food, it will salivate and run to it’s bowl. Here the sense of smell is an interface whereas the actions are methods. So by a glance the difference is what is being smelled , that is , the type of data being operated upon by the dog nose. The same general concept applies to methods within a Java program. If you want to learn polymorphism in programming mode with examples please visit our page.