0% found this document useful (0 votes)
3 views53 pages

Introduction To Programming Concepts

The document provides an overview of programming languages, categorizing them into machine-level, assembly-level, and high-level languages, with a focus on the differences between procedure-oriented programming (POP) and object-oriented programming (OOP). It explains key concepts of OOP such as encapsulation, inheritance, and polymorphism, highlighting the advantages of OOP over POP in terms of reusability, data hiding, and reduced complexity. The document also discusses the evolution of OOP and its commonly used languages, emphasizing the importance of objects as fundamental entities in programming.

Uploaded by

s24797487
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)
3 views53 pages

Introduction To Programming Concepts

The document provides an overview of programming languages, categorizing them into machine-level, assembly-level, and high-level languages, with a focus on the differences between procedure-oriented programming (POP) and object-oriented programming (OOP). It explains key concepts of OOP such as encapsulation, inheritance, and polymorphism, highlighting the advantages of OOP over POP in terms of reusability, data hiding, and reduced complexity. The document also discusses the evolution of OOP and its commonly used languages, emphasizing the importance of objects as fundamental entities in programming.

Uploaded by

s24797487
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

Introduction to Programming

Concepts
Introduction to Programming Languages

• Programmers write instructions using different types of programming


languages to perform various computational tasks. These languages
can be divided into:
• Machine-Level Language
• Assembly-Level Language
• High-Level Language
• 1. Machine-Level Language
• It is the lowest-level language understood directly by the computer’s CPU.
• Instructions are written in binary (0s and 1s).
• Each instruction performs a very specific operation such as:
• loading data
• performing arithmetic
• storing data
• jumping to another instruction
• Programs executed by the CPU consist entirely of machine-level
instructions.
• It is very difficult for humans to read, write, and debug.
• 2. Assembly-Level Language
• Assembly language is also a low-level language, but easier than machine
code.
• It uses mnemonics (short codes) like ADD, SUB, MOV instead of 0s and 1s.
• Each assembly instruction corresponds directly (one-to-one) to a machine
instruction.
• Assembly programs must be converted to machine code using an
assembler.
• It is more readable than machine code but still closely tied to hardware.
• 3. High-Level Language
• High-level languages are written in simple English-like statements.
• They are independent of hardware architecture.
• Programmers can focus on logic, not on memory or CPU registers.
• First high-level languages were invented in the 1950s.
• Examples of High-Level Languages:
• Ada, ALGOL, BASIC, COBOL, C, C++, Java, FORTRAN, LISP, Pascal,
Prolog, etc.
• Why they are called high-level?
• They are closer to human languages.
• They hide the complex details of the computer hardware.
• Compared to High-Level Languages:
• Machine language and Assembly language are considered low-level
because they are close to hardware.
Categories of High-Level Languages

• High-level languages are mainly divided into:


• Procedure-Oriented Programming (POP) Languages
• Example: C, Pascal
• Focus on step-by-step procedures (functions).
• Object-Oriented Programming (OOP) Languages
• Example: C++, Java
• Focus on objects, data, and real-world modeling.
Procedure Oriented Programming Language (POP)

• In the procedure-oriented approach, a problem is solved by writing a


sequence of steps such as reading input, performing calculations, and
printing output.
• POP focuses mainly on functions (procedures).
The program is divided into different functions, and the main
program controls the order of calling these functions.

The main program divides the problem into smaller sub-


[Link] sub-task is written as a [Link] functions share
the same global data of the program.
Disadvantages of POP
• Procedure-oriented programming has some major drawbacks:
1. Global Data Access
• All functions can freely access global variables.
• If one function changes a global variable, it can affect the entire program.
• This may lead to errors and makes debugging difficult.
2. Does Not Model Real-World Problems Well
• POP focuses on functions, not data.
• Real-world problems are based on objects (student, employee, bank
account), not just functions.
• So POP is not suitable for real-world modeling.
3. No Data Hiding
• In POP, data is not hidden.
• Any function can access and modify any data.
• This makes the program less secure and harder to maintain.
Data Handling in POP
• POP uses both:
• Global data (shared by all functions)
• Local data (private to each function)

Global data is shared → unsafe


Local data is separate → safe within a function
What is Structured Programming?

• Structured programming means writing code using three main


structures:
• 1. Sequence
• Steps executed one after the other in order.
Example:
• a = 10
• b = 20
• c=a+b
• 2. Selection (Decision-making)
• Choose between two or more paths.
Example:
• if marks >= 40:
• print("Pass")
• else:
• print("Fail")
• 3. Iteration (Loops)
• Repeat something multiple times.
Example:
• for i in range(5):
• print(i)

• All programs can be built using these three structures — this is the
basic idea.
• How Structured Programming Works
• Understand the problem
• Break it into smaller tasks
• Write each task using sequence / selection / loop
• Combine the tasks step-by-step
• Test each part individually
• This is called the top-down approach (from big → small).
Object-Oriented Programming (OOP)
• Object oriented programming as an approach that provides a way of
modularizing programs by creating partitioned memory area for
both data and functions that can be used as templates for creating
copies of such modules on demand.

• Modularizing programs: Breaking a program into smaller,


manageable pieces (modules/objects).
• Partitioned memory: Each object has its own separate memory for
data and functions.
• Templates: Classes act as templates, and we can create multiple
copies (objects) from them.
• In simple words: OOP organizes a program into “objects” that hold
both data and methods, and these objects can communicate and be
reused.
• The diagram
• The diagram has 3 objects: Object A, Object B,
Object C.
• Inside each object:
• Data: The variables or information stored in the
object.
• Functions: The operations or methods that act on
the object’s data.
• Arrows:
• Vertical arrow (inside object): Shows that
functions operate on the object’s own data.
• Horizontal or diagonal arrows (between objects):
Show communication between objects via
functions.
• Example: Object A can call a function of Object B, but it
does not access Object B's data directly.
• Key takeaway:
• Objects encapsulate data (keep it private) and
expose only functions to communicate with other
objects.
• This is called encapsulation
Features of Object-Oriented Programming
1. Emphasis is on doing rather than procedure
1. OOP focuses on what objects do, not the step-by-step procedure.
2. Programs are divided into objects
1. Instead of a long list of procedures, we divide the program into logical objects.
3. Data structures are designed to characterize objects
1. Data in objects represents the real-world attributes of that object.
4. Functions tied to data
1. Methods/functions operate only on the object’s data.
5. Data hiding
1. Internal data cannot be accessed directly from outside; only through methods.
2. This is called encapsulation.
6. Objects communicate through functions
1. Objects do not directly access each other’s data; they interact via methods.
7. Easy addition of new data/functions
•We can extend or add new functionality without breaking existing code.
•This is related to extensibility and inheritance.
8. Bottom-up approach in program design
•First, design individual objects, then combine them to create the system.
•Opposite of top-down, which focuses on the whole program first.
• How the diagram and features relate
• Each object in the diagram contains its own data and functions →
demonstrates encapsulation.
• The arrows between objects → show communication via methods,
not direct data access → demonstrates modularity and data hiding.
• Adding Object C → shows extensibility, we can add objects and
functions without disturbing others.
Comparison between Structured Programming (SP) and Object-Oriented
Programming (OOP):
Feature Structured Programming (SP) Object-Oriented Programming (OOP)

Focuses on breaking a program into functions/procedures Focuses on creating objects that encapsulate data
Definition
that operate on data. and methods together.

Basic Unit Function or procedure. Object (instance of a class).

Data Handling Data is separate from functions; global data is common. Data and behavior are encapsulated within objects.

Bottom-up or modular design; objects and classes


Design Approach Top-down design; problem is divided into smaller functions.
model real-world entities.
High; classes and objects can be reused across
Reusability Limited; functions can be reused but less modular.
programs.
Strong; data and methods are bundled; access
Encapsulation Minimal; no strict data hiding.
modifiers control visibility.
Abstraction Achieved through functions. Achieved through classes and interfaces.

Supported; allows new classes to inherit properties


Inheritance Not supported.
and methods from existing classes.

Supported; same method can behave differently in


Polymorphism Not supported.
different contexts.
Harder for large programs due to scattered data and Easier; modular objects and encapsulation simplify
Code Maintenance
functions. maintenance.
Examples C, Fortran, Pascal C++, Java, Python
• Procedure-oriented language: You give the computer a list of steps to
follow, like a recipe. Functions do the work, and data is separate.
Example: C.
• Structured language: Same as procedure-oriented, but you organize
your steps neatly so it’s easier to read and understand. Example:
Pascal, C.
• Object-oriented language: You think of the program as things
(objects) in real life. Each thing has its own data and actions. Example:
Java, C++.
• Functional language: You just give the computer a function (like a
formula) that always gives the same answer for the same input.
Example: Haskell.
• Logic language: You tell the computer facts and rules, and it figures
out the solution on its own. Example: Prolog.
What is OOP?

• OOP is a way of programming where everything is treated as


objects—things that represent real-world entities. Each object has:
• Data (called attributes or properties)
• Behavior (called methods or functions)

• Instead of just giving the computer step-by-step instructions, you


organize your program around objects that can interact with each
other.
Evolution of OOPS
Key Concepts of OOP

• Class: A blueprint for creating objects. Example: Car class.


• Object: A specific instance of a class. Example: myCar is an object of Car.
• Encapsulation: Keeping data and methods together, hiding internal details.
• Inheritance: One class can get properties and methods from another.
Example: SportsCar inherits from Car.
• Polymorphism: The same method can behave differently for different
objects. Example: start() works differently for Car and Bike.
• Abstraction: Hiding unnecessary details and showing only essential
features.
Advantages of Object-Oriented Programming
(OOP)
• 1. Reusability
• In OOP, the functions and classes written by a user can be reused by other
users without any modification. This reduces the effort of writing the same
code again and again.
• 2. Inheritance
• Through inheritance, we can eliminate redundant code. A new class can
use the features of an existing class, which helps in extending the use of
already written code.
• 3. Data Hiding
• OOP allows the programmer to hide data and functions inside a class from
other classes. This protects the data and helps in building secure programs.
• 4. Reduced Complexity of a Problem
• In OOP, a problem is viewed as a collection of objects. Each object is
responsible for a specific task. The problem is solved by interacting
these objects, which reduces the overall complexity of program
design.
• 5. Easy to Maintain and Upgrade
• OOP makes it easy to maintain and modify existing code. New objects
can be created by making small changes to existing ones. This helps in
managing software complexity easily.
• 6. Message Passing
• Objects in OOP communicate with each other by sending messages
(method calls). This technique makes it easier to interface with
external systems and improves the structure of programs.
Commonly used Object-Oriented
Programming (OOP) languages
• Java – Very popular for apps, web development, and enterprise software.
• C++ – Used for system software, games, and high-performance
applications.
• Python – Simple syntax, widely used for web apps, data science, AI, and
automation.
• C# – Mainly for Windows applications and game development (with Unity).
• Ruby – Known for web development (Ruby on Rails framework).
• Swift – Used for iOS and macOS app development.
• Objective-C – Older iOS/macOS apps, still in use in some legacy projects.
• PHP (with OOP features) – Web development using object-oriented style.
Key principles of Object-Oriented Programming (OOP)
• Encapsulation – Keep the data (attributes) and the code that works on it (methods)
together in one unit (object), and hide unnecessary details from outside.
• Example: A Car object hides how the engine works; you just call start().
• Abstraction – Show only the important features and hide the complex inner details.
• Example: You know a TV has a changeChannel() method, but you don’t need to know how it works
inside.
• Inheritance – A new class can take properties and methods from an existing class, saving
time and making code reusable.
• Example: SportsCar inherits from Car and can add extra features.
• Polymorphism – The same action can behave differently depending on the object it is
acting on.
• Example: draw() can draw a Circle or a Rectangle differently.
• Classes and Objects –
• Class: A blueprint (like a recipe).
• Object: A specific thing made from that blueprint (like a cake made from the recipe).
BASIC CONCEPTS OF OBJECTS ORIENTED
PROGRAMMING
• OBJECTS
• Objects are the basic run-time entities in an object-oriented system. They may represent a
person, a place, a bank account, a table of data or any item that the program must handle.
• The fundamental idea behind object oriented approach is to combine both data and function into
a single unit and these units are called objects.
• The term objects means a combination of data and program that represent some real word entity.
For example: consider an example named Amit; Amit is 25 years old and his salary is 2500.
• The Amit may be represented in a computer program as an object. The data part of the object
would be (name: Amit, age: 25, salary: 2500)
• The program part of the object may be collection of programs (retrieve of data, change age,
change of salary). In general even any user –defined type-such as employee may be used.
• In the Amit object the name, age and salary are called attributes of the object
• CLASS: A group of objects that share common properties for data part and some program part are
collectively called as class.
• A class is like a blueprint or template for creating objects.
• It defines common properties (data) and behaviors (methods/functions) that all objects of that
class will have.
• You don’t work with the class directly; you create objects (instances) from it.

• DATA ABSTRACTION : Abstraction refers to the act of representing essential features without
including the back ground details or explanations. Classes use the concept of abstraction and are
defined as size, width and cost and functions to operate on the attributes.
• DATA ENCAPSALATION : The wrapping up of data and function into a single unit (called class) is
known as encapsulation. The data is not accessible to the outside world and only those functions
which are wrapped in the class can access it. These functions provide the interface between the
objects data and the program.
• INHERITANCE : Inheritance is the process by which objects of one class acquire the properties of
another class. In the concept of inheritance provides the idea of reusablity. This mean that we can
add additional features to an existing class with out modifying it. This is possible by desining a
new class will have the combined features of both the classe
• POLYMORPHISIM: Polymorphism means the ability to take more than one
form.
• An operation may exhibit different instance.
• The behaviour depends upon the type of data used in the operation. A
language feature that allows a function or operator to be given more than
one definition. The types of the arguments with which the function or
operator is called determines which definition will be used.
• Overloading may be operator overloading or function overloading. It is
able to express the operation of addition by a single operater say ‘+’. When
this is possible you use the expression x + y to denote the sum of x and y,
for many different types of x and y; integers , float and complex no. You can
even define the + operation for two strings to mean the concatenation of
the strings.
Simple Explanation to understand
• Class – Think of a blueprint or recipe.
• Example: Car class is a blueprint. It tells you every car will have a color, model, and engine,
and can start or stop.
• Object – A real thing made from the blueprint.
• Example: myCar is a red Honda. dadCar is a blue Toyota. Both are objects of the Car class.
• Inheritance – One thing can take features from another thing.
• Example: SportsCar inherits from Car. It has all features of a car, plus can go super fast.
• Polymorphism – Same action, different behavior depending on the object.
• Example: start() for a Car just starts the engine. start() for a Bike starts differently, but you
just call start() for both.
• Encapsulation – Hiding internal details so others don’t mess with it.
• Example: You can start the car using a button, but you don’t need to know how the engine
works inside.
• Abstraction – Show only important details, hide complexity.
• Example: When using a TV, you see buttons or a remote. You don’t need to know how
electricity flows or how the circuits work.
• Dynamic Binding:
• The linking of the function call to the code happens at run-time, not
while writing the program.
• Which code runs depends on the actual object (dynamic type) at that
moment.
• This is usually used with polymorphism, where the same function
name can do different things for different objects.
• Message Passing:
In OOP, programs are made of objects. These objects don’t just work
alone—they talk to each other.
• When one object wants another object to do something, it sends a
message.
• A message is basically a request to run a function or method in the
receiving object.
• Sending a message usually involves:
• Name of the object you’re sending to
• Name of the function (the action you want)
• Data or information to send, if needed
Components of an OOP Environment
• An OOP environment generally consists of three major components
that help convert your source code into an executable program and
run it efficiently.
1) Compiler
2) Interpreter
3) Runtime System
1. Compiler
• What it does:
• Converts high-level OOP code (Java, C++, etc.) into machine code or
intermediate code.
• Checks for syntax errors, type errors, and class structure errors.
• Performs optimizations to improve performance.
• In OOP specifically:
• Checks class definitions, inheritance rules, method overriding, and
polymorphism structure.
• Ensures that encapsulation and access modifiers are used correctly.
• Example:
• Java compiler (javac) converts .java files into bytecode (.class files).
2. Interpreter
• What it does:
• Executes the code line-by-line or bytecode instruction by instruction.
• Does not convert the whole program at once.
• Useful for dynamic languages or debugging.
• In OOP:
• Helps in interactive execution of OOP statements.
• Useful for languages like Python, where classes and objects can be created
interactively.
• Example:
• Java uses the JVM interpreter to read and interpret bytecode.
• Python uses its Python interpreter directly.
3. Runtime System
• What it does:
• Provides services needed during program execution.
• Manages memory, objects, method calls, garbage collection, and exceptions.
• In OOP specifically:
• Creates objects using constructors.
• Allocates memory for:
• objects
• instance variables
• inheritance-linked objects
• Manages dynamic binding (deciding which method to call at runtime).
• Example:
• Java Runtime Environment (JRE):
• JVM
• class loader
• garbage collector
• runtime libraries
Components of an OOP Environment (with Java
Illustration)

• An OOP environment mainly includes:


• Compiler
• Interpreter
• Runtime System
• Let’s understand each with a Java program flow diagram + example.
• 1. Compiler (javac)
• Role:
• Converts Java source code (.java) → bytecode (.class).
• Checks for:
• Class definitions
• Syntax errors
• Data types
• Inheritance rules
• Method signatures
Flow:
• 2. Interpreter (JVM Interpreter)
• Role:
• Executes bytecode instruction-by-instruction.
• Converts bytecode → machine instructions.
• Enables portability (Write Once, Run Anywhere).
• Flow after compilation:

• Example:
The line

• is interpreted by the JVM, converted to machine-level instructions,


and printed on screen.
• 3. Runtime System (JRE)
• Role:
• Manages everything needed while the program is running:
• Object creation
• Memory allocation (Heap/Stack)
• Garbage collection
• Exception handling
• Dynamic method binding
Runtime Illustration in Java:
During execution of this code:

The runtime system does the following:


[Link] the Demo class into memory.
[Link] the object obj on heap.
[Link] show() method via dynamic binding.
[Link] memory and removes unused objects.
Java Program Structure
class className
{
Data Members
User_defined Methods
public static void main(String[] args)
{
Block of Statements
}
}
Example:
class Hello
{
public static void main(String[] args)
{
[Link]("Hello Java");
[Link]("My First Java Program");
}
}

To compile: javac [Link]


To execute: java Hello
Understanding Hello java program
1. class keyword is used to declare a class in java.
2. public keyword is an access modifier which represents visibility, it means it is visible
to all.
3. static is a keyword, if we declare any method as static, it is known as static method.
The core advantage of static method is that there is no need to create object to invoke
the static method. The main method is executed by the JVM, so it doesn't require to
create object to invoke the main method. So it saves memory.
4. void is the return type of the method, it means it doesn't return any value.
5. main represents startup of the program.
6. String[] args is used for command line argument.
7. [Link]() is used to print statements

You might also like