Encapsulation in C#
What is encapsulation ?
What is the advantage of Providing Variable Access via Setter
and Getter Methods in C# ?
If we provide Variable Access via Setter and Getter Methods in C#, we can validate the
user-given data before storing the value in the variable. we provide direct access to the
balance variable, it is impossible to validate the given amount value before storing it in the
balance variable. So, the main reason for using data hiding is security
What is the Problem if we don’t follow the Encapsulation
Principle in C# while Designing a Class?
If we don’t follow the Encapsulation Principle in C# while designing the class, we cannot
validate the user-given data according to our business requirements, as it is very difficult to
handle future changes.
What is the advantages of the Encapsulation ?
Abstraction in C#
What is Abstraction ?
we can define abstraction as the process of hiding implementation details of the object’s feature
and showing only the essential information of the feature to the user. Abstraction lets you focus
more on what an object does rather than how it does.
he problem is the user of our application accesses the SBI and AXIX classes directly. Directly
means they can go to the class definition and see the implementation details of the methods.
That is, the user will come to know how the services or methods are implemented. This might
cause security issues. We should not expose our implementation details to the outside.
How To Implement Abstraction Using Interfaces ?
1- use interfaces to write a services definition
2- make your class to implement this interface
3- make new class that create an object which it's pointer refers to the interface
4- then if user want to access any service then he should create an object for the new class .
How To Implement Abstraction Using Abstract Classes
1- use abstract class to write a services definition
2- make your class to implement this abstract class
3- make new class that create an object which it's pointer refers to the abstract class
4- then if user want to access any service then he should create an object for the new class .
Encapsulation vs Abstraction in C#
he Encapsulation Principle is all about data hiding (or information hiding). On the other
hand, the Abstraction Principle is all about detailed hiding (implementation hiding).
Using the Encapsulation principle, we can protect our data, i.e., from outside the class,
nobody can access the data directly. We are exposing the data through publicly exposed
methods and properties. The advantage is that we can validate the data before storing and
returning it. On the other hand, using the Abstraction Principle, we are exposing only the
services so that the user can consume the services, but how the services/methods are
implemented is hidden from the user. The user will never know how the method is
implemented.
We can implement Encapsulation by declaring the data members as private and exposing
the data members only through publicly exposed methods and properties with proper
validation. On the other hand, we can implement abstraction through abstract classes and
interfaces.
Abstraction in C# is used to hide unwanted data and shows only the required properties
and methods to the user. Encapsulation in C# is used to bind data members and member
functions into a single unit to prevent outsiders from accessing it directly.
Inheritance in C#
What is the Inheritance ?
Suppose I have a class (A) with a set of members, and I want the same members in
another class (B). One way to do this is to copy and paste the same code from class A into
class B. But if we copy and paste the code, then it is called rewriting the code. Rewriting
the code has an impact on the size of the application. If the size of the application grows,
ultimately, it will affect the performance of the application.
So, if you want to overcome that rewriting process and reuse the code, the best option that
is available for us is Inheritance in C#. Simply. what we need to do is establish a Relation
between the two classes. What relation? Parent/Child relation. Once you establish the
parent/child relationship, then all the members of the parent class (A) can be consumed
under the child class (B). For a better understanding, please have a look at the following
diagram.
Note: In Inheritance, the Child class can consume members of its Parent class as if it is
the owner of those members (except private members of the parent).
In object-oriented programming, when a subclass is instantiated, the constructor of its
parent class (superclass) is called first. This is because the subclass's constructor might
depend on the initialization of the superclass. Therefore, the initialization starts from the
top of the inheritance hierarchy.
If A has a private constructor, B cannot directly inherit from A unless A provides a
protected or public constructor A . In cases where A has a private constructor, and B tries
to inherit from A , the code will result in a compilation error.
Rules in Inheritance :
Rule number 1
Rule number 2
Rule number 3
Rule number 4
Rule number 5
Rule number 6
What is the Default Parent Class in C#?
The Default Parent class is the Object class present in the System namespace.
Advantages of Inheritance in C#
Code reusability: We can reuse the members of the parent class or base class in the child
class or derived class. So, there is no need to redefine the members again in the child class.
So, less code is required in the class.
Types of inheritance in general
Types of Inheritances in C# :
Note: Handling the complexity caused due to multiple inheritances is very complex. Hence
it was not supported in dot net with class and it can be done with interfaces.
the difference between Is A or Has A Relations :
Note: In C#, the IS-A relationship is implemented using Inheritance, and the HAS-A
relationship is implemented using Composition, i.e., declaring a variable. So, whenever we
declare a variable of one class inside another class, we call it a Composition, or you can
say HAS-A relationship.
What the different between specialization and generalization ?
\
What is the purpose of Inheritance ?
So, there are two purposes of inheritance. One is to share features with child classes and the
second one is to achieve polymorphism.