Home »
Posts tagged "Learn Fundamentals of Java" (Page 2)
Data Input The programs we have written up to now haven’t been interactive. A program is interactive if it is able to take input from the user and respond accordingly. Let us write a program to take user input. To take user input we use the prebuilt Scanner class. This class is available in the java.util package. First we import this class, import java.util.Scanner; Now we create an object of the...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Java Libraries The power of Java comes from the hundreds of Java classes that are already prebuilt and can be used in your programs. To use a prebuilt class and associated methods in those class, all you have to do is to use the keyword import to import the class from the package in which it is contained into your space. The import statements must appear before any class definitions in...
Continue reading »
February 29, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
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
Page 2 of 4« Prev
1
2
3
4
Next »