Java C Hash Python
1. Principles of Object-Oriented Programming
Section titled “1. Principles of Object-Oriented Programming”2.1 Object-Oriented Programming (OOP)
Section titled “2.1 Object-Oriented Programming (OOP)”OOP is a programming paradigm based on the concept of objects which contain data (attributes) and methods (functions). It focuses on modularity, reusability, and abstraction.
2.2 Core Principles of OOP
Section titled “2.2 Core Principles of OOP”a) Encapsulation
- Bundling of data and methods operating on that data into a single unit (class).
- Restricts direct access to internal data; access controlled via getters/setters.
Example (Java):
class Student { private int rollNo; public void setRollNo(int r){ rollNo = r; } public int getRollNo(){ return rollNo; }}b) Inheritance
- Mechanism to derive new classes (subclasses) from existing ones (superclasses).
- Promotes code reusability and hierarchical classification.
Example (C#):
class Animal { public void eat() { Console.WriteLine("Eating"); }}class Dog : Animal { public void bark() { Console.WriteLine("Barking"); }}c) Polymorphism
- Ability of different classes to respond differently to the same function call.
- Compile-time polymorphism: method overloading.
- Runtime polymorphism: method overriding via inheritance.
Example (Python):
class Shape: def area(self): passclass Circle(Shape): def area(self): return "Area of Circle"d) Abstraction
- Hiding implementation details and showing only essential features.
- Achieved using abstract classes and interfaces.
Example (Java):
abstract class Shape { abstract void draw();}class Circle extends Shape { void draw(){ System.out.println("Drawing Circle"); }}1.3 Advantages of OOP
Section titled “1.3 Advantages of OOP”- Modularity and maintainability
- Code reusability via inheritance
- Flexibility and scalability
- Enhanced data security via encapsulation
1.4 Key Concepts
Section titled “1.4 Key Concepts”- Class: Blueprint for objects.
- Object: Instance of a class.
- Constructor: Initializes object state.
- Message Passing: Communication between objects via methods.
2. Basics of Java / C# / Python Language
Section titled “2. Basics of Java / C# / Python Language”2.1 Introduction
Section titled “2.1 Introduction”- Java: Object-oriented, platform-independent (JVM-based).
- C#: Object-oriented, component-based, runs on .NET Framework / .NET Core.
- Python: Interpreted, dynamically typed, easy syntax, supports multiple paradigms.
2.2 Program Structure
Section titled “2.2 Program Structure”Java
class Main { public static void main(String[] args) { System.out.println("Hello World"); }}C#
using System;class Program { static void Main() { Console.WriteLine("Hello World"); }}Python
print("Hello World")2.3 Data Types
Section titled “2.3 Data Types”| Type Category | Java | C# | Python |
|---|---|---|---|
| Integer | int, long | int, long | int |
| Floating | float, double | float, double | float |
| Character | char | char | str (single char) |
| Boolean | boolean | bool | bool |
| String | String | string | str |
2.4 Variables & Constants
Section titled “2.4 Variables & Constants”-
Declaration
- Java:
int x = 10; - C#:
int x = 10; - Python:
x = 10
- Java:
-
Constants
- Java:
final int X = 10; - C#:
const int X = 10; - Python: conventionally uppercase:
X = 10
- Java:
2.5 Operators
Section titled “2.5 Operators”- Arithmetic:
+ - * / % - Relational:
== != > < >= <= - Logical:
&& || !(Java/C#),and or not(Python) - Assignment:
= += -= *= /= - Increment/Decrement:
++ --(Java/C# only)
2.6 Control Statements
Section titled “2.6 Control Statements”If-Else
if(x > 0) System.out.println("Positive");else System.out.println("Negative");if(x > 0) Console.WriteLine("Positive");else Console.WriteLine("Negative");if x > 0: print("Positive")else: print("Negative")Looping
for(int i=0;i<5;i++) System.out.println(i);for(int i=0;i<5;i++) Console.WriteLine(i);for i in range(5): print(i)2.7 Functions / Methods
Section titled “2.7 Functions / Methods”- Java
int add(int a, int b){ return a + b; }- C#
int Add(int a, int b){ return a + b; }- Python
def add(a, b): return a + b2.8 Input / Output
Section titled “2.8 Input / Output”- Java:
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); - C#:
int x = Convert.ToInt32(Console.ReadLine()); - Python:
x = int(input())
2.9 Comments
Section titled “2.9 Comments”- Single-line:
//(Java/C#),#(Python) - Multi-line:
/*...*/(Java/C#),'''...'''or"""..."""(Python)
2.10 Exception Handling
Section titled “2.10 Exception Handling”Java
try { int a=5/0; } catch(Exception e){ System.out.println(e); }C#
try { int a=5/0; } catch(Exception e){ Console.WriteLine(e.Message); }Python
try: a=5/0except Exception as e: print(e)3. Working with User Interfaces
Section titled “3. Working with User Interfaces”3.1 Introduction
Section titled “3.1 Introduction”User Interface (UI) enables interaction between user and application. In Java, C#, and Python, UI can be Graphical (GUI) or Web-based.
3.2 Java – Swing & JavaFX
Section titled “3.2 Java – Swing & JavaFX”a) Swing
- Part of
javax.swingpackage. - Lightweight, platform-independent GUI toolkit.
- Components:
JFrame,JButton,JLabel,JTextField.
Example
import javax.swing.*;class MyFrame { public static void main(String[] args){ JFrame f = new JFrame("Demo"); JButton b = new JButton("Click"); b.setBounds(100,100,80,30); f.add(b); f.setSize(300,200); f.setLayout(null); f.setVisible(true); }}b) JavaFX
- Modern GUI framework replacing Swing.
- Uses FXML and CSS for UI design.
Example
import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.stage.Stage;public class Main extends Application { public void start(Stage stage){ Button b = new Button("Click"); Scene scene = new Scene(b, 200, 100); stage.setScene(scene); stage.show(); }}3.3 C# – Windows Forms & WPF
Section titled “3.3 C# – Windows Forms & WPF”a) Windows Forms
- Found in
System.Windows.Formsnamespace. - Event-driven, drag-and-drop support in Visual Studio.
Example
using System;using System.Windows.Forms;class MyForm : Form { Button b; public MyForm(){ b = new Button(); b.Text = "Click"; b.Click += (s,e)=> MessageBox.Show("Hello"); Controls.Add(b); } [STAThread] static void Main(){ Application.Run(new MyForm()); }}b) WPF (Windows Presentation Foundation)
- Uses XAML for UI, supports data binding and animation.
Example
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button Content="Click Me" Width="100" Height="40"/></Window>3.4 Python – Tkinter & PyQt
Section titled “3.4 Python – Tkinter & PyQt”a) Tkinter
- Built-in Python GUI library.
- Uses widgets like
Button,Label,Entry,Frame.
Example
import tkinter as tkwin = tk.Tk()btn = tk.Button(win, text="Click", command=lambda: print("Hello"))btn.pack()win.mainloop()b) PyQt / PySide
- Advanced GUI library with Qt framework.
- Supports UI Designer tool.
Example
from PyQt5.QtWidgets import QApplication, QPushButtonapp = QApplication([])btn = QPushButton('Click')btn.show()app.exec_()3.5 Common Concepts
Section titled “3.5 Common Concepts”- Event Handling: Actions triggered by user inputs (click, keypress).
- Layout Managers: Arrange UI components (e.g.,
FlowLayout,GridLayout,pack()etc.). - MVC Pattern: Separates logic (Model), interface (View), and control (Controller).
- Threading in UI: Long-running tasks should run on separate threads to keep UI responsive.
4. Introduction to Threads in Java
Section titled “4. Introduction to Threads in Java”4.1 Definition
Section titled “4.1 Definition”A thread is the smallest unit of a process that can run independently.
Multithreading allows concurrent execution of two or more threads to maximize CPU utilization.
4.2 Thread Creation Methods
Section titled “4.2 Thread Creation Methods”a) Extending Thread class
class MyThread extends Thread { public void run(){ for(int i=0;i<5;i++) System.out.println("Child Thread"); } public static void main(String[] args){ MyThread t = new MyThread(); t.start(); // starts new thread for(int i=0;i<5;i++) System.out.println("Main Thread"); }}b) Implementing Runnable interface
class MyRunnable implements Runnable { public void run(){ for(int i=0;i<5;i++) System.out.println("Child Thread"); } public static void main(String[] args){ Thread t = new Thread(new MyRunnable()); t.start(); for(int i=0;i<5;i++) System.out.println("Main Thread"); }}4.3 Thread Life Cycle
Section titled “4.3 Thread Life Cycle”- New – Thread created but not started (
new Thread()). - Runnable – Ready to run after
start(). - Running – Scheduler picks the thread to execute
run(). - Blocked/Waiting – Waiting for resource or synchronization.
- Terminated – After execution completes.
4.4 Thread Methods
Section titled “4.4 Thread Methods”| Method | Description |
|---|---|
start() | Starts execution of thread (calls run() internally) |
run() | Defines thread’s task |
sleep(ms) | Pauses thread for specified time |
join() | Waits for thread to finish |
isAlive() | Checks if thread is active |
setPriority(int p) | Sets priority (1–10) |
getName() / setName() | Gets or sets thread name |
4.5 Thread Priority
Section titled “4.5 Thread Priority”- Range:
1 (MIN_PRIORITY)to `10 (MAX_PRIORITY) - Default:
5 (NORM_PRIORITY) - Used by scheduler for execution order, but not guaranteed.
4.6 Synchronization
Section titled “4.6 Synchronization”- Prevents multiple threads from accessing shared resources simultaneously.
Example
class Counter { int count = 0; synchronized void increment(){ count++; }}synchronizedensures thread-safe access.
4.7 Inter-thread Communication
Section titled “4.7 Inter-thread Communication”- Methods:
wait(),notify(),notifyAll()(used within synchronized blocks). - Used for coordination between threads.
4.8. Daemon Threads
Section titled “4.8. Daemon Threads”- Background threads that terminate when all user threads finish.
Thread t = new Thread(...);t.setDaemon(true);t.start();4.9 Advantages of Multithreading
Section titled “4.9 Advantages of Multithreading”- Better CPU utilization
- Faster program execution
- Responsive UI
- Simultaneous background processing
5. Database Connectivity using Java / C# / Python
Section titled “5. Database Connectivity using Java / C# / Python”5.1 Introduction
Section titled “5.1 Introduction”Database connectivity enables interaction between programs and databases (MySQL, SQL Server, Oracle, etc.) through APIs or libraries for CRUD operations (Create, Read, Update, Delete).
5.2 Java – JDBC (Java Database Connectivity)
Section titled “5.2 Java – JDBC (Java Database Connectivity)”a) Steps
-
Import package –
import java.sql.*; -
Load driver –
Class.forName("com.mysql.cj.jdbc.Driver"); -
Establish connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","password"); -
Create statement
Statement st = con.createStatement(); -
Execute query
ResultSet rs = st.executeQuery("SELECT * FROM student"); -
Process results
while(rs.next())System.out.println(rs.getInt(1)+" "+rs.getString(2)); -
Close connection
con.close();
b) PreparedStatement Example
PreparedStatement ps = con.prepareStatement("INSERT INTO student VALUES(?,?)");ps.setInt(1,101);ps.setString(2,"Gaurav");ps.executeUpdate();5.3 C# – ADO.NET
Section titled “5.3 C# – ADO.NET”a) Steps
-
Import namespace –
using System.Data.SqlClient; -
Establish connection
SqlConnection con = new SqlConnection("Data Source=SERVER;Initial Catalog=TestDB;Integrated Security=True");con.Open(); -
Execute command
SqlCommand cmd = new SqlCommand("SELECT * FROM student", con);SqlDataReader dr = cmd.ExecuteReader();while(dr.Read())Console.WriteLine(dr[0] + " " + dr[1]); -
Close connection
con.Close();
b) Using Parameters
SqlCommand cmd = new SqlCommand("INSERT INTO student VALUES(@id,@name)", con);cmd.Parameters.AddWithValue("@id", 101);cmd.Parameters.AddWithValue("@name", "Gaurav");cmd.ExecuteNonQuery();5.4 Python – DB API (Using MySQL Connector / SQLite3)
Section titled “5.4 Python – DB API (Using MySQL Connector / SQLite3)”a) MySQL Connector Example
import mysql.connectorcon = mysql.connector.connect(host="localhost", user="root", password="1234", database="testdb")cur = con.cursor()cur.execute("SELECT * FROM student")for row in cur: print(row)con.close()b) Parameterized Query
cur.execute("INSERT INTO student VALUES(%s, %s)", (101, "Gaurav"))con.commit()c) SQLite Example
import sqlite3con = sqlite3.connect("test.db")cur = con.cursor()cur.execute("CREATE TABLE IF NOT EXISTS student(id INT, name TEXT)")cur.execute("INSERT INTO student VALUES(1, 'Gaurav')")con.commit()for row in cur.execute("SELECT * FROM student"): print(row)con.close()5.5 Common Concepts
Section titled “5.5 Common Concepts”- Connection Object: Represents DB connection.
- Statement / Cursor / Command: Executes SQL queries.
- ResultSet / Reader: Holds query results.
- Prepared/Parameterized Statements: Prevent SQL injection.
- Transaction Control:
commit(),rollback()for consistency.