Basic
Basic
“The Java Virtual Machine (JVM) is a part of the Java Runtime Environment that is responsible for
executing Java bytecode. It provides a runtime environment that abstracts the underlying
operating system and hardware, making Java platform-independent.”
🔁 How It Works
1. Java code ( .java ) is compiled by the Java compiler ( javac ) into bytecode ( .class files).
3. Execution Engine:
allowing the same program to run on different operating systems as long as a JVM is available."
JVM Java Virtual Machine Runs Java bytecode Execution engine only
JRE Java Runtime Provides environment to run JVM + libraries + other files
Environment Java programs
JDK Java Development Kit Provides tools to develop Java JRE + development tools ( javac ,
applications debugger, etc.)
Contains:
JVM
Core Java libraries
Other runtime resources
Contains:
JRE
Compiler ( javac )
Debugger
Javadoc
to run Java applications. The JDK includes both the JRE and development tools like the compiler
and debugger, which are needed to write and build Java applications."
🧾 Syntax in Java
class Parent {
void display() {
[Link]("Parent display");
}
void show() {
[Link]("Child show");
}
Usage:
© 2025 CloudTech. All rights reserved.
Child obj = new Child();
[Link](); // Inherited from Parent
Multiple Inheritance (with ❌ No Java doesn’t support this directly (to avoid
classes) ambiguity)
class inherits from a Vehicle class, it automatically gets access to the common vehicle behaviors,
unit, and restricting direct access to some of the object’s components. It’s used to protect the
🎯 Purpose of Encapsulation
Modularity – You can change internal implementation without affecting external code.
2. Provide public getter and setter methods – To read and modify the values safely.
🧾 Example
public class Employee {
// Public getter
return name;
}
// Public setter
Usage:
📌 Key Points
© 2025 CloudTech. All rights reserved.
You control how data is accessed or modified.
You can add validation inside setters.
It’s a core pillar of OOP in Java (along with inheritance, abstraction, and polymorphism).
unit and restricting direct access to some of the object’s fields. It is achieved by making fields
private and exposing them through public getters and setters. This helps protect the internal
name can behave differently depending on the object that invokes it.”
class Calculator {
int add(int a, int b) {
return a + b;
}
class Animal {
void sound() {
[Link]("Dog barks");
}
}
Usage:
the context. It comes in two forms: compile-time polymorphism using method overloading, and
runtime polymorphism using method overriding. This enables flexibility and extensibility in
object-oriented programming.”
Definition Defining multiple methods with the Redefining a method in a child class that is
same name but different parameter already defined in the parent class, with the
lists within the same class. same method signature.
Method Method name must be the same, but the Method name, return type, and parameters
Signature number or type of parameters must must be exactly the same.
differ.
Return Type Can differ (though it’s not common Must be the same as the parent method (same
practice to overload methods only by signature).
return type).
Inheritance No inheritance required (methods are in Must involve inheritance, where the child class
the same class). overrides the parent class method.
Use Case Used when you want the same method to Used when you want to provide a specific
handle different types or numbers of implementation of a method in a subclass.
inputs.
Example Method name is the same, but the Method in child class provides a new definition
parameters differ (e.g., add(int, int) for an inherited method from the parent class.
and add(double, double) ).
🧾 Examples
Method Overloading
return a + b;
}
}
Here, both add methods have the same name but different parameter types. This is an example of
compile-time polymorphism.
Method Overriding
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
@Override
void sound() {
[Link]("Dog barks");
}
}
Here, Dog class overrides the sound method from the Animal class. This is an example of runtime
polymorphism, where the actual method that is called depends on the object type ( Dog in this case).
happens when a subclass provides a specific implementation of a method already defined in the
parent class, with the same method signature. Overriding is resolved at runtime and is a key
feature of runtime polymorphism.”
i.e., whether it can be accessed within the same class, package, or from other classes.”
2. protected
4. private
Scope: Accessible from anywhere (within the same class, different class, same package, and different
package).
Usage: When you want to make a class or class member accessible from any other class or package.
access them.
class Animal {
protected void makeSound() {
[Link]("Sound");
}
void display() {
Example: A protected method in a parent class can be accessed by subclasses (even if they are in
different packages).
Usage: When no access modifier is specified. The default access level is package-private, meaning it is
class MyClass {
Example: default members are not accessible outside the package, even in subclasses.
class MyClass {
private int data;
Example: A private method or variable can only be accessed inside the class where it is declared.
🔀 Summary of Scopes
Access Modifier Same Class Same Package Subclass Different Package
private Yes No No No
private . public allows access from anywhere, protected allows access within the same package
and by subclasses, default (no modifier) allows access within the same package only, and private
Automatic Call: The constructor is called automatically when an object is instantiated using the new
keyword.
Purpose: Primarily used to initialize the object’s instance variables when an object is created.
2. Parameterized Constructor
🔹 1. Default Constructor
Definition: A constructor with no parameters. If no constructor is explicitly defined, Java provides a
default constructor that initializes the object with default values (like null , 0 , false ).
Usage: Used when no special initialization is required at the time of object creation.
class Car {
String model;
int year;
// Default constructor
public Car() {
model = "Unknown";
year = 2023;
🔹 2. Parameterized Constructor
Definition: A constructor that takes parameters to initialize an object with specific values when it is
created.
Usage: Used when you want to initialize an object with specific values at the time of creation.
class Car {
String model;
int year;
// Parameterized constructor
[Link] = year;
}
}
🧾 Constructor Overloading
“Java allows constructor overloading, which means you can have multiple constructors with the
same name but different parameter lists in a class.”
class Car {
String model;
// Default constructor
public Car() {
model = "Unknown";
year = 2023;
// Parameterized constructor
[Link] = model;
[Link] = year;
parameters) and parameterized constructors (which allow passing values at the time of object
creation). Constructors can also be overloaded to provide multiple ways of initializing objects.”
compares object references (memory addresses), while equals() compares the actual content or
values of objects.”
Comparison Compares memory references Compares actual values (content or state) of the
Type (addresses). objects.
Works With Primitive types and object Objects (particularly for comparing content).
references.
Default For objects, == checks if both The default equals() method (in Object class)
Behavior references point to the same object checks for reference equality, but can be overridden
in memory. to compare object content.
Overriding Cannot be overridden (as it is a part Can be overridden to compare custom objects based
of the primitive comparison). on their content.
🧾 Examples
Using == (Reference Comparison)
String str1 = new String("Java");
String str2 = new String("Java");
str1 and str2 are two different objects in memory, even though they contain the same value.
equals() compares the actual content of the objects, and in this case, the content of str1 and str2
is the same.
For objects, it compares whether two references point to the same object in memory.
equals() :
Used for object comparison to check if two objects are logically equivalent (i.e., their
Can be overridden in classes to customize the comparison logic. For example, in the String
memory location. It works with both primitive types and object references. On the other hand,
equals() is used for content comparison, specifically to check if two objects have the same values.
In most classes, equals() should be overridden to perform content comparison, such as in the
String class."
classes that belong to the class itself rather than to any specific object of the class.”
Definition: A variable declared as static is shared by all instances of the class, rather than
counter).
Example:
class Counter {
void increment() {
}
}
[Link]();
[Link]([Link]); // Output: 2
}
Explanation: Both c1 and c2 share the same count variable, and it reflects the total
2. Static Methods:
Definition: A method declared as static can be called without creating an instance of the
class. Static methods can only access other static members (variables, methods) of the class.
Usage: When you want a method to be common to all instances of the class or when you don’t
need to access any instance-specific data.
Example:
class MathUtility {
static int add(int a, int b) {
3. Static Blocks:
Definition: A static block is used for initialization that should only be done once, when the
Example:
class Database {
static {
[Link]("Connecting to database...");
}
}
new Database(); // Static block will run once when the class is loaded
}
}
Explanation: The static block runs once when the Database class is loaded into memory,
Definition: A static class is a nested class that does not require an instance of the outer class
to be created.
Usage: When you want to define a nested class that doesn’t depend on the outer class’s instance
variables or methods.
Example:
class Outer {
}
}
Explanation: You can create an instance of Inner without needing an instance of Outer .
Shared across all instances: Static variables and methods are shared by all instances of a class.
Accessed without creating objects: You can access static members without creating an instance of the
class.
Limited access in static methods: Static methods can only directly access other static members of the
Used for constants: Static variables are often used for constants (i.e., values that should not change and
are shared by all instances).
that belong to the class rather than to individual objects. Static members are shared across all
instances of the class. A static variable or method can be accessed directly using the class name,
and they are typically used for values that are common to all instances or for one-time setup in
static blocks.”
Keyword Declared with the static keyword. Not declared with static .
Association Belongs to the class, not to any instance of Belongs to instances of the class
the class. (objects).
Accessing Can be called without creating an object Can only be called with an instance
of the class. of the class.
Access to Instance Cannot directly access non-static instance Can access both static and non-
Variables variables or methods. static variables and methods.
Memory Location Static methods are stored in the method Non-static methods are stored in the
area (class level). heap (object level).
Use Case Used for general operations or actions that Used for operations that depend on
don’t depend on object state. the state of an object (instance).
Inheritance Can be inherited by subclasses but cannot be Can be inherited and overridden in
overridden (they are hidden if redefined). subclasses.
🧾 Examples
Static Method
class Calculator {
}
}
[Link](result); // Output: 8
}
}
Explanation: add() is a static method. It can be called using the class name Calculator without
Non-static Method
class Calculator {
[Link](result); // Output: 15
(i.e., an object).
🔑 Key Differences
© 2025 CloudTech. All rights reserved.
Static Methods:
Cannot access instance variables or instance methods directly (they must access only static
Non-static Methods:
the class. They can only directly access static members of the class. Non-static methods, on the
other hand, belong to instances of the class and can be called only through objects. Non-static
methods can access both static and non-static members of the class.”
for other classes. Abstract classes can have both abstract methods (without implementation) and
concrete methods (with implementation). The purpose of an abstract class is to provide a common
base for other classes while allowing specific implementation details to be defined in the
subclasses.”
abstract methods.
2. Abstract Methods:
Subclasses of the abstract class are required to implement these abstract methods unless they
3. Concrete Methods:
An abstract class can also have methods that are fully implemented (i.e., concrete methods).
4. Constructors:
Abstract classes can have constructors, but they cannot be directly called to create an object.
5. Fields:
Abstract classes can have instance variables and constants, just like regular classes.
6. Inheritance:
A concrete class that extends an abstract class must implement all the abstract methods of the
// Concrete method
void sleep() {
void sound() {
Explanation:
The Animal class is abstract, with one abstract method ( sound() ) and one concrete method
( sleep() ).
The Dog class extends Animal and provides an implementation for the sound() method.
subclasses.
Concrete Methods: Fully implemented methods in the abstract class that can be inherited by
subclasses.
Abstract Class Cannot Be Instantiated: You cannot create an object of an abstract class directly. It
must be subclassed.
Purpose: Abstract classes are used when you want to define common functionality for subclasses but
subclassed. It can contain both abstract methods (without implementation) and concrete methods
classes provide a common base and can contain instance variables, constructors, and methods that
are shared by subclasses.”
methods, default methods, static methods, and constants (variables). Interfaces cannot have
instance variables or constructors. They are used to represent a contract or a set of methods that a
class must implement, ensuring that the class adheres to a particular behavior or specification.”
By default, all methods in an interface are abstract (prior to Java 8). These methods have no
implementation and must be implemented by any class that implements the interface.
2. Constants:
All variables declared in an interface are public , static , and final by default. They are
3. Multiple Inheritance:
Java allows a class to implement multiple interfaces, solving the problem of multiple
4. Default Methods:
From Java 8 onwards, interfaces can have default methods that provide a default
implementation. These methods can be overridden by implementing classes but are optional to
5. Static Methods:
Java 8 also introduced static methods in interfaces. These methods can be called on the interface
6. No Constructor:
7. Implemented by Classes:
A class that implements an interface must provide the implementation for all of its abstract
🧾 Example of an Interface
interface Animal {
void sound();
Explanation:
The Animal interface has one abstract method ( sound() ) and one default method ( sleep() ).
The Dog class implements the Animal interface and provides the implementation for the
sound() method.
class must implement. It specifies what actions the implementing class should perform, but not how.
No Constructors: Interfaces cannot have constructors because they are not meant to be instantiated
directly.
Default Methods: With Java 8 and above, interfaces can have default methods with an implementation.
Multiple Inheritance: A class can implement multiple interfaces, overcoming the limitations of single
implement. Interfaces can contain abstract methods, constants, default methods, and static
methods. A class that implements an interface must provide implementations for all its abstract
methods. Interfaces enable multiple inheritance in Java, which allows a class to implement more
the interface. The class is then required to provide implementations for all the abstract methods
declared in the interface (unless the class is abstract). An interface provides a contract, and the
An interface can contain abstract methods (methods without body), default methods (with
A class that implements an interface uses the implements keyword, followed by the interface
name.
The implementing class must provide concrete implementations for all abstract methods defined
in the interface.
If the class is not abstract, it must provide implementations for all abstract methods. Otherwise,
If the interface has default methods, the implementing class can either override them or use
interface Animal {
void sound();
}
}
Explanation:
The Animal interface defines an abstract method sound() and a default method sleep() .
The Dog class implements the Animal interface and provides an implementation for the
sound() method. The sleep() method is inherited as-is from the interface.
The implementing class must provide concrete implementations for all abstract methods declared
3. Default Methods:
If the interface contains default methods, the implementing class has the option to override them.
4. Multiple Interfaces:
A class can implement multiple interfaces, which allows for multiple inheritance (a class can
name. The class must then provide concrete implementations for all the abstract methods defined
in the interface. If the interface contains default methods, the class can choose to override them or
use the default behavior. A class can implement multiple interfaces, allowing it to inherit
behaviors from more than one source.”
Keyword Declared with the abstract Declared with the interface keyword.
keyword.
Methods Can have both abstract methods Can have abstract methods (without
(without implementation) and implementation), default methods (with
concrete methods (with implementation), and static methods.
implementation).
Implementation A subclass must implement abstract A class must implement all abstract
methods (if any), but it can inherit methods from the interface. If the interface
both abstract and concrete methods. has default methods, the class can use them
or override them.
State (Instance Can have instance variables (fields) All variables in an interface are implicitly
Variables) that are non-static and non-final. public, static, and final (constants).
Access Modifiers Methods and variables can have All methods are implicitly public , and
various access modifiers like variables are public , static , and final
public , private , protected , or by default.
default .
Multiple A class can inherit from only one A class can implement multiple interfaces,
Inheritance abstract class (single inheritance). allowing multiple inheritance of behavior.
Use Case Used when you want to share Used when you want to represent a contract
common code (implementation) or a set of behaviors that can be
among classes. implemented by different classes.
Method Visibility Can have methods with different Methods are public by default.
access levels ( public , private ,
etc.).
In JDK 8, interfaces can now have default methods (with implementation) and static
methods.
Default methods allow interfaces to provide default behavior, meaning classes that
implement the interface don’t necessarily need to implement the method (but can override it if
needed).
Static methods in an interface can be called using the interface name, but they are not inherited
by implementing classes.
interface Animal {
void sound(); // Abstract method
2. Abstract Methods:
Abstract classes can have both abstract and non-abstract (concrete) methods.
Interfaces (prior to JDK 8) could only have abstract methods, but now they can have default
3. State (Variables):
4. Multiple Inheritance:
Abstract classes support single inheritance. A class can only inherit from one abstract class.
Interfaces support multiple inheritance. A class can implement multiple interfaces, which
// Abstract method
// Concrete method
void sleep() {
void sound() {
}
}
Explanation:
Animal is an abstract class with one abstract method ( sound() ) and one concrete method
( sleep() ).
The Dog class extends Animal and provides an implementation for sound() .
// Abstract method
void sound();
// Default method
// Static method
}
}
Explanation:
Animal is an interface with an abstract method ( sound() ), a default method ( sleep() ), and a
The Dog class implements the Animal interface and provides an implementation for sound() .
abstract and concrete methods, instance variables, and constructors, while an interface can only
have abstract methods (until Java 8 introduced default and static methods). A class can implement
multiple interfaces but can inherit from only one abstract class. Abstract classes are used when
you want to share code among related classes, while interfaces are used to define a contract that