0% found this document useful (0 votes)
13 views19 pages

OOP-CG UNIT 1 NOTES

The document outlines the syllabus for a course on Object-Oriented Programming (OOP) and Computer Graphics, focusing on programming paradigms, particularly OOP concepts such as classes, objects, inheritance, and polymorphism. It emphasizes the advantages of OOP, including modularity, reusability, and maintainability, and discusses Java as a primary language for OOP. Additionally, it covers the structure of Java programs, data types, and the differences between primitive and reference types.

Uploaded by

rutujapmedhe
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)
13 views19 pages

OOP-CG UNIT 1 NOTES

The document outlines the syllabus for a course on Object-Oriented Programming (OOP) and Computer Graphics, focusing on programming paradigms, particularly OOP concepts such as classes, objects, inheritance, and polymorphism. It emphasizes the advantages of OOP, including modularity, reusability, and maintainability, and discusses Java as a primary language for OOP. Additionally, it covers the structure of Java programs, data types, and the differences between primitive and reference types.

Uploaded by

rutujapmedhe
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/ 19

OOP and CG www.sppuengineers.

site

PCC-202-COM : Object Oriented programming and Computer Graphics


(2024 Pattern) (Semester - III)

Unit 1 Introduction to OOP Concepts and Control Structure Marks


Programming paradigms - Introduction to programming paradigms,
Introduction to four main Programming paradigms- procedural, object oriented,
functional, and logic & rule based. Need of object oriented programming,
Fundamentals of object-oriented programming: Namespaces, objects, classes,
data members, methods, messages, data encapsulation, data abstraction and
information hiding, inheritance, polymorphism. Benefits of OOP, Java as object
oriented programming language.
Overview of Java Language: simple Java program structure: documentation
14
section, package statement, import statements, class definition, main method
class. Implementing Java Program, JVM, Data types, Primitive Types vs. Reference
type, floating point numbers, operators and expressions, Java Class Libraries,
Typical Java Development Environment, and Memory Concepts.
Control Statements: Selection Statements: if, if-else, nested if-else, Iteration
Statements: do, while, for, for-each statement, break, and continue statements
Case Study: Analyze the object -oriented features in Java with other object-
oriented programming languages.

Part 1: Programming Paradigms

Before we can appreciate what Object-Oriented Programming (OOP) is, we need to


understand what a "programming paradigm" is. Think of it as a style of building,
a blueprint, or a philosophy for writing computer programs. It's not a specific
language, but a way of thinking about and structuring your code. There are several
major paradigms, each with its own strengths.

1.1 The Four Main Programming Paradigms

1. Procedural Programming: The "To-Do List" Paradigm

• The Big Idea: This is one of the earliest and most straightforward paradigms.
You write a program as a series of sequential, step-by-step instructions, like a
recipe or a to-do list. The focus is on procedures (also known as functions or

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 1


OOP and CG www.sppuengineers.site

subroutines) that perform specific tasks on data. Data and the procedures that
operate on it are kept separate.
• Analogy: You're building a bookshelf. The procedural approach is a list of
instructions: "1. Get wood. 2. Cut wood to size. 3. Get screws. 4. Screw pieces
together. 5. Paint the shelf." The data (wood, screws) and the actions (cut, screw,
paint) are separate concepts.
• Characteristics:
o Top-down design.
o Code is organized into functions.
o Data is often stored in global variables, accessible to all functions.
o Focuses on the "how" – the sequence of actions.
• Example Languages: C, Pascal, FORTRAN.

2. Object-Oriented Programming (OOP): The "World of Things" Paradigm

• The Big Idea: Instead of a to-do list, OOP views a program as a collection of
interacting objects. An object is a self-contained unit that bundles together
both data (attributes) and the behaviors (methods) that operate on that data.
This is the cornerstone of OOP.
• Analogy: Instead of a list of instructions for building a car, you think about the
car itself as an object. A Car object has data (color, model, current speed) and
behaviors (startEngine(), accelerate(), brake()). The internal complexity of the
engine is hidden; you just interact with the simple interface (pedals, steering
wheel).
• Characteristics:
o Bottom-up design.
o Focuses on "what" things are, not just "how" to do things.
o Data and behavior are tightly coupled within objects.
o Emphasizes concepts like Encapsulation, Inheritance, and Polymorphism.
• Example Languages: Java, C++, Python, C#.

3. Functional Programming: The "Mathematical Machine" Paradigm

• The Big Idea: This paradigm treats computation as the evaluation of


mathematical functions. It avoids changing state and mutable data. The core

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 2


OOP and CG www.sppuengineers.site

principle is that a function, given the same input, will always produce the same
output, with no side effects (like modifying a global variable or printing to the
screen).
• Analogy: It's like a pure mathematical machine. If you put 2 and 3 into
an add machine, it will always spit out 5. It will never change a number written
on a whiteboard outside the machine. This "no side effects" rule is crucial.
• Characteristics:
o Focus on immutable data (data that cannot be changed after creation).
o Functions are "first-class citizens" (can be passed around like variables).
o Avoids loops in favor of recursion and higher-order functions
(like map, filter, reduce).
• Example Languages: Haskell, Lisp, F#, JavaScript (supports functional
concepts).

4. Logic & Rule-Based Programming: The "Detective" Paradigm

• The Big Idea: This paradigm is completely different. Instead of telling the
computer how to solve a problem, you tell it what you know (a set of facts and
rules) and then ask it a question. The computer uses logical deduction to figure
out the answer.
• Analogy: You are a detective. You have a list of facts ("Socrates is a man.") and
rules ("All men are mortal."). You then ask a question ("Is Socrates mortal?").
The logic engine connects the dots and answers "Yes." You didn't tell it the steps
to figure it out; you just gave it the knowledge base.
• Characteristics:
o Declarative style (you declare what is true, not how to compute).
o Based on formal logic.
o Consists of a database of facts and rules.
• Example Language: Prolog.

1.2 The Need for Object-Oriented Programming

As programs grew larger and more complex, the procedural paradigm started to show
its weaknesses. Global data could be modified by any function anywhere in the

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 3


OOP and CG www.sppuengineers.site

program, leading to unpredictable bugs that were hard to trace ("spaghetti code"). It
was difficult to model real-world problems and reuse code effectively.

OOP was born out of the need to manage this complexity.

• Managing Complexity: By bundling data and behavior into objects, OOP creates
self-contained, manageable units.
• Real-World Modeling: It's more natural to think about problems in terms of
real-world objects (a Student, an Invoice, a Button) rather than abstract
procedures.
• Code Reusability: Inheritance allows us to create new classes that reuse the
properties and methods of existing classes.
• Maintainability: Encapsulation protects data from accidental modification,
making code safer and easier to maintain and debug.

Part 2: The Core Principles of OOP

These are the fundamental concepts that define the object-oriented paradigm.

• Class: The blueprint or template for creating objects. A class defines the
common attributes (data members) and behaviors (methods) that all objects of
its type will have.

public class Car {

String color;

int speed;

void drive() {

System.out.println("Car is driving...");

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 4


OOP and CG www.sppuengineers.site

• Object: An instance of a class. It is a concrete entity created from the class


blueprint, residing in memory. You can create many objects from a single class.

public class Main {

public static void main(String[] args) {

Car myCar = new Car(); // Object created from Car class

myCar.color = "Red";

myCar.drive(); // Calls the drive method

• Data Members (Attributes/Fields): The variables inside a class that hold the
state or properties of an object. Data members are variables defined inside a
class. They represent the state of an object.

public class Student {

String name;

int age;

• Methods (Behaviors): The functions defined inside a class that perform actions
or operate on the object's data. Methods are functions defined inside a class.
They represent the behavior of an object.

public class Student {

void study() {

System.out.println("Student is studying...");

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 5


OOP and CG www.sppuengineers.site

• Data Encapsulation: The bundling of data (attributes) and the methods that
operate on that data into a single unit (the class). This is the foundational
concept of OOP.

public class Account {

private double balance;

public void deposit(double amount) {

balance += amount;

public double getBalance() {

return balance;

• Information Hiding & Data Abstraction: These two concepts work together,
enabled by encapsulation.
o Information Hiding: The practice of hiding the internal complexity of an
object from the outside world. This is achieved by making data
members private and providing controlled access
through public methods (getters and setters). This protects the object's
internal state.

public class Bank {

private String password;

public void setPassword(String pwd) {

// logic to validate password

password = pwd;

}}

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 6


OOP and CG www.sppuengineers.site

o Data Abstraction: The process of showing only the essential features of


an object to the user and hiding the unnecessary details. The simple
interface (public methods) is the abstraction.

abstract class Shape {

abstract void draw(); // Only the method name, no implementation

• Inheritance: The mechanism by which one class (the child or subclass) can
acquire the properties and methods of another class
(the parent or superclass). This supports the "is-a" relationship.

class Animal {

void makeSound() {

System.out.println("Animal sound");

class Dog extends Animal {

void bark() {

System.out.println("Dog barks");

• Polymorphism (from Greek, "many forms"): The ability of an object, method,


or operator to take on many different forms. In OOP, it most often refers to the
ability of a single interface (like a method name) to represent different
underlying forms (implementations).
o In Java, this is primarily achieved through method overriding. A parent
class Animal might have a makeSound() method. A Dog subclass would

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 7


OOP and CG www.sppuengineers.site

override it to print "Woof," and a Cat subclass would override it to print


"Meow." You can call animal.makeSound() on any animal object, and it will
perform the correct action.

class Animal {

void makeSound() {

System.out.println("Animal sound");

class Cat extends Animal {

@Override

void makeSound() {

System.out.println("Meow");

Benefits of OOP

• Modularity: Encapsulation creates self-contained objects, making


troubleshooting and collaborative development easier.
• Reusability: Inheritance allows for the reuse of code from existing classes.
• Flexibility: Polymorphism allows a single action to be performed in different
ways.
• Security: Information hiding protects internal data from accidental corruption.
• Maintainability: Code is easier to understand, maintain, and debug.

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 8


OOP and CG www.sppuengineers.site

Part 3: Java as the Vehicle for OOP

Java is a quintessential object-oriented programming language.

• Why Java is Strongly OOP: In Java, virtually everything is an object, and all code
must reside within a class. You cannot have a standalone function like in C++.
• Platform Independence (The JVM):
o JVM (Java Virtual Machine): The JVM is the magic that makes Java
platform-independent ("Write Once, Run Anywhere").
o Process:
1. You write Java source code (.java file).
2. The Java compiler compiles it into an intermediate, platform-neutral
format called bytecode (.class file).
3. The JVM, which is a program designed for a specific operating
system (Windows, macOS, Linux), takes this bytecode and translates
it into native machine code that the host computer can execute.
o Textual Diagram:
[YourCode.java] --(Compiler)--> [YourCode.class (Bytecode)] --(JVM)-->
[Native Machine Code for Windows/Mac/Linux]

3.1 A Simple Java Program Structure

package com.mycompany.project; // Package statement

import java.util.Scanner; // Import statement

// Class definition

public class MyFirstProgram {

// The main method - the entry point of the program

public static void main(String[] args) {

System.out.println("Hello, World!");

• Documentation Section: Comments explaining the code.

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 9


OOP and CG www.sppuengineers.site

• Package Statement: Declares a "namespace" to organize classes and avoid


naming conflicts.
• Import Statements: Imports built-in classes from the Java Class Libraries
(e.g., Scanner for user input).
• Class Definition: Every Java program must have at least one class.
• Main Method: The public static void main(String[] args) method is the special
method where program execution begins.

In Java, when you declare a variable, you are essentially creating a named box in the
computer's memory. What you can put inside that box is determined by its data type.
There are two fundamentally different kinds of boxes.

1. Primitive Types:

Primitive types are the most basic, fundamental building blocks for data. They are
simple and efficient. When you declare a primitive variable, the box in memory holds
the actual value itself.

The 8 Primitive Types in Java:

Type Size Description Example


int 4 bytes The default for whole int score = 100;
numbers.
double 8 bytes The default for decimal double price = 19.99;
numbers.
boolean 1 bit Stores only true or false. boolean isLoggedIn = true;
char 2 bytes Stores a single character char grade = 'A';
(Unicode).
byte 1 byte For very small whole numbers (- byte smallNumber = 100;
128 to 127).
short 2 bytes For short whole numbers. short year = 2024;
long 8 bytes For very large whole numbers. long population =
8000000000L;
float 4 bytes For decimal numbers (less float temperature = 98.6f;
precise than double).

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 10


OOP and CG www.sppuengineers.site

2. Reference Types:
Reference types are used for more complex data structures and objects. When you
declare a reference variable, the box in memory does not hold the object itself.
Instead, it holds a memory address—a "reference" or "pointer"—that tells the
computer where the actual, larger object is stored in a different part of memory called
the heap.

Types of Reference Types:


This includes all objects created from classes, and arrays.

• Classes: String, Scanner, or any class you create yourself (Car, Student, etc.).
• Arrays: int[], String[], etc.

Example: Primitive vs. Reference in Action

int x = 10;

int y = x; // y gets a COPY of the value in x

y = 20; // We change the value in y's box.

System.out.println(x); // Output: 10 (x is unaffected)

System.out.println(y); // Output: 20

int[] arr1 = {1, 2, 3};

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 11


OOP and CG www.sppuengineers.site

int[] arr2 = arr1; // arr2 gets a COPY of the ADDRESS in arr1.

// Both variables now point to the SAME array object in memory.

arr2[0] = 99; // We go to the address and change the object itself.

System.out.println(arr1[0]); // Output: 99 (arr1 is affected because it points to the


same object)

System.out.println(arr2[0]); // Output: 99

2. Java Class Libraries - Your Giant Toolkit

The Java Class Libraries (also known as the Java API) are a massive collection of pre-
written, ready-to-use code that comes with Java. It's like having a giant, professionally
organized toolkit. Instead of building everything from scratch, you can just grab a tool
that does what you need.

• Organized into Packages: These tools are organized into "packages" (like
drawers in the toolkit). To use a tool from a package, you must first import it.
• Example: If you want to read input from the user, you don't need to write
complex code to handle the keyboard. You just import the Scanner class from
the java.util package.

import java.util.Scanner; // Importing the Scanner tool from the 'util' drawer.

public class UserInputExample {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // Using the tool

System.out.print("Enter your name: ");

String name = input.nextLine();

System.out.println("Hello, " + name);

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 12


OOP and CG www.sppuengineers.site

3.Control Statements - Directing the Program's Flow

Control statements are the "traffic signals" of your program. They let you make
decisions and repeat actions, turning a simple list of instructions into a smart, dynamic
program.

1. Selection Statements (Making Decisions)

• if Statement
o Purpose: Executes a block of code only if a certain condition is true.
o Syntax:

if (condition) {

// Code to execute if condition is true

o Example:

int temperature = 30;

if (temperature > 25) {

System.out.println("It's a hot day!");

• if-else Statement
o Purpose: Provides an alternative path. It executes one block of code if the
condition is true, and a different block if it is false.
o Syntax:

if (condition) {

// Code to execute if condition is true

} else {

// Code to execute if condition is false

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 13


OOP and CG www.sppuengineers.site

o Example:

int score = 55;

if (score >= 50) {

System.out.println("You passed!");

} else {

System.out.println("You need to study more.");

• nested if-else (or if-else if-else)


o Purpose: Used for a chain of related decisions. It checks multiple
conditions in order.
o Syntax:

if (condition1) {

// Code for condition1

} else if (condition2) {

// Code for condition2

} else {

// Code if none of the above are true

o Example:

int grade = 85;

if (grade >= 90) {

System.out.println("Excellent (A)");

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 14


OOP and CG www.sppuengineers.site

} else if (grade >= 80) {

System.out.println("Good (B)");

} else if (grade >= 70) {

System.out.println("Average (C)");

} else {

System.out.println("Needs Improvement");

2. Iteration Statements (Looping)

• for loop
o Purpose: The perfect loop for when you know exactly how many times
you want to repeat an action.
o Syntax: for (initialization; condition; update) { ... }
o Example: Print numbers from 1 to 5.

for (int i = 1; i <= 5; i++) {

System.out.println(i);

• while loop
o Purpose: A flexible loop that repeats as long as a condition is true. The
condition is checked before each iteration.
o Syntax: while (condition) { ... }
o Example: A simple countdown.

int countdown = 3;

while (countdown > 0) {

System.out.println(countdown);

countdown--; // Don't forget to update the condition variable!

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 15


OOP and CG www.sppuengineers.site

System.out.println("Blast off!");

• do-while loop
o Purpose: Similar to a while loop, but it guarantees that the loop body will
be executed at least once. The condition is checked after each iteration.
o Syntax: do { ... } while (condition);
o Example: A menu that must be shown at least once.

Scanner input = new Scanner(System.in);

int choice;

do {

System.out.println("--- Menu ---");

System.out.println("1. Start Game");

System.out.println("0. Exit");

System.out.print("Enter your choice: ");

choice = input.nextInt();

} while (choice != 0); // Loop until the user chooses to exit.

• for-each loop (Enhanced For Loop)


o Purpose: A clean, simple way to iterate through every element of an array
or collection without needing an index variable.
o Syntax: for (DataType variable : collection) { ... }
o Example: Print all names in an array.

String[] names = {"Alice", "Bob", "Charlie"};

for (String name : names) {

System.out.println("Hello, " + name);

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 16


OOP and CG www.sppuengineers.site

3. Jump Statements

• break
o Purpose: To immediately exit the current loop (or switch statement).
o Example: Stop a loop as soon as a target number is found.

int[] numbers = {10, 20, 30, 40, 50};

for (int num : numbers) {

if (num == 30) {

System.out.println("Found 30!");

break; // Exit the loop now.

System.out.println("Checking " + num);

• continue
o Purpose: To skip the rest of the current iteration and jump straight to the
beginning of the next iteration.
o Example: Print only the odd numbers in a range.

for (int i = 1; i <= 10; i++) {

if (i % 2 == 0) { // If the number is even...

continue; // ...skip the print statement and go to the next 'i'.

System.out.println(i);

Part 5: Case Study - Java's OOP vs. Others

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 17


OOP and CG www.sppuengineers.site

Feature Java C++ Python


Paradigm Pure OOP. All code Hybrid. Supports Multi-paradigm &
Purity must be inside a both procedural Fully
class. Primitives are (standalone OOP. Supports
the only exception. functions) and procedural,
object-oriented functional, and OOP.
programming. Everything,
including numbers
and functions, is an
object.
Inheritance Single Inheritance Supports Multiple Supports Multiple
for classes. A class Inheritance. A class Inheritance. Mana
can only extend one can inherit from ges it using a
parent several parent Method Resolution
class. Multiple classes directly. Can Order (MRO)
Inheritance is lead to the "Diamond algorithm.
achieved Problem."
through interfaces.
Typing Statically Statically Dynamically
System Typed. Types are Typed. Similar to Typed. Types are
checked at compile Java, provides checked at runtime
time, leading to more compile-time safety. ("duck typing").
robust and error-free Offers great
code (but less flexibility and faster
flexibility). development but
can lead to runtime
errors.
Memory Automatic. Handled Manual. The Automatic. Handle
Management by the Garbage programmer is d by a garbage
Collector (GC). The responsible for both collector and
programmer does allocating (new) and reference counting.
not need to manually deallocating (delete)
deallocate memory. memory. More
powerful but prone
to memory leaks.

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 18


OOP and CG www.sppuengineers.site

Conclusion: Java enforces a pure OOP structure with a focus on safety and portability
(via the JVM and garbage collection). C++ offers more low-level control and
performance at the cost of complexity and manual memory management. Python
provides maximum flexibility and rapid development speed with its dynamic typing
and multi-paradigm approach.

Join Telegram Channel - https://t.me/sppuengineersss | @SPPU Engineers 19

You might also like