4 and Programming Flashcards
(27 cards)
Objects
Objects are the fundamental building blocks of object-oriented programming (OOP). They represent real-world entities or abstract concepts in code. An object encapsulates data (attributes) and the methods that operate on that data.
Classes
A class is a template or blueprint for creating objects. It defines the attributes and methods that all objects of that type will have.
Instantiation
Instantiation is creating an object.
And each object is an instance of its class.
Encapsulation
Encapsulation is like creating a protective capsule around an object’s internal data, shielding it from unauthorized access and manipulation. It involves:
* Data hiding: Making an object’s internal data private
* Public interfaces: Providing controlled access to the object’s data through methods
Association
This is a general relationship between objects. For example, a Student object might be associated with multiple Course objects.
Aggregation
This is a “has-a” relationship where one object contains references to other objects. For instance, a University object might contain multiple Department objects.
Composition
This is a stronger form of aggregation where the contained objects cannot exist independently of the container. For example, a House object might be composed of Room objects.
Inheritance
This is an “is-a” relationship where one class (subclass) inherits properties and methods from another class (superclass). For example, a Car class might inherit from a Vehicle class.
Object-Oriented Design Process
- Identifying the objects in the system
- Defining the attributes for each object
- Determining the methods each object should have
- Establishing the relationships between objects
Benefits of Object-Oriented Programming
- Modularity: Objects can be maintained independently of other objects or code. Allowing coding teams to divide the work
- Reusability: Objects can be reused in different programs. Plus inheritance.
- Productivity: Programmers can build new programs quicker through the use of existing objects.
- Easily upgradable and scalable: Updating or adding new features becomes easier.
- Security: The encapsulation of data and functionality provides a secure programming model.
- Flexibility: Through polymorphism, objects can be treated as instances of their parent class.
Advantages of Encapsulation
- Data Protection: Prevents unauthorized access to an object’s internal state.
- Flexibility: Allows changing the internal implementation without affecting the public interface.
- Maintainability: Easier to debug and maintain as data access is controlled.
- Modularity: Promotes modular design by separating internal workings from external interactions.
- Prevents accidental data editing from other classes.
Parent-child relationship in inheritance.
- Parent class (superclass): The existing class being inherited from
- Child class (subclass): The new class that inherits from the parent
Advantages of Inheritance
- Code Reusability: Reduces redundancy by reusing code from existing classes.
- Hierarchical Classification: Organizes classes into a logical hierarchy.
- Extensibility: Easily extend or specialize existing classes.
- Polymorphism: Enables runtime polymorphism through method overriding.
Polymorphism
Polymorphism allows methods to have the same name and only be differentiated by the class they are in or the parameters they have.
Types of Polymorphism
- Compile-time Polymorphism (Method Overloading): Multiple methods with the same name but different parameters in the same class.
- Runtime Polymorphism (Method Overriding): Subclass provides a specific implementation for a method defined in its superclass.
Attributes
Data to create object
Methods
Behaviors
UML Diagrams
Unified Modeling Language (UML) diagrams are visual representations of software systems. They help in designing, documenting, and communicating about object-oriented systems.
Recursion
Recursion occurs when a method calls itself within its own code. This self-referential approach allows complex problems to be solved by repeatedly applying the same solution to smaller instances of the problem.
Key Components of Recursion
- Base case: The condition that stops the recursion
- Recursive case: The part where the function calls itself
- Progress towards the base case: Each recursive call should move closer to the base case
Tracing Recursive Algorithms
Tracing recursive algorithms involves following the sequence of recursive calls and their return values.
Floating-point numbers
Decimals (Doubles)
Issue with doubles
Take more memory
A disadvantage of using Object Oriented Design.
To construct an Object Oriented program is difficult and for many smaller tasks potentially unnecessary.