Java is a high-level, object-oriented, platform-independent programming language used for developing web, desktop, mobile, and automation applications.

  • Platform independent
  • Object-oriented
  • Secure
  • Robust
  • Multithreaded
  • Portable

Variables are containers used to store data values.

Example:

int age = 25;

Two types:

  • Primitive → int, char, boolean
  • Non-primitive → String, Array, Class
PrimitiveNon-Primitive
Stores valueStores reference
Fixed sizeDynamic size

Converting one datatype into another datatype.

Example:

double d = 10.5;
int i = (int)d;

Operators perform operations on variables.

Examples:

  • Arithmetic (+)
  • Logical (&&)
  • Relational (>)
==.equals()
Compares memory referenceCompares values
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Encapsulation means wrapping data and methods together in a single class and restricting direct access using private variables.

Inheritance allows one class to acquire properties and methods of another class using extends keyword.

Polymorphism means one method can perform different actions based on the object.
Types:

  • Overloading
  • Overriding

Abstraction means hiding implementation details and showing only required functionality.

AbstractionEncapsulation
Hides implementationHides data

Method overloading means creating multiple methods with same name but different parameters.

Method overriding means redefining parent class method in child class with same signature.

OverloadingOverriding
Same classParent-child class
Different parametersSame parameters
Compile-time polymorphismRuntime polymorphism

An interface is a blueprint containing abstract methods that must be implemented by classes.

An abstract class is a class declared using abstract keyword. It can contain abstract and non-abstract methods

InterfaceAbstract Class
Supports multiple inheritancePartial abstraction
Mostly abstract methodsCan contain concrete methods

Constructor initializes object.

A constructor is a special method used to initialize objects.

Creating multiple constructors with different parameters is called constructor overloading

ConstructorMethod
Initializes objectPerforms operations
Same name as classAny valid name

Refers to current class object.

super refers to parent class object or constructor.

static belongs to class instead of object.

Used to:

  • Prevent inheritance
  • Prevent overriding
  • Declare constants

No, static and final methods cannot be overridden.

String is a sequence of characters used to store text.

StringStringBufferStringBuilder
ImmutableMutableMutable
SlowThread-safeFaster

For security, thread safety, and performance.

Java stores string literals in common memory called String Pool.

equals()equalsIgnoreCase()
Case-sensitiveIgnores case

Using StringBuilder:

String rev = new StringBuilder(str).reverse().toString();

Duplicates can be removed using loops, Set collection, or HashSet.

Using length() method.

str.length();

An array is a collection of elements of the same datatype stored in a single variable. It is used to store multiple values together using index positions.

ArrayList is a class in Java Collection Framework used to store dynamic collections of elements. Unlike arrays, its size can grow or shrink automatically.

ArrayArrayList
Fixed sizeDynamic size
Stores primitivesStores objects

Stores key-value pairs

HashMapHashtable
Not synchronizedSynchronized
HashSetTreeSet
UnorderedSorted

Used to traverse collection elements.

IteratorListIterator
Forward onlyForward & backward

Framework containing classes/interfaces for storing data.

ComparableComparator
Natural sortingCustom sorting
ArrayListLinkedList
Faster retrievalFaster insertion/deletion

Mechanism to handle runtime errors.

CheckedUnchecked
Compile-timeRuntime

Used to handle exceptions.

Always executes whether exception occurs or not.

throwthrows
Throws exceptionDeclares exception

Yes.

Occurs when accessing null object.

Passing exception from one method to another.

Reading and writing files using Java classes.

Using:

  • BufferedReader
  • Scanner
  • FileReader

Using FileWriter or BufferedWriter.

Used to read text efficiently.

Used to read/write byte data.

Executing multiple threads simultaneously.

Smallest unit of execution.

ProcessThread
Independent executionPart of process

Using:

  • Thread class
  • Runnable interface

Prevents multiple threads accessing shared resources simultaneously.

Pauses thread execution temporarily.

start()run()
Creates new threadExecutes normally

A constructor in Java is a special block of code used to initialize an object when it is created.

  • The constructor name must be the same as the class name
  • It does not have any return type (not even void)
  • It is automatically called when an object is created
  • It is mainly used to assign initial values to object variables

If a class does not have any constructor, the Java compiler automatically provides a default constructor

class Student {
    int id;

    Student() {   // Constructor
        id = 100;
    }
}
Key Points to Remember (for interview):
  • Constructor ≠ Method
  • Used for object initialization
  • Can be overloaded (multiple constructors with different parameters)
  • Types:
    • Default constructor
    • Parameterized constructor

Inheritance in Java is an OOP concept where one class (child/subclass) acquires the properties and behaviors (variables and methods) of another class (parent/superclass).

It helps in code reusability and reduces duplication.

Key Points (Interview Focus):
  • Achieved using the extends keyword
  • Promotes code reuse
  • Supports method overriding
  • Java supports:
    • Single inheritance
    • Multilevel inheritance
    • Hierarchical inheritance
  • Java does not support multiple inheritance with classes (but supports it using interfaces)
class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}

Here, Dog inherits the eat() method from Animal.

Polymorphism in Java means “many forms”—it allows a single method or object to behave in different ways depending on the context.

Types of Polymorphism:

1. Compile-time Polymorphism (Method Overloading)

  • Same method name
  • Different parameters
  • Decided at compile time
class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
}

2. Runtime Polymorphism (Method Overriding)

  • Same method name
  • Same parameters
  • Different implementation in child class
  • Decided at runtime
class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

Key Points (Interview Focus):

  • Achieves flexibility and reusability
  • Overloading → compile-time
  • Overriding → runtime
  • Runtime polymorphism uses inheritance