Need for Encapsulation
- Any program of any programming language contains only two parts.
i.Data
ii.Logic - Out of these two highest priority would be always given to data part.
- Functions /logics would e giving output based on the data.
- There is no proper security for the data part in the structured programming language programs like c - program.
- There was no proper separation between the data and functions of different domains because of which complexity of the programming increases and data insecurity arises
- The concept of proper binding is missing in structured programming languages
Reason for Data Insecurity in C-Programing language:
- It is the GLOBAL VARIABLES which leads to the data insecurity in c- programs
- GLOBAL VARIABLES would be available for both related and unrelated functions also.
- Whenever an unrelated function accesses the data in a wrong way data corruption arises
Sample c -program:
- #include <stdio.h>
- /* global variable declaration */
- int a = 20;
- int main ()
- {
- /* local variable declaration in main function */
- int a = 10;
- int b = 20;
- int c = 0;
- int mul=0;
- c = sum( a, b);
- printf ("a+b = %d\n", c);
- mul= sum( a, b);
- printf ("a*b= %d\n", mul);
- return 0;
- }
- /* function to add two integers */
- int sum(int a, int b)
- {
- return a + b;
- }
- /* function to multiply two integers */
- int mul(int a, int b)
- {
- return a * b;
- }
What is Encapsulation?
- The concept of binding the data along with its related and corresponding functions is known as Encapsulation.
Binding:
- The concept of restricting the availability of the data only to its related areas and related functions it is known as binding.
- Because of binding we can restrict the availability of dta to the unrelated functions
- So that unrelated functions cannot access the data
- Class is the base for encapsulation
Example program:
- package com.instanceofjava;
- public class Student {
- // variables
- int rno;
- String name;
- String clas;
- String Address;
- int rank;
- //setter and getter methods of variables
- public int getRno() {
- return rno;
- }
- public void setRno(int rno) {
- this.rno = rno;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getClas() {
- return clas;
- }
- public void setClas(String clas) {
- this.clas = clas;
- }
- public String getAddress() {
- return Address;
- }
- public void setAddress(String address) {
- Address = address;
- }
- public int getRank() {
- return rank;
- }
- public void setRank(int rank) {
- this.rank = rank;
- }
- //constructor
- Student(int rno,String name,String clas,String Address,int rank){
- this.rno=rno;
- this.name=name;
- this.clas=clas;
- this.Address=Address;
- this.rank=rank;
- }
- public static void main(String args[]){
- Student student= new Student(1, "sai", "class IV", "hyderabad", 1);
- System.out.println("Name: "+student.getName());
- System.out.println("Class: "+student.getClas());
- System.out.println("Rno: "+student.getRno());
- System.out.println("Rank: "+student.getRank());
- System.out.println("Address: "+student.getAddress());
- }
- }
Output:
- Name: sai
- Class: class IV
- Rno: 1
- Rank: 1
- Address: hyderabad
In the program if explain where the bending is happening then new learners can easily understand.
ReplyDeleteYeah, even I too have the same question. I am new in Java. Though I understood the concept clearly but I am not getting exactly where binding is happening in the program.
DeleteKeeping variables and corresponding methods in one place== class==binding happening
DeleteGood explanation.
ReplyDelete