C# Final Exam Notes PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides notes on C# programming, focusing on Object-Oriented Programming (OOP) concepts. The key topics covered include classes, objects, access modifiers, principles of OOP (encapsulation, abstraction, inheritance, and polymorphism), relationships between classes (aggregation and composition), and static variables/methods. It serves as a study guide.
Full Transcript
C# FINAL EXAM OOP BASICS Object-Oriented Programming (OOP) is a programming model that organizes software design around objects, which combine data (fields/properties) and behaviors (methods). Difference Between Class and Object A class is like a template for an object that describes what a specifi...
C# FINAL EXAM OOP BASICS Object-Oriented Programming (OOP) is a programming model that organizes software design around objects, which combine data (fields/properties) and behaviors (methods). Difference Between Class and Object A class is like a template for an object that describes what a specific kind of object will look like. A class is not an object itself, it’s a blueprint for creating similar objects. It doesn’t allocate memory when it is created. It doesn’t do anything until an object is created from it. Additionally, class defines properties and methods for objects to use. On the other hand, an object is the instance of a class. An object is a real example of a class with actual data for its features and can use the behaviors (methods) described in the class. Memory is allocated when the object is created. When an object is created from a class this is called Instantiation. For example, if the class represents a smartphone, Android Phones and Apple Phones would be objects of that class. Access Modifiers Access modifiers are the keywords used to indicate the accessibility of the methods, classes, constructors, and the other elements. There are three main access modifiers: public, private, and protected. Having a public access modifier means any user can have easy access from the outside of the class. Private is essentially restricting the access to the user from outside of the class. This is used to encapsulate and protect the data from being modified from outside code. In diagrams the plus sign represents the public access modifier, and the minus sign represents the private access modifier. To give an example, if a Bank Account is a class, the owner’s name, account number, account balance would be private to prevent data being changed. It can only be accessed by public methods. Protected access modifiers mean the code is accessible within the same class, or in a class that is inherited from that class. Principles: In C# OOP revolves around four main principles; 1) Encapsulation: Bundling the data (fields) and methods (functions) that operate on the data into a single unit called a class. As shown in this picture getters/setters. 2) Abstraction: Hiding complex implementation details and showing only the necessary features. As shown in this picture the Animal class is an abstract class. The abstract class Animal represents a general concept of an "animal" without specifying details. It provides a blueprint that other classes can extend. MakeSound() is an abstract method that enforces any derived class to define how that specific animal makes a sound. 3) Inheritance: A mechanism where a child class (derived class) inherits the properties and behavior of an existing-parent-class (base class). 4) Polymorphism: The ability of a function, object, or variable to take on multiple forms. Aggregation and Composition Concepts that describe the relationship between classes. Aggregation (Weak Relationship): A "has-a" relationship where the child object can exist independently of the parent object. In other terms where one object contains another object. Example: A school contains students. A student can exist independently of the school, and they don’t cease to exist if the school does. Composition (Strong Relationship): A "has-a" relationship where a child object cannot exist independently of the parent object. In other terms the lifetime of the part is tied to the whole. If the parent is destroyed, the child is also destroyed. Example: A human and their heart. The heart (child) cannot exist without the human (parent). Inheritance Inheritance allows a class to inherit members from another class. It promotes code reuse. One class derives properties and behaviors from another class. Polymorphism Polymorphism enables objects to be treated as instances of their parent class rather than their actual class. This can be achieved through method overriding and interface implementation. Polymorphism can be: Compile-time (Method Overloading): Same method name with different signatures. Doesn’t require inheritance!! Method signatures must be different!! Runtime (Method Overriding): Same method name in base and derived classes. It requires inheritance!! Method signatures must be the same!! The keyword virtual indicates that a method may be overridden by a subclass. Static Variables and Static Methods Static variables and static methods belong to the class and they are shared between all objects. If an object modifies a static variable, all objects of the same class are affected. Also a static variable can be accessed without creating an instance of the class. Static methods can be called using the same way we call any other methods. But static methods can’t access instance variables or methods, only static variables and methods. Abstract Classes A class that cannot be instantiated and is meant to be inherited by another class. It may contain abstract methods which must be implemented in the derived class and non-abstract methods. As shown below, the GetArea() method is implemented in the derived class. Only defined inside the abstract class. Interfaces A contract that defines what a class must implement, without providing implementation inside the interface only inside the class that implements it. Just like an abstract class, the interface doesn’t include any code for how these methods work but the implementing class must provide the logic for the methods. However, unlike abstract classes, interfaces don’t need inheritance but they allow any class to adopt multiple interfaces. The main purpose of an interface is to define a contract that implementing classes should follow. It allows for a form of abstraction where the focus is on what needs to be done rather than how it should be done. For example, if an Animal interface has a makeSound() method, the implementing Wolf class should implement the makeSound() Method with its logic. In this scenario, the makeSound method represents what needs to be done, while the implementation within the Wolf class specifies how it will sound. Additionally, any class could implement multiple interfaces. For instance, Wolf could also implement an Runnable interface to , include running behavior, along with the ability to make sounds.