What is meant by encapsulation in java?
- Binding the data with its related functionalities known as encapsulation
- Here data means variables and functionalities means methods.
- So keeping the variable and related methods in one place.
- That place is "class". class is the base for encapsulation.
- Lets see example program on encapsulation, how the variables and methods defined in a class
What is encapsulation in object oriented programming?
Basic java example program on data encapsulation:
- package com.java;
- public class EncapsulationDemo {
- String name;
- int rno;
- String address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getRno() {
- return rno;
- }
- public void setRno(int rno) {
- this.rno = rno;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public void showInfo(){
- System.out.println("Name: "+getName());
- System.out.println("R.No: "+getRno());
- System.out.println("Name: "+getAddress());
- }
- }
Class:
The above example program shows how the variables and methods will be there in a class.
So class is the base for encapsulation
class is user defined data type in java means by using class we can structure our own programs.
Lest see a example program on class.
- package com.java;
- public class ClassDemo {
- //variables of a class
- String empName;
- int empId;
- String Designation;
- //getter and setter methods for variables
- //by using these methods we can set the value to the variable and gat the value from the
- //variables
- public String getEmpName() {
- return empName;
- }
- public void setEmpName(String empName) {
- this.empName = empName;
- }
- public int getEmpId() {
- return empId;
- }
- public void setEmpId(int empId) {
- this.empId = empId;
- }
- public String getDesignation() {
- return Designation;
- }
- public void setDesignation(String designation) {
- Designation = designation;
- }
- }
Object:
Instance of class is known as object.
Instance means dynamic memory allocation. So dynamic memory allocation to class is called object.
By using "new" keyword the object will be created dynamically.
Class is a template. By using object we will get memory for all variables so that we can use them.
We can create number of objects for one class based on our requirements.
Below an example program on class and object.
- package com.java;
- public class User {
- String Name;
- int Id;
- String address;
- public String getName() {
- return Name;
- }
- public void setName(String name) {
- Name = name;
- }
- public int getId() {
- return Id;
- }
- public void setId(int id) {
- Id = id;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public static void main(String [] args){
- User obj= new User();
- obj.setName("sai");
- obj.setId(1);
- obj.setAddress("xyz");
- System.out.println(obj.getName());
- System.out.println(obj.getId());
- System.out.println(obj.getAddress());
- }
- }
Output:
- sai
- 1
- xyz
Nice Post. Keep posting. java training
ReplyDeletejava online training