Let us say you need to track the academic records of 'N' number of students in a class. The program should print out the name of student, roll no and GPA for the student.
So we create a bean class Student. Now to test the above wrong we write following code in main class
Now in terms of the above program, the concepts can be explained in this way.
public static void main(String args)
Student s1=new Student();
Student s2=new Student();
System.out.println("Student Name : Roll No : GPA");
Two objects of class Student, S1, S2 are accessing the variables rollNo,Name,GPA through a method. Here you are binding the data(variables) with a method. Or in some cases you could also bind one variable with another. In more complex applications, data binding is where you get two disparate data sources talking to each other, for eg XML to Database.
Here you have two different objects S1,S2 accessing the data. Now S1 is not aware of the value of data being accessed by S2 and vice versa. The actual data is visible only to the variables or methods that are accessing it, and remains hidden from other objects.
If we look at the above example, we are using String as a data type, but it is also a Class by itself in Java. Again Student is a class, but when we create an object of it as Student S1, in this case we are defining a variable of datatype Student. In this case an object could be serving a dual purpose, be used as a data type as well as a Class.
In the Student class above we are binding the private variables name, rollNo, GPA with the functions. When we create an object of Student, it internally has functions and data that are being bound with each other. Encapsulation is process of the data and function binding together in one single entity. Think of it as the hood of the car, under which you have a whole lot of operations going on. So let's say I add a method computeGPA that takes in the student's scores for semesters and gives the output. You are using only one single Student object, internally that has methods which bind with variables to do the operation.
Now the client testing the Student class, will invoke the GPA method as shown. Student s1=new Student(); s1.computeGPA(2.2,4.4, 5.5). Here you have the method that internally computes the overall GPA and returns the final result. The user who is calling the method, does not get to see it's implementation nor the binding there, it only sees the method. Abstraction is process of hiding the implementation details, and exposing only the interface to the client.