Class, object, fields, properties and methods — OOP Concepts in C#
Lesson 1 of 15 in the free OOP Concepts in C# course on DevLadder. A class is a blueprint; an object is an instance created from it at runtime. public class BankAccount { private decimal _balance ; // field (state) — private public string Owner { get ; } // read-only property public decimal Balance => _balance ; // computed property public BankAccount ( string owner , decimal opening = 0 ) //…
In the first module of the free OOP Concepts in C# course on DevLadder, a class is likened to a blueprint. An object is generated from that blueprint at runtime. The example provided is a BankAccount class, featuring a private field (_balance) representing the account balance, a read-only property (Owner) to get the account owner's name, and a computed property (Balance) reflecting the current balance.
The class has a constructor that initializes the owner and opening balance. It also includes a method named Deposit, which adds funds to the account after validating the amount. An object of the BankAccount class is instantiated using the new keyword, with Divakar as the owner and an initial balance of 500.
The lesson delves into the four pillars of Object-Oriented Programming (OOP) in C#. Encapsulation involves safeguarding the internal state of a class by making its fields private and providing a public API to access and manipulate these fields. In the BankAccount example, the _balance field is private, while the Deposit method provides a controlled way to modify it.
Abstraction focuses on exposing what an object represents while concealing the internal workings. The IPaymentGateway.ChargeAsync method hides the underlying complexities of making HTTP requests, retries, and handling signatures. Inheritance enables a derived class to inherit properties and methods from a base class, facilitating code reuse and extension.
In the example, Contractor derives from Employee, inheriting fields like Name and Describe. Polymorphism allows derived classes to override or extend methods in the base class. The employee.MonthlyPay method demonstrates this by calling either the Permanent or Contractor override at runtime, yielding different behavior based on the object's type.
The distinction between a class and an object is elucidated. A class serves as a type definition, outlining fields, properties, and methods within the code and metadata. On the other hand, an object is an instance of that class created at runtime, occupying memory and possessing its own state. Multiple objects can be generated from a single class.
Method overloading and method overriding are also differentiated. Overloading refers to the practice of defining multiple methods with the same name but differing parameters within the same class or its derived class. Overrides, however, involve a derived class providing a new implementation for a method inherited from its base class, using the same signature.
Overloading is resolved at compile time, based on the method signature, while overriding occurs at runtime, dictated by the object's actual type. The return type can differ in overloading but must remain the same in overriding.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.