0% found this document useful (0 votes)
2 views17 pages

Programming Fundamental

The document provides an educational overview of programming concepts, focusing on functional and object-oriented programming principles. It includes beginner-friendly resources, explanations of key programming terms such as variables, compile time, and runtime, and details on Java's inheritance, encapsulation, polymorphism, and abstraction. Additionally, it covers C programming fundamentals and includes practical examples and analogies to simplify complex topics.

Uploaded by

pmail1acc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Programming Fundamental

The document provides an educational overview of programming concepts, focusing on functional and object-oriented programming principles. It includes beginner-friendly resources, explanations of key programming terms such as variables, compile time, and runtime, and details on Java's inheritance, encapsulation, polymorphism, and abstraction. Additionally, it covers C programming fundamentals and includes practical examples and analogies to simplify complex topics.

Uploaded by

pmail1acc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming and Fundamental Concepts: Functional

and OOP principles, and more.

Kirti Vardhan

2025
Disclaimer
This content is for educational purposes only. No warranty is provided. Use at
your own risk. Always verify with official sources before applying.

Top 10 Beginner-Friendly Programming Resources

# Source What It Covers


1 geeksforgeeks.org ELI5 tutorials on Java, C, OOP, memory,
compilation
2 w3schools.com/java Java basics, syntax, examples, flow
3 cs50.harvard.edu/x Harvard’s intro C course, logic, compila-
tion
4 javatpoint.com Java tutorials: OOP, abstraction, encap-
sulation
5 tutorialspoint.com Quick references for Java/C with exam-
ples
6 learn-c.org Hands-on C code runner for beginners
7 javacodegeeks.com Java concepts, real-world patterns, inter-
view prep
8 educative.io Visual Java guides with interactivity
9 programiz.com Java/C/Python: I/O, types, variables ex-
plained simply
10 stackoverflow.com Community-driven bug fixes and ELI5 an-
swers

What is a Computer Program?

Imagine a computer is like a robot.


A program is a set of instructions you give to the robot to make it do something:
• ”Turn on the light”
• ”Add these two numbers”
• ”Show a picture”
Programs are written in special languages like Python, Java, or C — kind of like
English, but for computers.

What is a Variable?
Think of a variable as a box with a label on it.
• You can put stuff inside (like a number or a name).
• You can take it out later when you need it.
Example:
age = 5

This says: ”Make a box called age and put the number 5 inside.”

1
What is Compile Time?

• This is the preparation stage — like baking a cake.


• The computer checks your instructions for mistakes before running the
program.
• Languages like C, C++, and Java do this — they compile code into
something the computer can understand.
What does ”compile” mean?
• You write your code in a human-friendly language like C or Java.
• A special tool called a compiler acts like a translator.
• It converts your code into machine code (0s and 1s) that the CPU can
understand.
• This translation happens before the program runs — making it faster and
more efficient later.
Example in C:
# include < stdio .h >

int main () {
printf ( " Hello , world ! " ) ;
return 0;
}

Note on Java:
• Java compiles your code into bytecode, not full machine code.
• Then the Java Virtual Machine (JVM) runs it — so Java is partially
compiled, partially interpreted.

What is Platform Independence?

• Platform Independence means a program can run on any operating system


or device, without rewriting the code.
• Java code is compiled into bytecode.
• This bytecode runs on the Java Virtual Machine (JVM), which exists for
each platform.
• So the same ‘.class‘ file can run anywhere — that’s platform independence!

What is Runtime?
• This is when the program is actually doing the task.
• You hit ”run” and the program executes.
• Problems that happen during execution are called runtime errors.

2
What is Strongly Typed?

• You must tell the computer what kind of box (variable) you’re using.
• If you mix types, it gives an error.
int age = 5;
age = " hello " ; // Error ! Can ’t put a word in a number box

What is Weakly Typed?

• The computer is more relaxed.


• You can mix numbers and words, and it tries to figure it out.
var age = 5;
age = " hello "; // No problem !

Summary Table

Concept Simple Explanation Real-World Analogy


Program Set of robot steps A recipe
Variable A labeled value box Drawer with something inside
Compile Time Checked and prepared Baking the cake
Runtime Code is running Eating the cake
Strongly Typed Strict type rules Label must match box contents
Weakly Typed Flexible box rules Anything in any drawer

3
What Happens When You Compile a Program in C vs Java? (ELI5)

C Language: Native Compilation


Step 1: Write a .c file
# include < stdio .h >

int main () {
printf ( " Hello ! " ) ;
return 0;
}

Step 2: Compile it with GCC


gcc hello . c -o hello

What happens?
• Compiler checks for errors
• Translates to machine code (0s and 1s)
• Saves a platform-specific executable
Java Language: Platform-Independent Compilation
Step 1: Write a .java file
public class Hello {
public static void main ( String [] args ) {
System . out . println ( " Hello ! " ) ;
}
}

Step 2: Compile it with javac


javac Hello . java

What happens?
• Compiler checks the code
• Converts to bytecode
• Output is Hello.class file
• JVM runs it — platform independent

Comparison Table – C vs Java Compilation

Stage C Output Java Output Platform Target


Source Code .c .java Human-readable
Compiled File .exe / no ext .class Native vs JVM
Runs On CPU directly Java Virtual Machine Platform dependent vs independent
Real-World Analogy:
C is like baking a cake in your home oven — it only works in that oven.
Java is like baking a cake mix that works in any oven — thanks to the
JVM.

4
Core Concepts in C Programming (ELI5 Style)

C gives you superpowers — but with great power comes great responsibility! Let’s
break down the most important concepts every C programmer should know:
1. Basics
• Variables & Data Types: Boxes with labels. Types decide what goes
inside: int, float, char, etc.
• Control Flow: Use if, else, while, for to make decisions or repeat steps.
• Functions: Reusable code blocks. Think: mini-programs inside your pro-
gram.
2. Storage and Memory
• Arrays: Multiple boxes of the same type, stored side by side.
• Strings: Arrays of char ending in ’\0’. No real string type!
• Pointers: Variables that store memory addresses. They ”point” to other
boxes.
• Dynamic Memory: You control allocation with malloc, and cleanup with
free.
• Structs: Group different types together — like a contact’s name, phone, and
email.
3. Compilation Model
• Source Code → Preprocessing → Compilation → Linking → Exe-
cutable
• Preprocessor handles macros and includes.
• Compiler turns code into object files.
• Linker stitches everything together.
4. Preprocessor Directives
• #include — bring in code from other files (like stdio.h).
• #define — create macros (text shortcuts).
• #ifdef, #ifndef — compile only if certain macros exist.
• #pragma — special compiler instructions.
5. Scope and Types
• static: Variable keeps its value across calls.
• extern: Variable lives in another file.
• const vs #define: const is type-safe; #define is not.
• enum: Named numbers — useful for states, options, etc.
6. Low-Level Tools
• Bitwise Operators: Manipulate bits: &, |, ^, <<, >>.
• Type Casting: Convert one type into another — use with care.
• Undefined Behavior: Dangerous mistakes like using memory wrong. Can
crash or corrupt!
7. Input and Output
• printf(), scanf() — in stdio.h, used for basic console I/O.
• File operations — open, read, write, and close files using C functions.
8. Common Pitfalls
• Forgetting ’\0’ in strings
• Memory leaks (no free)
• Pointer misuse → segmentation fault
5
Java Inheritance: Reusing Code with Vehicles (Part 1)

What is Inheritance? Inheritance lets you define a general class (like Vehicle)
and create specific types (like Car, Truck) that automatically gain its functionality.
Why Use It?
• Reuse common code (e.g., engine logic).
• Create clear relationships between general and specific types.
• Enable method overriding and polymorphism.
Base Class and Subclass Example
class Vehicle {
void startEngine () {
System . out . println ( " Engine started " ) ;
}
}

class Car extends Vehicle {


void playMusic () {
System . out . println ( " Playing music ... " ) ;
}
}

Explanation:
• Vehicle = base class.
• Car = subclass that inherits from Vehicle.
• Car doesn’t need to rewrite startEngine().
Demo:
Car myCar = new Car () ;
myCar . startEngine () ; // Inherited
myCar . playMusic () ; // Own method

6
Java Inheritance: Overriding Behavior (Part 2)

What is Method Overriding? A subclass can change how an inherited method


works by redefining it using @Override.
Example: Override fuelType()
class Vehicle {
void fuelType () {
System . out . println ( " Generic fuel " ) ;
}
}

class ElectricCar extends Vehicle {


@Override
void fuelType () {
System . out . println ( " Electric battery " ) ;
}
}

Real-World Analogy:
All vehicles can start and stop — that’s built into the Vehicle class.
A Car might have music, a Truck hauls goods, and an ElectricCar
redefines how it fuels up.
Key Inheritance Terms:
• extends — used to inherit from a class.
• @Override — marks a method that redefines a parent method.
• super — lets you call the parent class version.

Java Encapsulation: Protecting Data (Part 1)

What is Encapsulation? Encapsulation means wrapping data and behav-


ior (methods) together inside a class and hiding internal details from outside
interference.
Real-World Analogy:
A car has an accelerator and brakes — but you don’t need to know
how they’re wired inside. You just use them. The complexity is hidden
inside the vehicle.
Why Use Encapsulation?
• Prevents unauthorized access to internal data.
• Protects object integrity.
• Allows controlled access using getters and setters.
How It Works:
• Use private variables — not directly accessible.
• Provide public getter/setter methods to read or update them safely.

7
Java Encapsulation: Code Example (Part 2)

Encapsulation in Action – Vehicle Speed Example


class Vehicle {
private int speed ; // Hidden from outside

// Getter
public int getSpeed () {
return speed ;
}

// Setter with validation


public void setSpeed ( int newSpeed ) {
if ( newSpeed >= 0) {
speed = newSpeed ;
}
}
}

public class Main {


public static void main ( String [] args ) {
Vehicle car = new Vehicle () ;
car . setSpeed (60) ; // Valid set
System . out . println ( car . getSpeed () ) ; // 60
}
}

Explanation:
• speed is private — can’t access it directly (e.g., car.speed = 100; is illegal).
• getSpeed() and setSpeed() provide safe access.
• Validation logic can be added inside the setter to protect data.
Moral of the Code:
Encapsulation keeps important values safe and only allows changes in
controlled ways.

8
Java Polymorphism: One Interface

What is Polymorphism? ”Poly” means many, ”morph” means form. Polymor-


phism means the same method name can do different things depending on the
object.
Types of Polymorphism:
• Compile-time (Method Overloading) — Same method name with dif-
ferent arguments.
• Runtime (Method Overriding) — Subclass changes how a parent method
behaves.
Real-World Analogy:
You press the “start” button in any vehicle. For a petrol car, it starts
the engine; For an electric car, it powers the battery. Same action,
different results based on the vehicle type.
Why Use Polymorphism?
• Simplifies code: Treat different types in a uniform way.
• Reduces duplication.
• Makes systems more extensible and flexible.

9
Java Polymorphism: Code Example with Vehicles (Part 2)

Example: Different Vehicles, Same Interface


class Vehicle {
void move () {
System . out . println ( " Vehicle is moving " ) ;
}
}

class Car extends Vehicle {


@Override
void move () {
System . out . println ( " Car drives on roads " ) ;
}
}

class Boat extends Vehicle {


@Override
void move () {
System . out . println ( " Boat sails on water " ) ;
}
}

public class Main {


public static void main ( String [] args ) {
Vehicle v1 = new Car () ;
Vehicle v2 = new Boat () ;

v1 . move () ; // Car drives on roads


v2 . move () ; // Boat sails on water
}
}

Key Point:
• The reference type is Vehicle, but the actual object determines which move()
is called.
• This is runtime polymorphism.
Moral of the Code:
Same interface. Different behavior. That’s polymorphism in action.

10
Java Abstraction: Hide Details
What is Abstraction? Abstraction means showing only the necessary details
to the outside world and hiding the internal complexity.
Real-World Analogy:
You drive a car using a steering wheel and pedals. You don’t see how
the engine or brakes work — that’s abstraction!
Why Use Abstraction?
• Focus on what an object does, not how.
• Improve code readability and modularity.
• Reduce duplication and make future changes easier.
Tools in Java for Abstraction:
• abstract class — has both defined and undefined methods.
• interface — defines what should be done, not how.

Java Abstraction: Code Example (Part 2)

Example: Abstract Vehicle Blueprint


abstract class Vehicle {
abstract void move () ; // No body ( undefined )

void start () {
System . out . println ( " Vehicle is starting ... " ) ;
}
}

class Bike extends Vehicle {


void move () {
System . out . println ( " Bike rolls on two wheels " ) ;
}
}

Explanation:
• Vehicle is an abstract class — can’t be instantiated.
• Bike must implement move().
• start() method is inherited directly.
Interface Version:
interface Engine {
void start () ; // implicitly abstract
}

class JetEngine implements Engine {


public void start () {
System . out . println ( " Jet engine ignited ! " ) ;
}
}

11
Access Modifiers in Java: Who Can See What?
Access modifiers control visibility.
Modifier Same Class Same Package Subclass Other Packages
public Yes Yes Yes Yes
protected Yes Yes Yes No (unless subclass)
default Yes Yes No No
private Yes No No No
Quick Tip:
Use private for encapsulation, public for shared APIs, and protected
when working with inheritance.

12
Java OOP Practice: Test What You’ve Learned
Q1: What will this print?
class Vehicle {
void move () {
System . out . println ( " Vehicle moves " ) ;
}
}

class Truck extends Vehicle {


void move () {
System . out . println ( " Truck carries cargo " ) ;
}
}

public class Demo {


public static void main ( String [] args ) {
Vehicle v = new Truck () ;
v . move () ;
}
}

Q2: Which principle does this demonstrate?

Q3: Predict the output and access error, if any:


class Bus {
private int passengers = 50;

public int getPassengers () {


return passengers ;
}
}

public class Terminal {


public static void main ( String [] args ) {
Bus b = new Bus () ;
System . out . println ( b . passengers ) ;
}
}

Q4: Fix it using encapsulation.

13
Conditionals: Making Decisions in Code

What are conditionals? They let your program make choices — like a fork in
the road.
Real-world analogy:
”If it’s raining, take an umbrella. Otherwise, wear sunglasses.”
• if = check a condition
• else = do this if the condition is false
• else if = check another condition
Example:
if temp > 30:
print ( " It ’s hot ! " )
else :
print ( " Ok ! " )

Program Flow: How Instructions Run in Order

Program flow = The order in which your instructions run.


Think of it like a recipe:
• Step 1 → Step 2 → (if something happens, go to Step 4) → Done!
Tools that control flow:
• if/else — decisions
• for/while — loops
• return/break/continue — jump, exit, or skip
Flow Diagrams help visualize this — like a roadmap of your code.

Recursion: When a Function Calls Itself


What is Recursion? A function solves a small piece of the problem — then calls
itself to solve the rest.
Real-world analogy:
You open a box, and inside it is a smaller box. . . and another. . . until
you find a note that says “done!”
Key parts:
• Base case: When to stop.
• Recursive case: When to keep going.
Example: Factorial
def factorial ( n ) :
if n == 1:
return 1
return n * factorial ( n - 1)

14
Termination: How and When Programs End

Termination means your program is done — either successfully or with an error.


Types of Termination:
• Normal termination: Code runs to the end, or calls return or exit().
• Abnormal termination: Program crashes (e.g., infinite loop, unhandled
error).
Important Note: Every recursive or loop-based function must have a way to
terminate — or it could run forever!

File I/O: How Code Talks to Files

File I/O = Input and Output with files. You can save data or read it back
— like a digital notebook.
Basic steps:
• Open a file — for reading or writing
• Read/write data
• Close the file
Example:
with open ( " data . txt " , " w " ) as file :
file . write ( " Hello ! " )

Modes:
• "r" = read
• "w" = write (overwrite)
• "a" = append

What if something goes wrong? Instead of crashing, you can handle it — that’s what
exceptions are for.
Real-world analogy:
You try to open a locked door. Instead of panicking, you check your keys or
call for help.

15
Exception Handling: Preparing for Mistakes

What if something goes wrong? Instead of crashing, you can handle it —


that’s what exceptions are for.
Real-world analogy:
You try to open a locked door. Instead of panicking, you check your
keys or call for help.
Code structure:
try {
int num = Integer . parseInt ( " abc " ) ;
System . out . println ( num ) ;
} catch ( N u m b e r F o r m a t E x c e p t i o n e ) {
System . out . println ( " That wasn ’t a number ! " ) ;
}

Why use exception handling?


• Prevents your program from crashing.
• Lets you show helpful error messages.
• Handles unexpected events like wrong input or missing files.

16

You might also like