Concepts of OOP: Object-Oriented Programming (OOP) is a powerful and widely used programming paradigm that allows developers to model real-world entities and their interactions in software. OOP promotes the use of objects, classes, and encapsulation, providing a structured and modular approach to building complex applications e.g.
Class: Classes are blueprints or templates for creating objects, defining their attributes (properties) and behaviors (methods) e.g.
Fields
Methods
A method is like a function to expose the behavior of an object
Advantage: e.g. Code Reusability & Code Optimization
Note: Instance Variable is created inside the class but outside the method
Constructors
Constructor is called when an instance of the class is created
Constructor is a special type of method that is used to initialize the object
Every time an object is created using the "new()" keyword & at least a constructor is called
Constructor Overloading:
A technique of having more than a constructor with different parameter lists
Each constructor performs a different task
Two types of constructors: e.g.
No-arg constructor or Default
Parameterized constructor
Rules to create Constructor: e.g.
Constructor name must be the same as its class name
Constructor must have no explicit return type
Constructor can't be abstract, static, final, & synchronized
Destructor
It can be used once at a time in a class
It's a special method that gets called automatically ASAP after the life cycle of an object is finished
A destructor is called to de-allocate and free memory
Destructors also known as finalizers are non-deterministic
A destructor will have exact same name as the class prefixed with a tilde (~) e.g. ~Vehicle()
It can neither return a value nor can it take any parameters
Blocks
Nested class & Interface
Note: Class is a collection of objects
Object: Objects are instances of classes, representing individual entities with specific characteristics and actions
Object is an instance of a class
Object is a real-world run-time entity
It has state & behavior
State: represents data (value) of an object
Behavior: represents the behavior (functionality) of an object: e.g. deposit, withdraw
Inheritance>>>: Inheritance enables a class (subclass) to inherit attributes and methods from another class (superclass)
Inheritance represents the IS-A relationship, also known as parent-child relationship, and promotes code reusability
Types of Inheritance: e.g.
Single Inheritance
Multiple Inheritance (Through Interface)
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance (Through Interface)
Why use inheritance?
For Method Overriding (so runtime polymorphism can be achieved)
For Code Reusability
Polymorphism>>>: It allows objects of different classes to be treated as objects of a common superclass through method overriding and method overloading. It enhances flexibility and enables dynamic behavior based on the actual type of the object
When one task is performed in different ways i.e. known as polymorphism
Features:
Method Overloading (compile-time polymorphism)
Within a class: Multiple methods having the same name but different parameters
Advantage: Increases the readability of the program
Different ways to overload:
By changing the number of arguments
By changing the data type
Method Overriding (runtime polymorphism)
Subclass (child class) has the same method as declared in the parent class
Rules for method overriding: e.g.
Method must have the same name as in the parent class
Method must have the same parameter as in the parent class
Must be IS-A relationship (inheritance)
Abstraction>>>: Abstraction focuses on defining the essential features of an object while hiding unnecessary implementation details. Abstract classes and interfaces provide foundations for defining common behavior across multiple classes.
Abstract Class: Abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
Abstract Method: Abstract method can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).
Hiding internal details and showing functionality
Abstraction is achieved by using abstract class and interface
Interface
Is a blueprint of a class
It has static constants and abstract methods
It is used to achieve multiple inheritance e.g. represents IS-A relationship
It can be used to achieve loose coupling
Interface fields are public, static, and final by default, and methods are public and abstract
Marker or tagged interface
Within an interface that has no member
e.g. Serializable, Cloneable, Remote, etc.
Used to provide essential information to JVM
Abstract class achieves partial abstraction (0 to 100%) whereas Interface achieves full abstraction (100%)
Encapsulation>>>: Encapsulation is the process of bundling data (attributes) and methods that operate on the data within a class, hiding the internal details from outside interference
Binding (or wrapping) code and data together into a single unit e.g. known as encapsulation
Access modifiers (public, private, protected) control the visibility of class members, enforcing data protection and providing a well-defined interface for interactions
Association and Aggregation
Association represents a relationship between two or more classes, showing how they are connected
Aggregation is a special form of association, where one class contains another class as a part of relationship
Composition
Composition is a strong form of aggregation where the lifetime of the contained object is tied to the container
It allows building complex structures by combining multiple objects
Design Patterns
Design patterns are reusable solutions to common programming problems, facilitating software design and development
e.g. Singleton, Factory, Observer, and Strategy patterns
Singleton Pattern: Ensures that a class has only one instance and provides a global point of access to it. Click >>>
Factory Pattern: Defines an interface for creating an object but lets subclasses alter the type of objects that will be created. Click >>>
Observer Pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Click >>>
Facade Pattern: Provides a simplified interface to a set of interfaces in a subsystem, making it easier to use. Click >>>
Strategy Pattern: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it. Click >>>
SOLID Principles and OOP
The SOLID principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) complement OOP, guiding developers in writing maintainable and scalable code
Conclusion
Object-Oriented Programming provides a structured and efficient approach to software development, allowing developers to create modular, maintainable, and flexible applications.
Understanding the core concepts of OOP empowers developers to design elegant solutions and build robust software systems.
Embracing OOP principles leads to higher code quality, better collaboration, and enhanced software development experiences.
Note: Object Oriented Programming (OOP) is easier than Procedure-Oriented Programming (POP)
#Programming #OOP #CodeAnalysis #Concepts #AbdurRahimRatulAliKhan #ARRAK