Saturday, 26 August 2017

Objects in Java

                         Objects in Java


In general sense an Object is any physical or conceptual thing that we find around us. If we look around we will see many examples of real-world objects: your classmate, your table, your television etc.
The above three objects mentioned share similar characteristics. 

They all have identity, state and behavior.
 For example your dog has state (name, color, breed) and behavior (barking, fetching and wagging tail).
Identity is the name given to an object, like name given to a dog.
So when you create a class and create an object of that class the object will be having access to the members or fields of that class and we access the members of that class by using the object.

Imagine the Object of  a class as an agent of that class. Whenever you need to access the members of that class you need to do it through the agent (in our case the Object) we created for that class.
Consider a sample program below.

Carefully note that I have creates two different classes; SampleClass and SampleObject.

I have declared the values of a,b and c in the class SampleClass.
Now suppose I need to access these members from other class so I will create a class names SampleObject.


The next thing I do is creating the object of that class whose members I need to access. In our case it is SampleClass and the members that I need to access are a,b,c.


Syntax for

creation of Objects using the “new“ keyword.

Here we shall learn how to create an object of a particular class.
The general syntax is given below.



 

By using the above syntax mentioned we can create the object for class SampleClass as below.

          SampleClass  obj = new SampleClass();

The above statement will define an object named “obj” and by using this object we access the members of class SampleClass.

In nest post we shall discuss the role of "new" keyword and some examples of object creation.

Bye! Keep visiting.
 


More about classes

                          General Form of a Class      

So a “class” in java is a fundamental building block of a program or in simple words I can say that it is a name given to the program.
There may exist a question that “Why would one give a name to a program using “class” while the purpose of the program is to get execute and just run on a machine”. The above statement could be true in some cases; for example consider in C Language we will not mention any class name there, all we do is to write and run the code.
But remember that C is a structure-Oriented language and Java is a Object-Oriented Language. So, by naming the class in java you are able to implement the features of OOP like Abstraction, Encapsulation and Data hiding.
You will see how we use class names to implement these OOP principles mentioned above in further posts.

For now consider the below picture which depicts the generalized form of class
The instance-variables are the member fields of the class. They show the characteristics of the class.

The methods could be declared inside the class and could be accessed within the class. If any other class wants to access these members then that particular class should create an object of the class whose members it needs to access.

The Object creation will be discussed in the next post.

Bye! Keep visiting

Thursday, 24 August 2017

Class in Java



“class”  In  Java:

So a “class” in java is a fundamental building block of a program or in simple words I can say that it is a name given to the program.
There may exist a question that “Why would one give a name to a program using “class” while the purpose of the program is to get execute and just run on a machine”. The above statement could be true in some cases; for example consider in C Language we will not mention any class name there, all we do is to write and run the code.
But remember that C is a structure-Oriented language and Java is a Object-Oriented Language. So, by naming the class in java you are able to implement the features of OOP like Abstraction, Encapsulation and Data hiding.

Consider a pic(almost) that shows the difference between c and java.



You will see how we use class names to implement these OOP principles mentioned above in further posts.



In next post we will deal the structure and general form of a class.
BYE! Keep visiting

             The `this` keyword in java The this keyword in java can be used for different purposes. Some of these are listed below. ...