Class:
Think of a class as a blueprint or a plan for creating something. Let’s say you want to build a toy car. You would start by drawing a plan that shows how the car should look, what color it should be, how big the wheels should be, and so on. This plan is like a class. It tells you what the car should be like but doesn’t actually make the car.
Definition: A class is a blueprint or a template that defines the properties (attributes) and behaviors (methods) of objects. It serves as a blueprint for creating multiple instances of objects with similar characteristics. A class encapsulates related data and functionality into a single unit.
Attributes: Also known as instance variables or fields, attributes represent the state or characteristics of an object. They define the data associated with an object. For example, a Car class may have attributes like color, make, and model.
Methods: Methods represent the behaviors or actions that objects can perform. They define the operations or functionality associated with an object. For example, a Car class may have methods like start(), accelerate(), and brake().
public class Car {
// Attributes
String color;
String make;
String model;
int year;
// Methods
void start() {
// code
}
void accelerate() {
// code
}
void brake() {
// code
}
}
Object:
An object is like the actual thing you build using the blueprint (class). So, when you follow the plan (class) and make a real toy car, that toy car is an object. It’s a specific instance or example of what the class describes.
Definition: An object is an instance of a class. It represents a specific occurrence or entity based on the class blueprint. An object is created using the new keyword followed by the class name and parentheses (if no constructor arguments are required). Each object has its own set of attributes and can invoke the methods defined in its class.
Car car1 = new Car();
Car car2 = new Car();
Accessing Attributes and Invoking Methods:
car1.color = "Red";
car2.model = "SUV";
car1.start();
car2.accelerate();
Constructor in Java
In Java, constructor is a block of codes similar to method. It is called when an instance
of object is created and memory is allocated for the object. It is a special type of method which is used to initialize the object.
When is a constructor called?
Every time an object is created using new() keyword, at least one constructor is called.
It is called a default constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor:
-
Constructor name must be same as its class name
-
Constructor must have no explicit return type
Types of java constructors:
There are two types of constructors in java:
-
Default constructor (no-arg constructor)
-
Parameterized constructor
-
Copy constructor
Java Default Constructor
A constructor is called “Default Constructor” when it doesn’t have any parameter.
Example of default constructor:
class Bike1 {
Bike1() {
System.out.println("Bike is created");
}
public static void main(String args[]) {
Bike1 b = new Bike1();
}
}
Parameterized constructor
The parameterized constructor is used to provide different values to certain objects.
Example:
class Student4 {
int id;
String name;
Student4(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student4 s1 = new Student4(111, "Karan");
Student4 s2 = new Student4(222, "Aryan");
s1.display();
s2.display();
}
}
Copy constructor
A copy constructor in a Java class is a constructor that creates an object using another object of the same Java class
EXAMPLE:
class Student6 {
int id;
String name;
Student6(int i, String n) {
id = i;
name = n;
}
Student6(Student6 s) {
id = s.id;
name = s.name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
Student6 s1 = new Student6(111, "Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Constructor Overloading in Java
Constructor overloading in Java is a technique of having more than one constructor
with different parameter lists. They are arranged in a way that each constructor
performs a different task. They are differentiated by the compiler by the number of
parameters in the list and their types.
class Student5 {
int id;
String name;
int age;
Student5(int i, String n) {
id = i;
name = n;
}
Student5(int i, String n, int a) {
id = i;
name = n;
age = a;
}
void display() {
System.out.println(id + " " + name + " " + age);
}
public static void main(String args[]) {
Student5 s1 = new Student5(111, "Karan");
Student5 s2 = new Student5(222, "Aryan", 25);
s1.display();
s2.display();
}
}