Flashcards
Fundamental Concepts of Programming
Cards in this deck (8)
- 1InheritanceDescription:
Inheritance is a fundamental concept in object-oriented programming (OOP) where a class (subclass) can inherit attributes and methods from another class (superclass). The subclass automatically gains access to all public and protected members of the superclass, allowing for code reuse and creating a hierarchical relationship between classes. Inheritance facilitates the creation of more specialized classes based on existing ones, promoting modularity, extensibility, and easier maintenance of code.
Example:
Consider a superclass "Animal" with attributes and methods common to all animals, such as "name" and "eat()". We can then create subclasses like "Dog" and "Cat" that inherit from the "Animal" class. These subclasses inherit the attributes and methods of the "Animal" class but can also have their own unique attributes and methods specific to dogs and cats, such as "bark()" for dogs and "meow()" for cats. - 2PolymorphismDescription:
Polymorphism is a key concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables flexibility in code by allowing different objects to respond to the same message (method) in different ways based on their specific implementations. This promotes code reusability and simplifies coding tasks by enabling more flexible and modular design.
Example:
In a zoo simulation program, we have different types of animals, such as dogs, cats, and birds, all derived from a common superclass "Animal". Each animal has its own unique way of making sounds. We can call a common method "makeSound()" on any animal object. The actual sound produced will depend on the specific implementation provided by each subclass. For example, a dog will bark, a cat will meow, and a bird will chirp. This allows us to write code that is more generic and adaptable, making our program easier to maintain and extend. - 3EncapsulationDescription:
Encapsulation in OOP involves bundling data and methods into a single unit (class), hiding internal details and exposing only necessary features. It enhances data security, modularity, and maintenance by preventing direct access to an object's internal state.
Example:
Car objects encapsulate attributes like speed and methods like accelerate. External code interacts with the car's public interface, ensuring the integrity of its internal state. - 4AbstractionDescription:
Abstraction simplifies complexity by focusing on essential features and hiding unnecessary details. It promotes code reusability and easier maintenance by providing a clear separation between interface and implementation.
Example:
A TV remote control abstracts the TV's functions like power and volume control. Users interact with the simplified interface without needing to understand the TV's inner workings. - 5API (Application Programming Interface)Description:
An API is a set of rules and protocols that allows different software applications to communicate with each other. It defines the methods and data structures that developers can use to interact with a particular software component, service, or operating system. APIs provide a standardized way for applications to exchange information and functionality, enabling developers to integrate third-party services, libraries, and platforms into their own applications. APIs can be provided by operating systems, libraries, web services, or software development kits (SDKs).
Example:
Consider the Google Maps API, which allows developers to embed maps and location-based services into their applications. Developers can use the API to access features like displaying maps, finding directions, and searching for locations within their own applications. By integrating the Google Maps API, developers can enhance the functionality of their applications without needing to build mapping features from scratch. - 6StackDescription:
The stack is a memory structure used by programs to manage function calls and local variables. It follows a last-in, first-out (LIFO) order, meaning that the last item added is the first one removed. Each time a function is called, a new stack frame is created to store its parameters, return address, and local variables. When the function completes, its stack frame is removed, allowing the program to return to the previous function or exit. The stack is typically managed by the operating system or runtime environment.
Example:
In a simple program, function calls are added to the stack when executed. As functions complete, they are removed from the stack in reverse order. For instance, consider a function calling another function, which then calls a third function. Each function call adds a stack frame, creating a stack of function calls. As each function completes, its stack frame is popped off the stack until the initial function returns, clearing the stack entirely. - 7HeapDescription:
The heap is a memory structure used for dynamic memory allocation in programs. Unlike the stack, which operates on a last-in, first-out (LIFO) basis, the heap allows for flexible memory allocation and deallocation in any order. Memory allocated on the heap persists until explicitly freed by the program. The heap is commonly used for storing data with dynamic size or lifetime, such as arrays, objects, and data structures. Proper management of heap memory is crucial to prevent memory leaks and optimize program performance.
Example:
In a program, dynamic memory allocation is performed using functions like malloc() or new. For instance, consider a scenario where a program needs to read a variable amount of data from a file. Instead of preallocating memory on the stack, which may be insufficient or wasteful, the program allocates memory on the heap based on the data size. Once the data processing is complete, the allocated memory is freed using functions like free() or delete to avoid memory leaks and conserve resources. - 8Object-Oriented Programming (OOP)Description:
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects," which are instances of classes that encapsulate data (attributes) and behavior (methods). OOP promotes code organization, modularity, and reusability by modeling real-world entities as objects with properties and actions. The key principles of OOP include encapsulation, inheritance, and polymorphism.
Example:
Consider a "Vehicle" class with attributes like "color" and "speed" and methods like "accelerate" and "brake." We can create subclasses like "Car" and "Truck" that inherit from the "Vehicle" class. Each subclass can have its own unique attributes and methods, such as "numDoors" for cars and "cargoCapacity" for trucks, while inheriting the common behavior of vehicles. This modular and hierarchical approach to modeling real-world entities is a fundamental concept in OOP.
Information
- Description
- Selbstständig Programming Basics Flashcards on Fundamental Concepts of Programming, created by Amano Amano on 08/02/2024.
- Created by
- Amano Amano
- Views
- 2
- Rating