Java
JDK - (Java Development Kit) set of development tools and libraries used to create Java programs (Used to develop applications) (JRE + debugger, javac)
JRE - Java Runtime Environment, provides an environment to run Java programs on the system. In Includes Standard libraries and JVM. (To run java applications) ) (rt.jar)
JVM - Virtual Machine for executing Java programs (Executes Java bytecode) (Bytecode is platform independent) (ClassLoader, Garbage Collector)
Class Loader (loads .class file) Byte Code Verifier (Ensures security before execution) Interpreter (Execute the byte code) (Interpreter + JIT compiler to execute bytecode for optimal performance) Execution: Makes calls to underlying hardware
JVM is responsible to running the java program line by line (also known as interpreter)
Loading - Linking - Initialization
Loading - Class loader reads the .class file, generate binary data and save it in method area.
Loaded class and its immediate parent class in method area checks whether .class file is related to class or interface modifier, variables, and method information
after loading ".class" file, JVM creates an object of type class to represent this file in the heap memory. This is predefined in java.lang, use getClass() to access the variable name, methods etc.
Java Identifiers
53 identifiers in Java 50 keywords 3 literals
# Primitive Data Types
boolean
char
byte
short
int
long
float
double
# Non primitive Data Types
Defined using classes
String
Array
Class
Interface
Object
Object is the topmost data type
NP.toString(); # normally memory location
NP.equals();
String str1;
str1.equals(str2);
Importing packages
import java.util.*
Class
package root // root folder path
class Car { // dynamic class
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
public class Geeks {
public static void main(String[] args) {
Car myCar = new Car("Honda", 2021);
}
}
class Animal {
protected void speak() {
System.out.println("Grrr")
}
}
class Cow extends Animal {
@override
void speak() {
System.out.println("Moooo");
}
}
Package-private -> within the same folder
// interface
public interface Card {
void makeTransaction();
void readCard();
}
public CreditCard implements Card {
@Override
void makeTransaction() {}
@Override
void readCard() {}
}
public DebitCard implements Card {
@Override
void makeTransaction() {}
@Override
void readCard() {}
}
// folder
public class ShopKeeper {
private Card newCard;
public ShopKeeper(Card customerCard) {
this.newCard = customerCard;
}
}
public abstract class Animal {
void eat(){
System.out.println("Gnom Gnom");
}
abstract void makeSound();
}
public class Dog extends Animal {
@Override
void makeSound() {
}
}
Multiple interfaces can be inherited
List -> interface (ArrayList)
ArrayList implements List
Map -> interface (HashMap, TreeMap)
LinkedList -> class (interface List)
Stack -> class (interface List)
Set -> interface (HashSet, TreeSet, LinkedHashSet)
Queue -> interface (LinkedList, PriorityQueue)
Dequeue -> interface
List.add(idx, obj);
List.add(obj);
List.get(idx);
List.set(idx, obj);
List.remove(idx);
List.remove(obj);
List.contains;
List.isEmpty();
List.clear();
List.indexOf;
List<Integer> list = new ArrayList<>();
collections.sort(list, (a,b) -> b - a);
HashMap.put(); get(); remove(); containsKey(); containsVal(); clear();
HashSet.add(); contains();
Queue.add(); remove(); peak();
Stack.pop(); push(); peak(); empty();
Dequeue addLast(); addList(); removeFirst(); removeLast();
// generics
public class Node<T> {
T val;
public Node(T val) {
this.val = val;
}
}
Scanner scanner = new Scanner(System.in);
int val = scanner.next() // string
scanner.nextInt();
scanner.hasNext();
OOPS
OOPs is a programming model which revolves around the concept of Objects
OOPS -> helps understand software easily
OOPS -> readablility
OOPS -> easily managed
Imperative Programming Paradigm: Focuses on how to execute program logic and defines control flow as statements 1) Procedural Programming Paradigm 2) OOP
Declarative - focuses on what to execute and defines program logic.
Access specifiers - controls the accessibility of the entities like classes, methods etc.
Inheritance - may take time to navigate through different classes
Interface - Special type of class which contains methods, but not their definition. You cannot instantiate an interface
Static Polymorphism - Object is linked with the respective function or operator based on the values during compile time. (Method overloading, operator overloading)
Dynamic Polymorphism - Actual implementation of the function is determined during the runtime (Method overriding)
Data abstraction is accomplished through abstract classes and methods
Abstract Class - special class containing abstract methods. They are not implemented by declared. Subclasses must declare them and implement them
| Feature | Abstract Class | Interface |
|---|---|---|
| Purpose | Partial abstraction (base class with some implementation) | Full abstraction (just a contract for behavior) |
| Methods | Can have abstract (no body) and concrete (with body) methods | Only abstract methods (until Java 8; default/static allowed later) |
| Fields | Can have fields/variables (with access modifiers) | Only public static final constants (no instance variables) |
| Constructors | ✅ Yes, can have constructors | ❌ No constructors |
| Access Modifiers | Can use private, protected, public |
All methods are implicitly public |
| Inheritance | Supports single inheritance only | Supports multiple inheritance (a class can implement many interfaces) |
| Usage | When there is shared code or common base behavior | When you just want to enforce a contract (no implementation needed) |
| Example Use Case | Animal base class: eat(), sleep() (shared logic) |
Interface Flyable or Swimmable: classes promise to define behavior |
| Encapsulation - Binding data members and methods of a program without revealing unnecessary details. |
Data hiding - hiding unwanted information Data binding - binding data members and methods together as a whole.
Abstraction is the method of hiding unnecessary details from the necessary ones.
Types 1) Default constructor 2) Parametrized constructor 3) Copy constructor