Object-and-Class
Object
- It is a basic runtime entity that has a state & behaviour & identity.
- It is an instance(result) of a class.
- It is made up of attributes and methods
Syntax :
class_name object_name= new class_name();
Example:
B obj= new B(); //here obj is attribute
obj.s(); //here s() is method
Characteristics of the objects:
- State: It represents the value of an object.
- Behaviour: It represents the functionality of an object.
- Identity: Gives uniqueness to the object i.e. student ID card etc.
Example of the three characteristics of an Object:
- In the real world, we can consider a Car as an object.
STATE BEHAVIOUR IDENTITY
Speed Gear vehicle
Class
- Blueprint from which object is created.
JAVA class contains:
- Fields.
- Methods.
- Constructor.
- Blocks.
Syntax:
access_modifier class class_name
{
// var
// method
// constructor
// blocks and statement.
}
Example: Creation of class
Access
modifier keyword class_name
| | |
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello");
}
}
Output:
hello
Example: for calling and accessing the class
package quipohouse;
public class HelloWorld {
int a = 10;
public static void main(String[] args) {
HelloWorld obj = new HelloWorld(); // here helloWorld is a class and obj is an object
System.out.println(obj.a); // calling instance variable by its object
}
}
Output:
10