What is OOP in Java?
OOP (Object oriented programming) refers to a programming methodology based on objects instead of just functions and procedures. In this blog, guide how to do encapsulation in java program.
What are the main feature of OOP?
- Encapsulation:
Encapsulation is Combination of data and function in a single unit. This is use to hide the implementation details from user. That is why is also known as “data Hiding“ for this hiding approach. The main method for data invoke is setter and getter method.
- Inheritance:
Inheritance is a process by which object of any class have access of other classes data and methods or all the properties. It is a mechanism in which one object acquires all the properties and behaviors of a parent object. Derived class known as child class. The class that inherits from another class · The base class namely parent class. In the class being inherited from.
- Polymorphism :
Polymorphism refers the ability to take more than one form. And the most common use of polymorphism in OOP is, when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one is considered to be polymorphic.
- Abstraction :
Abstraction in OOP able to hides implementation details in class. It can also be referred to as modeling and is nearly conjuncted to the ideas of theory and design.
Now discuss about Encapsulation in java:
Encapsulation( known as data hiding) is a mechanism of wrapping the variables and methods together as a single unit. No outside class can access private data member or variable of other class. That is variables of a class hidden from other classes. In order to invoke that variable and method, one can be accessed only through the getter setter methods provided by current class. This way data can only be accessed by public methods thus making the private fields and their implementation hidden for other classes.
Encapsulation achieve −
- Declare the variables of a class as private.
- Provide getter and setter methods in public (modify and view the data values).
- Java encapsulation keep related fields and methods together in order to easy to code and read.
It control the values of data fields.
For example,
class Encap{
private int counts;
public void setCounts(int counts) {
if (counts >= 0) {
this.counts = counts;
}
}
}
Here, counts variable private and applying logic inside the setCounts() method. Can be achieved data hiding using encapsulation. In the above example, if change the length and breadth variable into private, then the access to these fields is restricted.
Following is an example of Encapsulation in Java :
public class MySelf
{
private String name;
private String deptName;
private int age;
public String getName()
{
return name;
}
public String getdeptName()
{
return deptNamename;
}
public int getAge() {
return age;
}
public void setName(String name)
{
this.name = name;
}
public void setdeptName(String dName)
{
this.dName = dName;
}
public void setAge(int age)
{
this.age = age;
}
}
Following MySelf class, if any class wants to access the variables(Name,Age,deptName) should access them through these getters(i.e getName() ,getdName(),getAge()) and setters(i.e setName(),setdName(),setAge()).
The variables of the MySelf class can be accessed using the following program −
public class UseMySelfClass
{
public static void main(String args[])
{
MySelf myself= new MySelf ();
myself.setName(“Harun”);
myself.setdeptName(“ICE”);
myself.setAge(22);
System.out.print(“Name : ” + myself.getName());
System.out.print(“Dept-Name : ” + myself.getdeptName());
System.out.print(” Age : ” + myself.getAge());
}
}
This will produce the following result −
Output
Name : Hello
Dept-Name : ICE
ICE Age : 22
Advantages of encapsulation:
Encapsulation use improves flexibility and re-usability. For instance, In the above implemented code of void setName(String name) ,void setAge(int age) and setdeptName(String dName) can be changed at any point of time. Since the implementation is wholy hidden for other classes, yet they would still access the private field Name using the same methods (setName(String name) and getName()). Therefore, the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the class.
The fields can be made read-only, if don’t define setter methods in the class. In other saying, field can be write-only, if don’t define the getter methods in the class, such as,
getName() // provides read-only access
setName() // provides write-only access
Example, If class have only a field or variable that don’t want to be changed, simply define the variable as private and instead of setter, getter. Both just need to define the get method for that variable. As the set method is not present there is no way an outside class can modify the value of that field.
User would know update a field by calling set method and to read a field call get method. set and get methods activity hidden from user.
See more Topic on Java OOP with real-time example
Nested class in JAVA with EXAMPLES
What is the defference between String, StringBuffer and StringBuilder.
Java Interface tutorials with Example.