Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

OOP Fundamentals in C# 4 methods of System.

Object:
What is OOP?
- Equals()
- An approach to designing and building applications that - GetHashCode()
are: - GetType()
o Flexible - ToString()
o Natural
o Well-crafted
o Testable
- By focusing on objects that interact cleanly one with another

The two most basic words in object-oriented programming are object and class. Though they are often
used interchangeably when speaking about object-oriented programming and when building
applications, they are not the same thing.

A class is code. The properties define the data managed by the class. The functions define the actions
or behaviors managed by the class. In object-oriented programming these functions are called methods.
Together the properties and methods of a class define the class members.

The basic purpose of a class is to provide the definition for a particular type of object. An object is an
instance of a class. It is created using the C# new keyword.

To build an application following object-oriented programming techniques:

- Start by identifying the classes from the requirements or specification


- Next, analyze the classes you identified and separate responsibilities as needed
- Establish relationship - Object-oriented programming involves understanding the
relationships between the classes. The relationships between the classes define how the
objects created from those classes can work together to perform the operations of the
application and one of the key goals of object-oriented programming is reuse

1
- Leveraging reuse - Extensive reuse of existing proven classes not only shortens
development time, it also leads to more robust applications

Abstraction – one of the 4 OOP Pillars

- Defining appropriate classes by focusing on what is important for a purpose.


- You can think of abstraction as the process of defining classes by
o simplifying reality
o ignoring extraneous details
o focusing on what is important for a purpose.

Encapsulation – another OOP Pillar

- Encapsulation is a way to hide or encapsulate the data and implementation details within a
class, thus hiding complexity
- In C#, the data is exposed to the application through property getters and setters
- The operations are exposed to the application through methods
- Encapsulation allows the objects in an application to work together without each object
knowing the details of the implementation. Encapsulation hides both the data and
implementation within the class.
- This data hiding has many benefits.
o It protects the data. No other code can modify the underlying data except through
the getters and setters. This prevents an object's values from being corrupted by
other objects.
o Allows for authorization before getting the data. You can add code to the getter to
perform operations before the data is provided. For example, you could check to
ensure the user has access to the specific data element.
o Allows for validation before setting the data. You can add code to the setter to
perform operations before the data is set
- Implementation hiding
o Helps manage complexity, by breaking it down into manageable units
o Only the class needs to understand the implementation
o Implementation can be changed without impacting the application

2
Static modifier

– declares a member that belongs to the class itself


public static int InstanceCount { get; set; }

- Accessed using the class name – not an object variable


Customer.InstanceCount += 1;
Method signature
- The signature of a method is comprised of its name and type of each its formal parameters
- The signature does not include the return type
- Each signature must be unique

Overloading

- The term overloading is used to describe methods that have the same name, but different
parameters

Contract

- All of the properties and methods defined using the public access modifier, defined the
class' contract
- The contract is also called the class interface

Constructor

- A constructor is code that is executed each time an instance of the class is created
- It is basically a special kind of method named with the class name
- Constructors are normally defined at the top of the class, above the properties and methods
- If you don't need any other constructors, and you don't need any code in the constructor,
you don't actually have to create a constructor. An implied default constructor will be
defined for you.

3
Coupling

- Degree to which classes are dependent on neach other

4
Cohesion

- Degree to which members of the class relate to the purpose of the class

Separation of Concerns

- Decompose an application into parts with minimal overlap


- Each part is responsible for a separate concern

Design patterns

- Common practices for defining appropriate classes and their associated relationships

Objects relationships

- There are three basic types of relationships defined in object-oriented programming


o Collaboration – also known as ‘uses a’ relationship
o Composition – also known as ‘has a’ relationship
o Inheritance – also known as ‘is a’ relationship

Collaboration - exists whenever an object from one class uses one or more instances of another class

Composition - exists whenever an object from one class is composed of one more objects from another
class. Composite objects often contain references to their component objects as properties.

5
Inheritance

- Inheritance allows you to build a class that takes on or inherits the members of another
class so you can define a more specific type and it will inherit all of the members of the
classes above it in the hierarchy.
- C# does not allow multiple inheritance, meaning that a class can derive from only a single
class
- Only build an inheritance relationship between your classes if the derived classes have
specialized properties or behaviors that require unique code and you can significantly
leverage code reuse from the base class

6
Techniques for Reuse

Polymorphism – “Many shaped”

- Two types of polymorphism:


o inheritance-based polymorphism
 The idea behind inheritance-based polymorphism is that a base class can
define a method and any derived class can override that method to provide
its own unique definition and implementation, basically providing its own
shape for that method
o interface-based polymorphism
 One method, multiple implementations in different classes

7
- Polymorphism is the concept that a single method such as the ToString method, can behave
differently, depending on the type of object that calls it

Abstract classes

- An abstract class cannot be instantiated, meaning you can't use the new keyword to create
one.
- An abstract class is intended only to be used as a base class for other classes and never used
on its own.

Concrete classes

- A concrete class can be instantiated, meaning you can use the new keyword to create one

Sealed classes

- A sealed class is a class that cannot be extended through inheritance, meaning that no other
class can inherit from it.
- Sealing a class prevents extension and more importantly, prevents customization, so if you
want to ensure that no other classes extend or override the functionality of a class, consider
marking it as sealed.

Virtual or Abstract?

- Virtual
o Use the virtual keyword if you want to provide the option for the method to be
overridden.
o When using virtual, the method has its own default implementation, but allows a
derived class to override it.
- Abstract
o Use the abstract keyword if the method must be overridden.

8
o When using abstract the method doesn't have its own implementation, so the
derived classes must implement it.
- So to summarize, a derived class cannot override a base class member unless the base class
member is declared specifically to allow it to be overridden using either the virtual or
abstract keywords. The derived member must then use the override keyword to explicitly
indicate that the method is overridden.

Access modifiers

- Public means it's accessible to any other class.


- Private means that it's accessible only to the class in which the property or method resides.
- The protected modifier has to do with inheritance. If a member is protected, it is only
accessible in the base class in which it resides, or any derived classes that inherit from that
base class

Static classes

- A static class cannot be instantiated


- That means you can't create an instance of it using the new keyword to create a variable of
the class type. Because there is no instance variable, you access the members of a static
class using the class name

Extension methods

- An extension method allows you to add methods to any existing type including the .NET
types like string, without having the source code of the type, without using inheritance,
without recompiling, without any changes at all to the original type.
- Another benefit of extension methods is discoverability. If another developer on the team is
looking for a method on string that is already implemented, if the developer typed a string
dot, they will see the methods that are available there from your common component. No
need to hunt around for or remember the name of the class in the component. This will help
your reusable code get reused much more often

Interfaces

- Interfaces provide a clean way for your application to interact with other classes,
components, applications, or systems.
- Interfaces are used in many design patterns and interfaces are an integral part of the .NET
framework
- Definition from Wikipedia: In computer science, an interface is a shared boundary across
which two separate components of a computer system exchange information. The exchange
can be between software, computer hardware, peripheral devices, humans, and
combinations of these

9
- Examples of Interfaces in programming:
o UI - the boundary between the user and an application is called the user interface
o Web API - In web development, the boundary between an application that provides
services and the clients requesting those services is called a web API
o Class interfaces - In an object-oriented application, the boundary between each
class and the rest of the application is called a class interface
- Every class has an implicit class interface. That interface is comprised of the public
properties and methods in the class – implicit interface
- In C# there is also the concept of an explicit interface. An explicit interface is a separate
type that you can create using the interface keyword. Like a class, the interface defines a set
of properties and methods
- An interface does not contain any implementation details

10

You might also like