Object and Class
It is a basic runtime entity that has
- State
- Behaviour
- Identity
It is an instance(result) of a class. It is made up of attributes and methods
Blueprint from which object is created.
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:
Feature | Description | Example (Car) |
---|---|---|
State | Represents the data/attributes of an object. | Speed, Fuel Level, Color |
Behavior | Represents the functionality of an object. | Accelerate, Brake, Turn |
Identity | A unique identity that distinguishes objects. | Vehicle Registration Number |
Class:
Blueprint from which object is created.Class:
It 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
Accessing a Class and Calling its Members
To use a class, create an object and access its variables/methods.
Example: 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