1.Difference between class and Object in Java ?
2.What is constructor.How is it different from a method?
It is used to set default or user-defined values for the object's attributes
Primary Purpose
Initializes the state of a new object.
Invocation
Called automatically when an object is created (usually with the new keyword).
Return Type
Does not have a return type, not even void.
Naming
Must have the exact same name as the class.
Inheritance
Is not inherited by subclasses.
Default Version
Provided automatically by the compiler if none are defined.
Key Differences
Feature Constructor Method
Primary Purpose
Initializes the state of a new object.
Defines the behavior or functionality of an existing object.
Invocation
Called automatically when an object is created (usually with the new keyword).
Must be called explicitly by the programmer using the object reference.
Return Type
Does not have a return type, not even void.
Must specify a return type (e.g., int, String, or void).
Naming
Must have the exact same name as the class.
Can be given any valid name.
Inheritance
Is not inherited by subclasses.
Can be inherited and overridden by subclasses.
Default Version
Provided automatically by the compiler if none are defined.
Never provided automatically; must be explicitly defined.
how is it different from a method?
A Constructor helps in initialising an object that doesn't exist. A Method performs functions on pre-constructed or already developed objects.
Why is constructor used?
To give initial values to object variables.
This() and Super()
.this Keyword (Avoid confusion)
Used when class variable and parameter have same name
this refers to current class constructor
this refers to current object
this() calls another constructor in the same class .This technique is called Constructor Chaining
Constructor Syntax
class ClassName {
// Constructor
ClassName() {
// Initialization code
}
// Parameterized Constructor
ClassName(dataType parameter1, dataType parameter2) {
// Initialization code using parameters
}
// Copy Constructor
ClassName(ClassName obj) {
// Initialization code to copy attributes
}
}
Example
class Bike{
Bike(){
System.out.println("Bike is created");
}
public static void main(String[]args){
Bike b = new Bike();
}
}
Restrictions Rule
A constructor cannot be
static(Belong to Class,not Objects) because Constructor is called using Objects.static is called by class
final(Not Inherited)
abstract(must have a body)
and synchronised
Default Constructor (Implicit)
A constructor doesnot have any parameter
A constructor that is automatically provided by Java if no constructor is written in the class.
It initializes object with default values (0, null, false).
Example
class Bike {
int id;
String name;
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Bike b = new Bike(); // calls default constructor
b.display();
}
}
No-Argument Constructor(User-defined)
user-defined empty constructor
A constructor that is manually written by the programmer with no parameters.
It is used to initialize objects with custom default values.
class Bike {
int id;
String name;
// No-argument constructor
Bike() {
id = 101;
name = "Duke";
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Bike b = new Bike();
b.display();
}
}
Parameterised Constructor(Explicit)
A constructor have a any number of parameter
Used to provide different value for objects
Accepts arguments to initialize an object with specific values.
It allows passing values while creating the object.
class Bike {
int id;
String name;
// Constructor only initializes values
Bike(int id, String name) {
this.id = id;
this.name = name;
}
// Method to display bike details
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Bike b = new Bike(111, "Duke");
Bike p = new Bike(122, "Splender");
b.display();
p.display();
}
}
3.Constructor
Allows to create multiple constructors in the same class with different parameter and different data type and different order of parameters
It can be overloaded like Java Methods
You can define all type of Constructors like no-argument and parameterised constructor.
class Bike {
int id;
String name;
// 1. Constructor with no parameters
Bike() {
id = 0;
name = "Null";
}
// 2. Constructor with one parameter
Bike(int id) {
this.id = id;
this.name = "Default Bike";
}
// 3. Constructor with two parameters
Bike(int id, String name) {
this.id = id;
this.name = name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Bike b1 = new Bike(); // calls 1st constructor
Bike b2 = new Bike(111); // calls 2nd constructor
Bike b3 = new Bike(122, "Duke"); // calls 3rd constructor
b1.display();
b2.display();
b3.display();
}
}
Constructor → setup object
Constructor overloading → multiple ways to setup object
Difference between Constructor Loading and Method Overloading
Feature Constructor Overloading Method Overloading
Purpose Initialize objects Perform different tasks
Name Same as class name Any method name
Return type No return type Can have return type
Called when Object creation Method call
Used for Different ways to create object Different ways to use method
Executes automatically? Yes No
Constructor Overloading
Multiple constructors with:
same class name
different parameters
Example
class Bike {
Bike() {
System.out.println("Default constructor");
}
Bike(int id) {
System.out.println("Bike ID: " + id);
}
Bike(int id, String name) {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Bike b1 = new Bike();
Bike b2 = new Bike(101);
Bike b3 = new Bike(102, "Duke");
}
}
Method Overloading
Multiple methods with:
same method name
different parameters
Example
class MathOperation {
void add(int a, int b) {
System.out.println(a + b);
}
void add(int a, int b, int c) {
System.out.println(a + b + c);
}
void add(double a, double b) {
System.out.println(a + b);
}
public static void main(String[] args) {
MathOperation m = new MathOperation();
m.add(10, 20);
m.add(10, 20, 30);
m.add(5.5, 2.5);
}
}
Concept Analogy
Constructor Overloading Different ways to create account
Method Overloading Same calculator doing different additions
Key Similarity
Both use:
Same name + different parameters
4.Inheritance
One Of the object-oriented
Allows one class(subclass or child,Extended ,derived) to acquire or inherit fields and methods of another class(superclass or parent or base)
field => Global variable
static and non-static variables.
establishes a hierarchical relationship between classes.
Code Reusability: Inheritance allows subclasses to reuse code from the parent class.
While Multiple and Hybrid inheritance are not supported using classes, they can be achieved using Interfaces
Polymorphism: Allows the method of a subclass to be invoked, even when using the superclass reference.
Access Control: Use the protected access modifier for superclass members that should be accessible in subclasses but not to the outside world.
Syntax
class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}
Subclass:
A subclass can add its own fields and methods or modify existing ones to extend functionality.
extends Keyword:
The specific keyword used to perform inheritance between classes
Why inheritance is important?
Code reusability
Saves times and memory
Better code organisation
Supports method overriding
5.Types of inheritance
single inheritance
Multilevel inheritance
Hierarchical inheritance
6.Hybrid Inheritance
Combination of multiple inheritance types.
java does not support hybrid inheritance using classes
but it can be achieved using interfaces
8.Method Overloading vs Method Overriding
The differences between Method Overloading and Method Overriding in Java are as follows:
Feature Method Overloading Method Overriding
Definition Defining multiple methods with the same name but different parameters in the same class. Redefining a parent class method in the subclass with the same signature and compatible return type.
Purpose To achieve compile-time polymorphism (static binding). To achieve runtime polymorphism (dynamic binding).
Parameter List Must be different (in number, type, or order). Must be exactly the same as in the parent class.
Return Type Can be same or different, but must not conflict. Must be same or covariant (subtype of parent’s return type).
Inheritance Not required; can occur within the same class. Requires inheritance between superclass and subclass.
Access Modifier Can have any access modifier. Cannot reduce parent method’s access level.
Static / Final Methods Can be overloaded. Cannot be overridden if marked static or final.
Binding Time Compile-time binding. Runtime binding.
Exception Handling Overloaded methods can declare any exceptions. Overridden method cannot throw broader checked exceptions than the parent.
Example void add(int a, int b) and void add(double a, double b) Parent: void show() → Child: void show()
13.what is the static keyword in Java
The static keyword in Java is used for memory management.
A static member belongs to the class, not to the object.
Static variables are shared by all objects of the class.
Static methods can be called without creating an object.
Memory for static members is allocated only once when the class loads.
A static method can access only static data directly.
The main() method is static because JVM calls it without creating an object.
Static blocks are used for static initialization of a class.
Static variables are commonly used for constants and counters.
Example: Math.sqrt() is called using class name because it is static.
14.what is the difference btw static and non-static members ?
Static Members Non-Static Members
Belong to the class Belong to the object
Created only once in memory Created separately for each object
Shared by all objects Each object has its own copy
Accessed using class name Accessed using object name
Can be used without Need object creation
creating object
Static methods can access Non-static methods can access both
only static members directly. static and non-static members
Memory allocated when class Memory allocated when object is
loads created
Example: static int count Example: int age
main() method is static Regular methods are usually non-static
Used for common/shared data Used for object-specific data
15.what is the main() method static in java ?
The main() method in Java is declared as static so JVM can call it without creating an object.
It is the entry point of every Java program.
JVM starts execution from the main() method automatically.
If main() were non-static, JVM would need to create an object first.
Before object creation, JVM needs a starting point, so main() is made static.
Static methods belong to the class, not to objects.
Therefore JVM can directly call ClassName.main().
The syntax of main method is:
public static void main(String[] args)
public allows JVM to access it from anywhere.
static allows execution without object creation.
16.can static methods be overridden .why or why not.
No, static methods cannot be truly overridden in Java.
Static methods belong to the class, not to objects.
Overriding depends on runtime polymorphism and object behavior.
Static methods are resolved at compile time using class name.
If a subclass defines the same static method, it is called method hiding, not overriding.
17.how does inheritance improve code reusability?
Inheritance allows a child class to reuse the properties and methods of a parent class.
Common code is written once in the parent class and shared by subclasses.
It reduces duplicate code and improves maintainability.
Changes made in the parent class automatically affect child classes.
This saves development time and makes programs easier to manage.
18.Design a real-world example using inheritance and polymorphism(Vehicle -> car/Bike)
Real-World Example Using Inheritance and Polymorphism
Vehicle → Parent class
Car and Bike → Child classes
Inheritance → Car and Bike inherit Vehicle properties
Polymorphism → Same method behaves differently
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
// Child class 1
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with key");
}
}
// Child class 2
class Bike extends Vehicle {
@Override
void start() {
System.out.println("Bike starts with self-start");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v;
v = new Car();
v.start();
v = new Bike();
v.start();
}
}
Output
Car starts with key
Bike starts with self-start
How Inheritance Works
Car IS-A Vehicle
Bike IS-A Vehicle
So both inherit:
start()
from Vehicle.
How Polymorphism Works
Same method:
start()
behaves differently:
Car → starts with key
Bike → starts with self-start
This is:
Method Overriding + Runtime Polymorphism
Analogy
Vehicle Type Starting Method
Car Key ignition
Bike Self-start/button
Both are vehicles, but behavior differs.
Inheritance → reuse code
Polymorphism → same method, different behavior
19.How do access modifiers help secure data in large applications.
Access modifiers restrict unauthorized access to variables and methods.
private members protect sensitive data by allowing access only within the same class.
They support encapsulation, which hides internal implementation details.
Controlled access reduces accidental modification of important data.
In large applications, this improves security, maintainability, and reliability of the code.
20.Which Java OOP concepts are most commonly used in real-time projects and why?
Java OOP Concepts Commonly Used in Real-Time Projects
OOP Concept Why it is Used in Real Projects
Encapsulation Protects data and improves security by hiding internal details using private variables and getters/setters.
Inheritance Reuses common code and reduces duplication by allowing child classes to inherit parent class features.
Polymorphism Allows the same method to behave differently, improving flexibility and scalability of applications.
Abstraction Hides complex implementation and shows only essential features, making systems easier to use and maintain.
Classes & Objects Used to model real-world entities like User, Vehicle, Employee, BankAccount, etc.
Method Overloading Allows methods with same name but different parameters for better readability and usability.
Method Overriding Enables runtime polymorphism and customized behavior in subclasses.
Interfaces Used for achieving multiple inheritance and designing loosely coupled systems.
Constructors Initialize objects automatically with required values during object creation.
Access Modifiers Control visibility and improve security in large-scale applications.
🔥 Most Used in Industry
Encapsulation + Inheritance + Polymorphism
These are heavily used in:
Spring Boot
Android apps
Banking systems
E-commerce projects
Enterprise applications
🔹 One-line Memory Trick
Encapsulation protects, Inheritance reuses, Polymorphism gives flexibility.
Top comments (0)