Object-Oriented
Programming Concepts
Sub-Tittle: Classes, Objects, Encapsulation, Polymorphism, and More
Name: Koushik Biswas
Roll No: 11900123066
Department: Computer Science and Engineering
Subject: Object Oriented Programming (PCC-CS503)
Year: 3rd Semester: 5th Section: B
[Link]
The Four Pillars of OOP
Encapsulation Inheritance
Bundling data and methods that operate on the data A mechanism where one class acquires the properties
within a single unit, hiding internal details. and behaviors of another class, promoting code reuse.
Polymorphism Abstraction
The ability of an object to take on many forms, allowing a Showing only essential features of an object and hiding
single interface to represent different underlying forms. the complex implementation details.
These principles form the foundation for building robust, scalable, and maintainable software systems.
[Link]
Classes and Objects: The Building Blocks
Class: The Blueprint
A class is a template or blueprint from which objects are created. It defines the properties (attributes) and behaviors (methods) that all
objects of that class will have.
Think of it as the design for a car.
Object: The Instance
An object is a real-world entity and an instance of a class. When a class is defined, no memory is allocated until an object of that class is
created.
The car itself, built from the blueprint, is an object.
[Link]
Encapsulation: Protecting Your Data
Encapsulation is the mechanism of binding data and the methods that manipulate that
data together within a single unit, typically a class. It's about bundling the data with the
functions that operate on that data.
It hides the internal state of an object and requires all interaction to be through the
object's methods.
How it's Achieved:
• Private Variables: Declaring instance variables as private so they cannot be accessed directly from outside the class.
• Public Getters and Setters: Providing public methods (getter and setter methods) to access and modify the private variables indirectly.
Benefits:
• Security: Prevents direct access to data, protecting it from unintended modifications.
• Modularity: Each object can be self-contained and operate independently, simplifying debugging and maintenance.
• Flexibility: Allows changes to the internal implementation without affecting how the object is used externally. [Link]
Polymorphism: "Many Forms"
Polymorphism means "many forms". It refers to the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs
when a parent class reference is used to refer to a child class object.
1 2
Compile-time Polymorphism (Method Overloading) Runtime Polymorphism (Method Overriding)
Achieved when a subclass provides a specific implementation for a
Achieved by having multiple methods with the same name but method that is already defined in its parent class. The method to be
different parameters within the same class. The compiler decides called is determined at runtime.
which method to call at compile time.
class Animal { void sound() { /*...*/ } }class Dog
class Calculator { int add(int a, int b) { return extends Animal { void sound() { /*Woof*/ } }Animal
a + b; } double add(double a, double b) { return myDog = new Dog();[Link](); // Calls Dog's
a + b; }} sound()
This principle allows for flexible and extensible code, as a single interface can be used to handle various data types.
[Link]
Polymorphism in Action
One interface, many implementations: This is the core concept
of polymorphism. It means that the same method call can
behave differently depending on the object type it's invoked on.
Consider a draw() method. If you have a Shape class, and
subclasses like Circle, Rectangle, and Triangle, each
subclass can implement its own specific draw() method.
This dynamic behavior enables the creation of highly flexible and extensible code, allowing new shape types to be added without
modifying existing code that uses the draw() method.
[Link]
Constructors: Object Initialization
A constructor in Java is a special type of method that is used to initialize objects. It is called when an instance of the class is created. Constructors have the same name as the class and do not have a return type.
Types of Constructors:
• Default Constructor: If you don't define any constructor, Java provides a default (no-argument) constructor automatically. It initializes instance variables with their default values.
• Parameterized Constructor: Takes one or more parameters. It's used to initialize instance variables with the provided values, allowing you to create objects with specific initial states.
• Copy Constructor (Concept): While Java doesn't have a built-in copy constructor like C++, the concept is achieved by creating a constructor that takes an object of the same class as an argument to copy its values.
Example Code Snippet:
class Student { String name; int age; // Parameterized Constructor Student(String n, int a) { name = n; age = a; } public static void main(String[] args) { Student student1 =
new Student("Alice", 20); // Calls parameterized constructor [Link]([Link] + ", " + [Link]); }}
[Link]
Data Types, Variables, and Operators
Data Types
• Primitive Types: Fundamental data types that store simple values. They directly hold
the data in memory.
• int (integers), char (single characters), boolean (true/false), double (floating-
point numbers), float, long, short, byte
• Reference Types: Store references (addresses) to objects in memory, not the actual
data. They are user-defined or built-in classes.
• String, Arrays, Classes, Interfaces
Variables and Operators
• Variables: Named memory locations used to store data. They must be declared with a specific data type.
• Operators: Symbols that perform operations on variables and values.
• Arithmetic: +, -, *, /, % (modulus)
• Relational: == (equal to), != (not equal to), <, >, <=, >=
• Logical: && (AND), || (OR), ! (NOT)
• Assignment: =, +=, -=, etc.
• Unary: ++, -- [Link]
Primitive vs. Reference Types: Memory and Usage
Primitive Types Reference Types
• Direct Value Storage: Store the actual data value directly in memory. • Object Reference Storage: Store the memory address (reference) where the object is actually
• Fixed Size: Each primitive type has a predefined, fixed size in memory. stored.
• Memory Allocation: Stored on the Stack. • Variable Size: Objects can be of varying sizes, and their size is determined at runtime.
• Examples: int x = 10;, boolean isTrue = true; • Memory Allocation: Stored on the Heap.
• Examples: String str = "Hello";, MyClass obj = new MyClass();
Key Differences and Example:
Understanding the distinction is crucial for memory management and object manipulation in Java.
// Primitive Type Exampleint a = 5;int b = a; // 'b' gets a copy of 'a's valueb = 10;[Link](a); // Output: 5 (a is unchanged)// Reference Type
Exampleint[] arr1 = {1, 2, 3};int[] arr2 = arr1; // 'arr2' refers to the SAME array as 'arr1'arr2[0] = 99;[Link](arr1[0]); // Output: 99 (arr1[0] is
changed)
[Link]
Conclusion: Mastering OOP
Summary of Concepts Importance for Java Next Steps & Resources
We've covered the foundational Development Continue practicing by building small
concepts of OOP: Classes, Objects, Mastering OOP is not just about projects, focusing on applying each OOP
Encapsulation, Polymorphism, understanding definitions; it's about principle. Explore advanced topics like
Abstraction, Inheritance, and adopting a powerful paradigm that is interfaces, abstract classes, and design
Constructors. These principles empower central to Java. It leads to modular, patterns. Utilize the vast resources
developers to write more organized, reusable, and scalable applications, available:
efficient, and maintainable code. making complex systems manageable. • Books: "Head First Java", "Effective
Java" by Joshua Bloch
• Online Courses: Coursera, Udemy,
Codecademy (search for "Java
OOP")
• Documentation: Official Oracle Java
Documentation
[Link]