Docsity

Understanding OOP: Encapsulation, Inheritance, Polymorphism, and Design Patterns, Study notes of Programming Languages

Toggle sidebar
bg1
Object Oriented Programming
Object Oriented Programming (OOP) is a method of computer programming (software design) in
which programmers describe not only the data types of a date structure, but also the types of
operations that may be performed on them (functions) (What is Object Oriented Programming?
OOP Explained in Depth, 2021). The first step in OOP is to identify all of the objects that a
programmer wishes to work with and their relationships. Once an item is identified, it is
classified as a class of objects that defines the sort of data it contains and all logic sequences that
can handle it. Every different logical sequence is referred to as a method, and objects can
communicate using well-defined message interfaces. Object-oriented programming languages
include Java, C++, C#, Python, JavaScript, Ruby, Dart, and others.
Features/Characteristics of OOP
The features or principles of OOP are explained below briefly:
1. Encapsulation
Encapsulation Meaning: In object-oriented computer programming languages, the notion
of encapsulation (or OOP Encapsulation) refers to the bundling of data, with the methods
that operate on that data, into a single unit. Many programming languages use
encapsulation frequently in the form of classes. A class is a program code-template that
allows developers to create an object that has both variables (data) and behaviors
(functions or methods). A class is an example of encapsulation in computer science in
that it consists of data and methods that have been bundled into a single unit.
Encapsulation may also refer to a mechanism of restricting the direct access to some
components of an object, such that users cannot access state values for all of the variables
of a particular object. Encapsulation can be used to hide both data members and data
functions or methods associated with an instantiated class or object. − Implementation in
C#:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39

Partial preview of the text

Download Understanding OOP: Encapsulation, Inheritance, Polymorphism, and Design Patterns and more Study notes Programming Languages in PDF only on Docsity!

Object Oriented Programming Object Oriented Programming (OOP) is a method of computer programming (software design) in which programmers describe not only the data types of a date structure, but also the types of operations that may be performed on them (functions) (What is Object Oriented Programming? OOP Explained in Depth, 2021). The first step in OOP is to identify all of the objects that a programmer wishes to work with and their relationships. Once an item is identified, it is classified as a class of objects that defines the sort of data it contains and all logic sequences that can handle it. Every different logical sequence is referred to as a method, and objects can communicate using well-defined message interfaces. Object-oriented programming languages include Java, C++, C#, Python, JavaScript, Ruby, Dart, and others. Features/Characteristics of OOP The features or principles of OOP are explained below briefly:

  1. Encapsulation Encapsulation Meaning: In object-oriented computer programming languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, with the methods that operate on that data, into a single unit. Many programming languages use encapsulation frequently in the form of classes. A class is a program code-template that allows developers to create an object that has both variables (data) and behaviors (functions or methods). A class is an example of encapsulation in computer science in that it consists of data and methods that have been bundled into a single unit. Encapsulation may also refer to a mechanism of restricting the direct access to some components of an object, such that users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object. − Implementation in C#:
  1. Abstraction Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. − That’s a very generic concept that’s not limited to object-oriented programming. You can find it everywhere in the real world. − Objects in an OOP language provide an abstraction that hides the internal implementation details. Similar to the coffee machine in your kitchen, you just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation. But you don’t need to understand how this method is implemented and which kinds of actions it has to perform to create the expected result. − Let’s implement the coffee machine example in Java. You do the same in any other object-oriented programming language. The syntax might be a little bit different, but the general concept is the same. − Advantages of Abstraction • The main benefit of using an Abstraction in Programming is that it allows you to group several
  1. Inheritance Inheritance is the process of a derived class inheriting the properties of a base class. The properties of the parent class will be inherited by the child class according to this principle. The primary goal of inheritance is to make coding simpler and shorter by allowing the parent class's code to be reused in the child class. It aids in reusability. In object-oriented programming, inheritance allows new objects to inherit properties from existing objects. A superclass or base class is a class that serves as the foundation. A subclass or derived class is a subclass of a super class. It is also appropriate to use the terms parent class and child class. A child inherits its parent's visible properties and methods while also adding its own. The types of inheritance are explained below briefly: a. Single inheritance The simplest form of inheritance is single inheritance. The lone derived class inherits from the only parent class in this inheritance. Example:

b. Multiple inheritance The derived class inherits from two or more base classes in this sort of inheritance. Example

5. Object / Class In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). − The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code. 6. Polymorphism: − The word polymorphism is used in various contexts and describes situations in which something occurs in several different forms. In computer science, it describes the concept that

objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface. It is one of the core concepts of object- oriented programming (OOP). − If you’re wondering if an object is polymorphic, you can perform a simple test. If the object successfully passes multiple is-a or instance of tests, it’s polymorphic. As I’ve described in my post about inheritance, all Java classes extend the class Object. Due to this, all objects in Java are polymorphic because they pass at least two instance of checks. − Different types of polymorphism  static or compile-time  dynamic Benefits of OOP  OOP is easy to maintain because of its modularity and encapsulation.  OOP simulates the real world, making it simpler to comprehend.  Because objects are complete in and of themselves, they can be reused in other programs.  More time and resources may be spent on software verification with faster software development and cheaper development expenses. Object-oriented software tends to deliver better software, despite the fact that quality is based on team expertise. Limitation of OOP  Object-Oriented programs are typically sluggish and consume a lot of memory.  Over-generalization  It is possible that programs designed using this paradigm will take longer to develop. Some of the most important programming methods, such as inheritance and polymorphism, can be difficult to grasp.

I. Creational Design Pattern − The creational patterns aim to separate a system from how its objects are created, composed, and represented. They increase the system's flexibility in terms of the what, who, how, and when of object creation. − As modern software engineering depends more on object composition than class inheritance, emphasis shifts away from hard-coding behaviors toward defining a smaller set of basic behaviors that can be composed into more complex ones. Hard-coding behaviors are inflexible because they require overriding or reimplementing the whole thing in order to change parts of the design. Additionally, hard-coding does not promote reuse and makes it difficult to keep track of errors. For these reasons, creational patterns are more useful than hardcoding behaviors. Creational patterns make design become more flexible. They provide different ways to remove explicit references in the concrete classes from the code that needs to instantiate them. In other words, they create independency for objects and classes. − Consider applying creational patterns when: − A system should be independent of how its objects and products are created. − A set of related objects is designed to be used together. − Hiding the implementations of a class library or product, revealing only their interfaces.

− Constructing different representation of independent complex objects. − A class wants its subclass to implement the object it creates. − The class instantiations are specified at run-time. − There must be a single instance and client can access this instance at all times. − Instance should be extensible without being modified. − Types of creational design patterns

  1. Factory Method
  2. Abstract Factory
  3. Builder
  4. Prototype
  5. Singleton III. Structural Design Patterns − Structural design patterns are concerned with how classes and objects can be composed, to form larger structures. − The structural design patterns simplifies the structure by identifying the relationships. − These patterns focus on, how the classes inherit from each other and how they are composed from other classes. − Types of Structural Design patterns:
  6. Adapter
  7. Bridge
  8. Composite
  9. Decorator
  10. Facade
  11. Flyweight
  12. Proxy IV. Behavioral Design Patterns − In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication. − Types of Behavioral Design Patterns:

design patterns, such as the Visitor Pattern, allow you to do things that would otherwise be impossible or complicated, such as adding new functionality to a group of classes without them realizing it. One would create an interface or virtual methods using standard OOP techniques for each class. An interface or virtual methods that each class implements would be created using standard OOP techniques. This is in violation of the OOP open/closed principle.

  1. Choose effective differences between ‘Boxing’ and ‘Unboxing’. a. ‘Boxing’ is the process of converting a value type to the reference type and ‘Unboxing’ is the process of converting reference to value type b. ‘Boxing’ is the process of converting a reference type to value type and ‘Unboxing’ is the process of converting value type to reference type c. In ‘Boxing’ we need explicit conversion and in ‘Unboxing’ we need implicit conversion d. Both ‘Boxing’ and ‘Unboxing’ we need implicit conversion Unified Modeling Language (UML) UML is a standardized modeling language that consists of an integrated collection of diagrams that was created to assist system and software developers in designing, visualizing, building, and documenting software system artifacts, as well as business modeling and other non-software systems. As a result, UML makes these assets scalable, secure, and reliable to use. In the development of object-oriented software, UML is a critical component. It creates visual models of software systems using graphic notation. The UML architecture is a straightforward design based on the meta object's functionality. The meta object facility is the one that usually establishes the groundwork for developing modeling languages. They are usually accurate enough to produce the whole application. As a result, a fully executable UML can be deployed across different platforms. They can be distributed to many platforms and hence used across the software because they use diverse technologies. The UML encapsulates the finest technical practices for huge and dynamic systems that have been proven to be useful in simulation. The UML is an important part of software development and production. The UML's most used notations for program architecture is graphical notations. Project teams can use UML to interact, explore alternative designs, and test the software's architectural design hence it is very popular.

Benefits of UML UML is largely used to model the system's work flow and behavior. However, there are more benefits of UML which are listed below briefly:  To give consumers with a ready-to-use expressive visual language that aids in the interchange of meaningful models between developers.  To provide customization and extensibility mechanisms to the main concepts.  To incorporate best practices that aid in the creation of a solid application.  To promote concepts such as collaboration, frameworks, patterns, and components at a higher degree of development. UML tools A software program that supports part or all of the syntax and semantics associated with the Unified Modeling Language (UML), which seems to be the industry norm general-purpose modeling language for computer science, is known as a UML tool. The term "UML tool" is used generically here to refer to application programs that are not entirely focused on UML but do support some UML capabilities as an add-on, a component, or as part of a comprehensive capability. Some of the best UML tools which are leading industry are listed below:

  1. Umbrello

Explanation: This is a manager's development system. Create a system that can manage students as well as subject teachers. When users first launch the app, they will have the option of viewing the student or lecturer management screens. The administration has the ability to add, edit, delete, and perform query tasks such as searching. Then you'll be able to see what you've seen. This is the manager's development system. Create a system that can manage students as well as subject teachers. When users first launch the app, they will have the option of viewing the student or lecturer management screens. The administration has the ability to add, edit, delete, and perform query tasks such as searching. Then we'll be able to see what we've changed. This can be viewed as a solution that simplifies management. – Class Program

  • This is the main class used to run the program and execute statements − Class MainMenu
    • This is a class used to Display Main Menu and get input from user to decide which menu will be display next (StudentMenu or LectureMenu) − Class StudentMenu
  • Display menu of StudentManagment − Class StudentManagement:
  • This class is used to manage Student and execute functions (Add, Delete, Update, Search, View) that inherit from Class Management. − Class LectureManagement - This class is used to manage Lecture and execute functions (Add, Delete, Update, Search, View) that inherit from Class Management. − Class Lecturer
  • This is the class used to declare a property variable that a student object will include. Lecturer Id, Name, Age, Email to contact. Declare functions to get value and set value and publicize it for use in other classes − Class Student
    • This is the class used to declare a property variable that a student object will include. Student Id, Name, Age, Email to contact. Declare functions to get value and set value and publicize it for use in other classes − Class Management
  • This is a class used to store data of both students and lecturer objects after inputting or data of two classes are entered here to manage and query with similar functions such as add, delete, update., view, search III. Pseudo-code

− Explain: This is a feature of the application that displays a list of newly added lecturers; the required data are Access LecturerID, Name of Lecturer, Date of Birth, Home Address, and finally Mail to Contact, which must be filled out. The lecturer's show list view is successful because there is enough information. Define class diagrams for specific design patterns using a UML tool. I. Creational Design patterns ➢ Factory method:

− "Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses." (Gang Of Four) − Creating an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. − The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects. − UML class Diagram: In the above UML class diagram, the Creator class that requires a Product object doesn't instantiate the Product1 class directly. Instead, the Creator refers to a separate factoryMethod() to create a product object, which makes the Creator independent of which concrete class is instantiated. Subclasses of Creator can redefine which class to instantiate. In this example, the Creator1 subclass implements the abstract factoryMethod() by instantiating the Product1 class. − Example: