Let's start with very simple examples to understand classes
and objects
.
Suppose, you want to drive a car, and based on road conditions you want to control the speed and direction. But before you can drive a car, the car should be designed following some engineering drawings. These engineering drawings include designs for different parts of the car and their functionality. An accelerator pedal will be designed to control the speed of the car, similarly, steering will be designed to control direction. But the complex internal mechanisms are kept hidden from normal users so that the users can use them without having any technical knowledge. Furthermore, you can't drive a car until a car is built.
Similarly, if you want to build a house in your locality, you have to consult with a civil engineer for preparing a blueprint for the building. You have to share your requirements. But as a normal user, you don't need to understand how the constructor will build the house. Also in this context, you can't stay in the home until it is built.
In both of the engineering drawings, the details of the cars/house are mentioned, like the number of doors, and the number of windows. In the case of cars, details of all the parts like steering, pedals, engine, fuel chamber, etc. are mentioned. Similarly, the number of rooms, types of rooms, colours, etc. are mentioned in designing a house.
Now the question is - Can you differentiate a car from a house? If you can, then how can you differentiate?
The answer is very simple. The properties of a car and a house are not the same. These properties are known as attributes.
In this context, my next question is - Can you differentiate between two cars or two houses? If you can, then how do you differentiate them?
I think the answer is simple enough. The values of the attributes of the two cars are different. Similarly, the values of the attributes of two houses (like my house and my friend's house) are different. So, car and house are two classes. But my car and my friend's car are two objects (or instances) of car class. Similarly, my house and my friend's houses are two objects of house class.
Thus, a class is nothing but a prototype or design. Whereas an object is an instance of the class, which has real existence. Speed up or speed down, turn left or right, etc. are the methods of the car class. Similarly, setting or getting the number of rooms, doors, color, etc. are the methods
of house class.
Classes with Instance Variables
Here we are defining two classes, namely Car and House, where only instance variables are declared. In this tutorial, we are following a naming convention where instance variables start with the _ symbol.
Car.java
House.java
Classes with Instance Variables and Methods
The following sample programs contain instance variables and access methods. The classes are defined just for your understanding, that's why they contain very few numbers of variables and methods. Being an expert in future you will understand that many more things you should keep in your mind at the time of designing a class. Let us define the classes as follow:
Car.java
House.java
Hope, you will be able to define simple classes.
Hiding Instance Variables
At this moment, you have to understand how to hide variables from random access. Most of the time, we need to define classes where some of the variables can not be modified after creating the instances. Thus, if we define a class as we have discussed earlier, the attributes can be updated. So to impose this restriction, we need to apply some tricks so that some of the attributes can not be changed. Let us discuss this with an example.
Employee.java
TestMain.java
In this above example, the employee id is changed after creating the object emp1
. Thus, to restrict it the attributes of the Employee class need to be declared as private and all the attributes need to be accessed by methods. If an attribute should not be changed after creating the object, you should not define any method by which that value can be altered. Let us redefine the classes as follows.
Employee.java
TestMain.java
In the above example, the attributes of the class are private, thus they can not be accessed from outside of the class. So, the setter methods have been defined only for those attributes which are modifiable from other locations. But, the getter methods have been defined for all the attributes.
Generally, the setter method definition is discouraged. The setter methods are defined when it is necessary to use.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.