Oops Concepts in Java
Basically, there are 4 oops concepts in java, they are :
1) Abstraction
2) Encapsulation
3) Inheritance
4) Polymorphism
Abstraction
When we hide internal functionalities and showing only the outcome of any process/machine, etc is known as abstraction.
example: fan
when we switch on the fan starts moving. however how the fan moves mean working of motor, winding, etc is hidden.
lets again take one common example: mobile phone
When we make any call from mobile or receive any call from the mobile phone at that time we only know the input is coming in the form of voice and we need to reply in the same. but how voice is processing to network and what exactly going on in mobile machinery we don't know.
the same concept applies to the java programming language.
in java, we achieve abstraction by using abstract classes and interfaces.
Abstract classes and interfaces are very important topics that we are going to discuss in our upcoming tutorials.
Encapsulation :
gathering or grouping similar things is known as encapsulation.
Example: a geometry box
in geometry box rounder, divider, protector, etc are together combined to form a fully functional geometry box.
let's see another example from our kitchen: a spices box
in the spices box, there are different divisions one for chilly powder second for coriander powder third for salt, etc. all divisions in a combined form become a spices box.
Spices encapsulated in a spices box
Above figure showing encapsulation of different constituents of the same category.
in java, we can say that the gathering of all data including variables and methods together is known as encapsulation.
the result of encapsulation is a class.
Inheritance
the general meaning of inheritance means something that we got from our predecessors.
for example property of parents is inherited by the child.
obviously, parent's property is inherited by their children.
similarly, in java, the child class gets properties of the parent class called inheritance.
or, if one class gets properties or behavior of another class is known as inheritance.
inheritance is a very important concept as per the java programming perspective and we will discuss it in detail in our upcoming tutorials.
Polymorphism
it is a Greek word that means multiple forms of a thing/object, etc
example: a boy
a boy can be a brother of someone or he can be a son of someone or grandson of someone or a student of someone. thus boy is a single entity but he is playing different roles.
Figure showing boy is playing different roles
let's take another example: turmeric
turmeric is a very common spice in India . but apart from its usage as a spice, it is also used in many ayurvedic medicines also it is used for cosmetics. hence, it has multiple forms and uses.
Figure showing turmeric is playing different roles
In java, we can define polymorphism as "one object multiple forms"
or when an object is associated with multiple roles then this phenomenon is known as polymorphism.
this is a very important oops concept and widely used in building applications.
No comments:
Post a Comment