Java is an Object-Oriented Language. And classes and objects are the fundamental components of OOP’s.
Objects are real-life entity and classes are the blueprint for creating an object. For example: consider a class for a car, it has color, some weight, size, etc, these are its attributes and drive, gear, brake, etc are its behaviour.
CLASS
A class is a user-defined blueprint or prototype for creating Objects. It is a logical entity. We can say that classes are categories, and objects are items within each of those categories.
A class in Java contain:
- Methods.
- Fields.
- Constructors.
- Interfaces.
For the declaration of a class following components are included:
1. Modifiers:
A class can be public or default access.
2. Class name:
A class should have a name with an initial letter Capitalized.
3. SuperClass(if any)/ Interfaces:
If required to include SuperClass then proceed by the keyword ‘extends’. Using the keyword implements, a class can have more than one interface.
4. Body:
A class boundary is within the curly braces'{}’.
Syntax for class:
class ClassName
{
// field;
//method;
}
Syntax example:
public class Dog
{
//variables
int age;
String color;
//Methods
void eat()
{
}
void bark()
{
}
}
OBJECTS:
It can be defined as real-world entities that have state and behavior. An object is a runtime entity. Eg. bike, Lamp, pen, table, etc. An Object is an instance of a class. It is a physical as well as a logical entity.
Objects has three characteristics:
- State: It is represented by attributes of an object that is the data value.
- Behaviour: It represents the functionality that is the behaviour of an object. Example withdraw, deposit, etc.
- Identity: It is a unique ID given to each Object created so that JVM can identify all the objects uniquely.
Example:
Name of an Object: dog
Identity: Name of the dog.
State: age, color, breed, size, etc.
Behaviour: eat, sleep, bark, run, etc.
Creating an Object:
As mentioned above that class is a blueprint for objects. So, Object is created from a class. And ‘new’ Keyword is used to create an Object.Three steps to create Object:
- Declaration: Declaration of a variable with the variable name with an object type.
- Instantiation: When an object of a class is created, it is said to be instantiated. And the ‘new‘ keyword is used to create an object.
- Initialization: The ‘new’ operator allocates memory for a new object that is being instantiated. Also invokes the class constructor.
Syntax of Object:
ClassName ReferenceVariableName = new ClassName();
Following Example shows the creation of an Object:
public class Dog
{
//constructor having one parameter 'name'
public Dog(String dogName)
{
System.out.println("My Dog Name is :" + dogName );
}
public static void main(String []args) {
// Creating an Object
Dog myDog = new Dog( "Bruno" );
}
}
Output:
My Dog Name is :Bruno
Creating Multiple Objects:
Multiple Objects can be created for the same class.
The following example shows that Objects are created for One class, invoking different variables.
class MyClass
{
int x = 10;
int y = 20;
}
public class Main
{
public static void main(String[] args)
{
MyClass Obj1 = new MyClass();
MyClass Obj2 = new MyClass();
System.out.println("Value of x:" +Obj1.x);
System.out.println("Value of y:" +Obj2.y);
}
}
Output:
Value of x:10
Value of y:20
Example of class and objects in Java:
public class Dog
{
// Instance Variables
String name;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name,int age, String color)
{
this.name = name;
this.age = age;
this.color = color;
}
public void display()
{
System.out.println("Hi my name is "+ name);
System.out.println("My age is "+ age);
System.out.println("My body color is "+ color);
}
public static void main(String[] args)
{
Dog myDog = new Dog("Bruno",1,"white");
myDog.display();
}
}
Output:
Hi my name is Bruno
My age is 1
My body color is white