Class and Object

Let's take an example to understand Class and Object. Let's say one college published its admission form which students can take, fill it and can submit to take admission into the college.
Two students Alice and Bob took two forms, filled in their details and submitted. Here both the filled forms have the same format. Only the data in each field may be different for two different students.
This is the concept of class and object.
Class is a template and objects are created from a class (template) but they have data filled for each field. Different objects may have different data for the fields. This concept can be represented as the below image.


The fields of a class are called Members. Additionally a class can have Methods inside it. Methods are basically the behaviour of a class. If we represent the above scenario into java class then it would look like:


There are two categories of methods shown in the above image. The method 'getAveragePercentage' is a behaviour of the class 'AdmissionForm' which gives us the average percentage value of a student. This makes the task of the admission clerk easier. He can call this method to get the average value. Another category of method is Getters and Setters. These are also behaviour of a class but they perform a specialized task. As we know private members cannot be accessed outside of the class, the getter methods provide the member values to the outside world. Getter methods are used outside of the class to get the member values. This is the reason all getter methods are declared as public. Similarly setter methods are also used outside the class to set or initialize the values of the members. This is the reason setter methods are public and they take one argument which is to be set as the member value.
Now let us see how the admission clerk uses these objects to select a student for admission. Let us say the student having higher average percentage marks will get admission.




The output is: 

This example illustrates how getter and setter methods are used by the admission clerk to select a student who has secured higher average marks.

I hope you can now understand class, object, method, getter and setter methods.

If you have any questions please write in comment. I will answer them.

Thanks,
S. R. Giri

Comments

Popular posts from this blog

Welcome