Class, Attributes, and Methods in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Class, Attributes, and Methods in Java

Share This

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
public class Car { String _number; int _noOfWheel; int _noOfDoors; int _noOfWindows; String _color; float _maxSpeed; float _currentSpeed; }
House.java
public class House { int _noOfRooms; int _noOfDoors; int _noOfWindows; String _color; float _carpetArea; float _supBuiltArea; }


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
public class Car { String _number; int _noOfWheel; int _noOfDoors; int _noOfWindows; String _color; float _maxSpeed; float _currentSpeed; void speedUp() { _currentSpeed = _currentSpeed + 1; } void speedDown() { _currentSpeed = _currentSpeed + 1; } }
House.java
public class House { int _noOfRooms; int _noOfDoors; int _noOfWindows; String _color; float _carpetArea; float _supBuiltArea; public int get_noOfRooms() { return _noOfRooms; } public void set_noOfRooms(int noOfRooms) { this._noOfRooms = noOfRooms; } public int get_noOfDoors() { return _noOfDoors; } public void set_noOfDoors(int noOfDoors) { this._noOfDoors = noOfDoors; } public int get_noOfWindows() { return _noOfWindows; } public void set_noOfWindows(int noOfWindows) { this._noOfWindows = noOfWindows; } public String get_color() { return _color; } public void set_color(String color) { this._color = color; } public float get_carpetArea() { return _carpetArea; } public void set_carpetArea(float carpetArea) { this._carpetArea = carpetArea; } public float get_supBuiltArea() { return _supBuiltArea; } public void set_supBuiltArea(float supBuiltArea) { this._supBuiltArea = supBuiltArea; } }

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
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } }
TestMain.java
public class TestMain { public static void main(String[] args) { Employee emp1 = new Employee(1, "Arnab", "Developer", "Kolkata", "M", "22.10.1998", "10.05.2017"); System. out.println("Employee id : " + emp1._empid); emp1._empid = 10; System.out.println("Employee id : " + emp1._empid); } }

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
public class Employee { private int _empid; private String _name; private String _designation; private String _address; private String _gender; private String _dob; private String _dateOfJoining; public int get_empid() { return _empid; } public String get_name() { return _name; } public String get_designation() { return _designation; } public void set_designation(String _designation) { this._designation = _designation; } public String get_address() { return _address; } public void set_address(String _address) { this._address = _address; } public String get_gender() { return _gender; } public String get_dob() { return _dob; } public String get_dateOfJoining() { return _dateOfJoining; } public void set_dateOfJoining(String _dateOfJoining) { this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } }
TestMain.java
public class TestMain { public static void main(String[] args) { Employee emp1 = new Employee(1, "Arnab", "Developer", "Kolkata", "M", "22.10.1998", "10.05.2017"); // System.out.println("Employee id : " + emp1._empid); can not be accessed emp1.set_address("Delhi"); emp1.set_dateOfJoining("02.01.2018"); emp1.set_designation("Senior Developer"); System.out.println("Employee [_empid=" + emp1.get_empid() + ", _name=" + emp1.get_name() + ", _designation=" + emp1.get_designation() + ", _address=" + emp1.get_address() + ", _gender=" + emp1.get_gender() + ", _dob=" + emp1.get_dob() + ", _dateOfJoining=" + emp1.get_dateOfJoining() + "]"); } }

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.




Happy Exploring!

No comments:

Post a Comment