Summary of "كورس C# OOP منحة ITI شرح م احمد ممدوح #3"
Summary of the Video:
"كورس C# OOP منحة ITI شرح م احمد ممدوح #3"
This video is a detailed lecture on Object-Oriented Programming (OOP) concepts in C#, focusing primarily on classes, objects, members, Encapsulation, Access Modifiers, and basic principles of OOP such as Abstraction and Encapsulation. The instructor uses practical examples like a "Car" class and a "Facebook Account" class to illustrate these concepts.
Main Ideas and Concepts:
1. Classes and Objects:
- A class is a blueprint for objects.
- An object is an instance of a class, containing copies of the class’s attributes (fields/variables).
- Multiple objects from the same class have the same attributes but can hold different values.
- Example: Two objects from the class
Careach have their ownpriceandnumberattributes.
2. Class Members:
- Members include variables (fields) and functions (methods) inside a class.
- Members can be accessed from inside the class or outside the class (through objects).
- Syntax example:
C1.priceaccesses thepricemember of the objectC1.
3. Accessing Members and Compiler Errors:
- Direct access to class members from outside the class can cause compiler errors if the member is not publicly accessible.
- To access members from outside, two conditions must be met:
- You must have an object of the class.
- The member must be public.
4. Encapsulation:
- Encapsulation is the principle of controlling the visibility of class members.
- It hides certain details from the user for:
- Security reasons (e.g., password fields).
- Irrelevance of internal details (e.g., how a car engine starts).
- Control over data integrity (e.g., business rules for price values).
- Encapsulation is implemented using Access Modifiers.
5. Access Modifiers:
- Public: Member is accessible from outside the class.
- Private: Member is accessible only within the class.
- By default, members are private if no access modifier is specified.
- Private members cannot be accessed directly from outside but can be accessed indirectly via public methods.
6. Using Public Methods to Access Private Members:
- To maintain control, private members are accessed via Public getter and setter methods.
- Example:
- Private field
pricecannot be accessed directly. - Public method
SetPrice(value)validates the value (e.g., price >= 5000) before assigning it. - Public method
GetPrice()returns the current price.
- Private field
- This approach enforces business rules and data validation.
7. Business Logic and Access Control:
- If a variable must follow certain rules (e.g., price must be >= 5000), it should be made private.
- Public setter methods enforce these rules before changing the private member.
- Example with a Facebook password:
- Password is private.
- Public setter method requires the old password before setting a new one.
- No public getter for the password (for security).
8. Abstraction vs. Encapsulation:
- Abstraction: Deciding which attributes and methods to include in the class based on business requirements.
- Encapsulation: Deciding which members are visible or hidden from outside users.
- Encapsulation does not remove the member; it just controls its visibility.
9. Design Principles:
- Classes and functions should focus on a single responsibility/topic.
- Functions should perform one action only (Single Responsibility Principle).
- This improves code clarity, maintainability, and reusability.
10. Displaying Object Information:
- Old way: Using
Console.WriteLinewith indexed placeholders (e.g.,{0},{1}). - New way: Using Interpolated strings with
$"..."syntax, embedding variables directly inside curly braces{}. - Example:
Console.WriteLine($"Price: {price}, Number: {number}"); - This method is clearer and less error-prone.
11. Function Calls and Object Context:
- When calling an object’s method like
C1.Display(), the method works on the data of the calling object (C1). - The function knows which object's data to use implicitly (explained to be covered in detail in a future lesson).
Methodology / Instructions Presented:
- Creating Objects:
Car C1 = new Car();Car C2 = new Car();
- Accessing Members:
- Direct access
Category
Educational