Wednesday, October 5, 2016

Muhammad Akbar

Java Object and Classes Tutorial

The basic definition of java contains a phrase: object oriented. What does that means actually? Let us take some practical world examples.
Assume you have a collection of reading books, and your collections are filled with different kind of books, for examples novels, poetry, adventure books, programming books etc. These books are real objects; it has title, weight, volume, color, publisher, author, price, genre etc. Now assume you are trying to make a common template or blue print for representing all of your books, which may look like below:
Java language specifications exactly reflect the real world scenario. In java language, the template to represent real objects is called class and the real objects are called objects.
You can imagine that the template is a logical thing; it has no real world existence. The book which you have in your home has real world existence (physical). Translating this to java : class is a logical thing, whereas objects are physical things.
So, considering the Book example given above, if you want to do exactly the same work in Java, you will  create a class named Book.
The only program we have written so far, has a class named LearnJava. You may be wondering what kind of Template is this class. To answer this we can say, it is not used yet as a template, but can be used.
Now you know about class, let’s create a Book class with some variable. (I guess you have opened eclipse and remember how to create a class)
package com.java;

public class Book 
{
	// instance variable
	String title;
	String author;
	String genre;
	double price;

	public static void main(String[] args) 
	{
		// creating first object of Book
		Book book1 = new Book();
		// assigning instance variable values
		book1.title = "Java Complete Reference";
		book1.author = "Herbert Schield";
		book1.genre = "Computer Programming";
		book1.price = 300.0;

		// creating second object of Book
		Book book2 = new Book();
		// assigning instance variable values
		book2.title = "One, Two, Three, Infinity";
		book2.author = "George Gamow";
		book2.genre = "Science";
		book2.price = 600.0;

		System.out.println("I have two books. Which are");
		System.out.println(book1.title + " written by: " + book1.author	+ " and the price is :" + book1.price);
		System.out.println("And");
		System.out.println(book2.title + " written by: " + book2.author	+ " and the price is :" + book2.price);

	}
}
Believe me, though there is many things to explain, basically the program is simple. Pay attentions to the comments!
Your first encounter with the unknown things is happening in this line:
Book book1 = new Book();
What is this line doing actually? In the comment, it is written that this is creating first object of Book. Indeed. What does this mean? Before this line you had written a class (template) with no objects. Let us break up the line:
Book book1;
book1 = new Book();
First line creating a reference of Book.(nothing interesting). The reference is pointing to null.
In the second line, on the right hand side of the = operator there is an interesting statement newBook();. By this statement you are creating a new object of Book class and assigning memory space for it.
Then you are assigning the reference book1 to this new object. So the book1 reference pointing now to the object, or in other words to the memory location.
Let’s explain this by a small diagram. 
Now you have an object of Book. You can refer the object with the name book1 (reference). Remember we said that object has physical existence? It is the object which takes some space in memory. It has existence, some memory space has been allotted for this object. For class ( Book, the logical template) there is no memory allocation.
All this is done with one of most important operator in Java: new! This is the standard way to create object in Java.
Ok, some memory space has been allotted for the object. But what it will store in that space? You have created the Book class with four variable, this variable is stored in the memory space. Check the comment section for variable declaration, it is said as instance variable.
 Java objects are called instance of class. So, when you will create an object of a class, that object (instance) will have all the instance variable in its memory space the class has defined. The variables are called object property.
Can you imagine, what values are these variables now containing? Nothing ! Because we have not assigned it any values yet.
Now check the next four lines of code:
// assigning instance variable values
book1.title = "Java Complete Reference";
book1.author = "Herbert Schield";
book1.genre = "Computer Programming";
book1.price = 300.0;
We have assigned the four variables some values. After these line, the memory space will contain the assigned values for the respective variables.
So now the memory space snapshot (with high zoom camera, probably you will require around 400X zoom) for object which book1 is pointing now will look like:
 Nice, right?
You can imagine what is happening when we are creating the second object ( book2) and assigning values to it. When we are writing the statement:
Book book2 = new Book();
The right hand side of = operator again creating a new object, again allocating some memory space ( obviously different from the first memory space) for the object , and the reference book2 pointing to the new memory space.
Now at the end we want to see that the values which we have stored has been stored properly or not. (though, probably you have seen it with the high zoom camera, we want to prove it programmatically)
How can we access the first object ? We need a reference. We already have it, this is: book1.
Check this line:
System.out.println(book1.title + " written by: " + book1.author	+ " and the price is :" + book1.price);
What book1.title is doing, it is going to the memory space for the first object, fetching the value of title and giving it to the output. Same is happening for other variables as well.
In exactly the same way we are getting the values for book2.
Nomenclature issue: Although it is common practice to say book1 object or book2 object, you know that book1 and book2 are reference only, which is pointing to some objects. But go according to the common practice, so at the time of saying it, you can say that in this program we have two objects of Book class; book1 and book2. Keep behind of your mind that book1 and book2 are reference of Book class, which is at this time pointing to some objects. At some other point of time, through programming, we can make those reference point to some different object or to no object at all (we will show you how).
Run this program: (right click on the file and then Run as Java Program)
The o/p will be:
I have two books. Which are
Java Complete Reference written by: Herbert Schield and the price is :300.0
And
One, Two, Three, Infinity written by: George Gamow and the price is :600.0

Which is exactly what is expected.
Now play a little with the code:
Let’s say that the price has changed for the “Java Complete Reference” book.  The new price is 450. So you want to change it. How can you do this?
Again we have to take help of the reference, which is in this case book1.
So instead of the previous code, we will write this part of code while assigning values for the variable for the first book: 
// assigning instance variable values
book1.title = "Java Complete Reference";
book1.author = "Herbert Schield";
book1.genre = "Computer Programming";
book1.price = 450.0;
Save the file and see whether the change has been reflected. You should get a o/p now as:
I have two books. Which are
Java Complete Reference written by: Herbert Schield and the price is :450.0
And
One, Two, Three, Infinity written by: George Gamow and the price is :600.0

Before ending this tutorial we want to say some very little thing about package. As you have seen that the first line of our program is containing a line:
package com.java;
We told you this line is coming because you have created a package com.java under src folder. Package is an important concept of Java. Package is nothing but folder structure. Go to your workspace folder in your local drive. Go to the project folder, then to src -> com – java. You can see that com and java folder has been created automatically, it is been created when you have created a package com.java.
Now can you save two file with the same name and extension within a folder? For example try to save ‘Please Forgive Me.mp3’ twice under your Brain Adams folder. You can’t ! You have to rename either one of the file. Java package exactly do this. It creates a folder and enforce namespace. That means in our project inside ‘java’ folder you can not create a second class Book.java now. Try to do this and see what eclipse say.
It is a wonderful and absolutely necessary practice to use package. It separates the different class and make sure you don’t use same class name (which will create confusion to both you and compiler) under same package.

   
         

Methods in Java

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you