Class and Objects
In object-oriented programming technique, we design a program using objects and classes.Object is the physical as well as logical entity whereas class is the logical entity only.
Objects in Java
An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical.t is a basic unit of Object Oriented Programming and represents the real life entities.A typical Java program creates many objects, which interact by invoking methods. An object consist of :1. State : It is represented by attributes of an object. It also reflect the properties of an object.
2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
3. Identity : It gives a unique name to an object and enables one object to interact with other objects.
If you compare the software object with a real-world object, they have very similar characteristics. Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.
Declaring objects
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.Syntax for declaring object
Person somebody;
As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. Class in Java
A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.A class in Java can contain:- fields
- methods
- constructors
- blocks
- nested class and interface
Syntax for declaring class
class {
field;
method;
}
A class can contain any of the following variable types.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
A variable which is created inside the class but outside the method, is known as instance variable. Instance variable doesn't get memory at compile time. It gets memory at run time when object(instance) is created. That is why, it is known as instance variable.
Creating an object
As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.There are three steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.As we shown above
Person somebody;
Instantiation − The 'new' keyword is used to create the object.
new Person();
Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Person Hansraj = new Person();
Object and class example
Program
//declaring class
class Employee
{
int emp_id;
String name;
}
public class Main
{
public static void main(String args[])
{
//make instance of Employee class through refrence
Employee e1 = new Employee();
e1.emp_id = 1234;
e1.name = "Raj";
System.out.println(e1.name + " \'s employee id is " + e1.emp_id);
}
}
Output
Raj's employee id is 1234
Comments
Post a Comment