Java Programming with Data Structures - Full Notes
Object-Oriented Programming (OOP)
Definition: OOP is a programming style based on the concept of 'objects' which contain data and methods.
Features:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Diagram:
[Class] -> [Object] -> [Methods, Data]
Code:
class Car {
void drive() { [Link]("Driving"); }
Classes and Objects
Definition: A class is a blueprint, and objects are instances created from it.
Diagram:
[Class] ---> [Object1, Object2]
Code:
class Student {
int id;
void display() { [Link](id); }
Student s1 = new Student();
Java Programming with Data Structures - Full Notes
Inheritance
Definition: Allows one class to inherit fields and methods from another class.
Types: Single, Multilevel, Hierarchical
Diagram:
A -> B -> C
Code:
class A { void show(){} }
class B extends A { }
Polymorphism
Definition: Ability of an object to take many forms.
Types: Compile-time (Overloading), Runtime (Overriding)
Code:
class Calc {
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; }
Abstraction & Encapsulation
Abstraction: Hiding details, showing essential features.
Encapsulation: Binding data and methods together.
Code:
class Account {
private int balance;
Java Programming with Data Structures - Full Notes
void deposit(int amt) { balance += amt; }
Java Features
Simple, Object-Oriented, Platform Independent, Secure, Robust, Multithreaded, High Performance
Diagram:
[Source Code] -> [Compiler] -> [Bytecode] -> [JVM]
Java Program Structure
Structure: Class -> main() method -> Statements
Code:
class Hello {
public static void main(String[] args) {
[Link]("Hello");
Exception Handling
Definition: Mechanism to handle runtime errors.
Code:
try {
int x = 10/0;
} catch (Exception e) {
[Link](e);
} finally {
[Link]("Done");
}
Java Programming with Data Structures - Full Notes
Stack (Array)
Definition: LIFO structure for storing elements.
Operations: push(), pop(), peek()
Code:
int top = -1;
int[] stack = new int[100];
stack[++top] = 5;
Binary Tree Traversal
Types:
Inorder: Left -> Root -> Right
Preorder: Root -> Left -> Right
Postorder: Left -> Right -> Root
Code:
void inorder(Node root) {
if(root != null) {
inorder([Link]);
[Link]([Link]);
inorder([Link]);