Java Classes and Objects
The fundamental building blocks of Java are classes and objects. They form the core foundation of object-oriented programming.
Java Class:
It is a template or blueprint from which objects are created.
It contains properties of the object which is called variables and contains behavior of the object which is called method.
Class contains different members -
- Variable
- Constructor
- Method
- Block
Example-1:
A Human class is a blueprint that defines the properties and behaviors that every human has and can perform.
- Variables (properties): name, age, gender
- Methods (actions): eat(), sleep(), walk()
So the Human class is just an idea/design/blueprint. It describes common features of all humans.
Example-2:
A Fruit class is a blueprint that defines the properties and behaviors that every fruit has and can perform.
- Variables (properties): name, color, taste
- Methods (actions): grow(), ripen(), eat()
So the Fruit class is just an idea/design/blueprint. It describes common features of all fruits.
How to define class:
Syntax:
public class ClassName {
// variables (data)
// methods (behavior)
}- public → Access modifier. (will understand later)
- class → Keyword used to declare a class in Java.
- ClassName → The name of the class (it should start with a capital letter).
- { } → Curly braces define the body of the class, where variables and methods are written.
- variables (data) → Store the properties or state of the class.
- methods (behavior) → Define the actions or functionality of the class.
Example-1:
public class Human {
// Variables (properties)
String name;
int age;
String gender;
// Method:describes the eating behavior of a human
void eat() {
System.out.println("Human is eating");
}
// Method:describes the sleeping behavior of a human
void sleep() {
System.out.println("Human is sleeping");
}
// Method:describes the walking behavior of a human
void walk() {
System.out.println("Human is walking");
}
}Example-2
public class Fruit {
// Variables (properties)
String name;
String color;
String taste;
// Method:describes how the fruit grows
void grow() {
System.out.println("Eating fruit");
}
// Method:describes the ripening process of the fruit
void ripen() {
System.out.println("Fruit is ripening");
}
// Method:describes eating behavior of the fruit
void eat() {
System.out.println("Eating the fruit");
}
}Java Object:
In Object-Oriented Programming (OOP), the main building block is the object. These objects interact with each other to achieve a goal.
Objects are instances of a class that take memory during runtime.
An object has a state stored in variables, behavior defined by methods, and a unique identity that is a unique name given to the object.
How to create object:
Syntax:
ClassName objectName = new ClassName();- ClassName → The name of the class (blueprint). It tells Java to create an object based on this class.
- objectName → The reference variable (object name) used to access properties and methods of the object.
- = → Assignment operator. It assigns the newly created object to the reference variable.
- new → Keyword used to create memory for the object in the heap and instantiate the class.
- ClassName() → Constructor. It initializes the object and must have the same name as the class.
Example-1 So from previously given example of Human class we are going to create different object.
public class HelloWorld {
public static void main(String[] args) {
// Creating object of Human class
Human person1 = new Human();
// Assigning values to object properties
person1.name = "Ashish";
person1.age = 20;
person1.gender = "Male";
// Using object properties
System.out.println(person1.name);
System.out.println(person1.age);
System.out.println(person1.gender);
// Using object methods
person1.eat();
person1.sleep();
person1.walk();
}
}We created an object of the Human class named person1(identity), assigned values to its properties (name, age, gender), and then accessed those properties and called its methods (eat(), sleep(), walk()).
In this way, we can create any number of objects from the Human class.
All the objects share the same class (blueprint/structure), but each object has its own separate data/state (properties)
Creation of an object is known as instantiation.
See you in next page....🙂