Getter and Setter Methods Private data members of a class cannot be accessed outside the class however, you can give controlled access to data members outside the class through getter and setter methods. A getter method returns the value of a data member. For example we could define a getter method in the Book class for the price data member as given below: double getPrice() { return price; } Similarly, we...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Access Modifiers In the previous section, we allowed access to the data members of the class from outside the class. We created Book objects in the BookTest class and were able to set the object’s data members. However, it is generally not good programming practice to allow data members to be accessed outside the class. By allowing objects to change their data members arbitrarily, we lose control over the values being...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Constructors A data member that is declared but not initialized before using, is assigned a default value by the compiler usually either zero or null. However, it is good programming practice to always initialize variables before using them. A special method member called the constructor method is used to initialize the data members of the class (or any other initialization is to be done at time of object creation). The constructor...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Class Design We are now ready to create a class in Java. A class in Java begins with the keyword class followed by the name of the class. The body of the class is enclosed within curly braces. The body contains the definitions of the data and method members of the class. The data members are defined by specifying their type. The method members of the class are defined just like...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Object Oriented Programming By now you know quite a bit of Java programming. Now we begin to look at Java’s most fundamental feature – Classes and Objects. Java is an Object Oriented Programming (OOP) language. In an OOP language, a program is collection of objects that interact with other objects to solve a problem. Each object is an instance of a class. Imagine you are creating a database application for a...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
User Defined Methods A method in Java is a block of statements grouped together to perform a specific task. A method has a name, a return type, an optional list of parameters, and a body. The structure of a Java method is as below: return_type method_name(list of parameters separated by commas) { statements return statement } The method name is a word followed by round brackets. The parameter list placed within...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Arrays Previously you learned about variables that are place holders for a single value. Arrays are variables that can hold more than one value, they can hold a list of values of the same type. For example, to store the marks of a student, we used a variable: double marks_obtained = 346; To store the marks of another student, we changed the variable marks_obtained. marks_obtained = 144; By changing marks_obtained, we...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
The for statement The for loop is the most widely used Java loop construct. The structure of the Java for statement is as below: for (counter = initial_value ; test_condition ; increment/decrement counter) { statements } Semicolons separate the three parts of a for loop: 1. The initial_value initializes the value of the loop counter. 2. The test_condition tests whether the loop should be executed again. The loop is exited when...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment