Week 1 Lecture 2 – C# Fundamentals

OOP Basic Concepts

Encapsulation Is the inclusion of property & method within a class/object in which it needs to function properly. It enables reusability of an instant of an already implemented class within a new class while hiding & protecting the method and properties from the client classes.

The class is a kind of container or capsule or a cell, which encapsulates the set of methods, attributes, and properties to provide its indented functionalities to other classes. Encapsulation also allows a class to change its internal implementation with out hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does its operations while allowing requesting its operations.

Benefits of Encapsulation

◆ Ensures that structural changes remain local:

⬥ Changing the class internals does not affect any code outside of the class

⬥ Changing methods' implementation does not reflect the clients using them

◆ Encapsulation allows adding some logic when accessing client's data

◆ Hiding implementation details reduces complexity

◆ easier maintenance

Inheritance

Inheritance is a way of organizing classes. The term comes from the inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once in the parent class. Superclass inherits its attributes & methods to the subclass(es). A subclass can inherit all its superclass attributes & methods besides having its unique attributes & methods

Inheritance allows child classes to inherit the characteristics of the existing parent class:

• Attributes (fields and properties)

• Operations (methods)

A child class can extend the parent class:

• Add new fields and methods

• Redefine methods (modify existing behavior)

A class can implement an interface by providing the implementation for all its methods.

Benefits of Inheritance

• Expresses commonality among classes/objects

• Allows code reusability

• Highlights relationships

• Helps in code organization

Abstraction The types of classes are Concrete Class

Can be instantiated directly Abstract Class

Can’t be instantiated directly Abstraction is a design principle. It is the process of removing characteristics from something to reduce them to a set of essential characteristics. Through the process of abstraction, a programmer hides all but the relevant data about a class in order complexity and increase reusability. Abstraction is a basic representation of a concept.

Abstraction allows programmers to represent a complex real-world most simply. It is a process of identifying the relevant qualities and behaviors an object should possess, in other words, represent the necessary features without representing the background details You should always use abstraction to ease reusability, and understanding for the design and enable the extension. When we design the abstract classes, we define the framework for later extensions.

An abstract class, which is declared with the “abstract” keyword, cannot be instantiated. It can only be used as a super-class for other classes that extend the abstract class. An abstract class is a design concept and implementation gets completed when it is being realized by a subclass.

An abstract class is a class that may not have any direct instances.

An abstract operation is an operation that it is incomplete and requires a child to supply an implementation of the operation.

Polymorphism

Polymorphism is a generic term that means “many shapes”. Polymorphism means the ability to request that the same methods be performed by a wide range of different types of things. It is achieved by using many different techniques named method overloading, operator overloading, and method overriding.

An object has “multiple identities”, based on its class inheritance tree. It is the ability to look at a class in its parent image

Polymorphism means “many forms” and it occurs when you have many classes that are related to each other by inheritance