Friday, 28 May 2021

Few interesting C# OOPs concept

 The Definition of Software Architecture


In simple words, software architecture is the process of converting software characteristics such as flexibility, scalability, feasibility, reusability, and security into a structured solution that meets the technical and the business expectations.


A Software Architect can be responsible for various roles & responsibilities in an organization such as:


Communicates with clients to identify software requirements and other needs.

Comes up with optimal technical standards, tools, processes, etc. for the project.

Leads the team of developers and distributes the development tasks accordingly.

Collaborates with other teams, end-users, etc. to deliver high-quality software solutions.

Works on Code Reviewing, Documentation, Troubleshooting, and other related areas, etc.

-------------------------------------------------------------------------------------------------

The following rules must be kept in mind while overloading methods in your C# application. The method signature must be different. Either the number of arguments, type of arguments, or order of arguments must be different. The return type of the methods and access modifier they do not play any role in method overloading


*Return types and access modifiers are not qualifying factors for method overloading.

--------------------------------------------------

Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.

------------------------------------------------

Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding. Can be achieved through base keyword

public class Base

{

    public Base(Parameter p)

    {

        Init(p)

    }


    void Init(Parameter p)

    {

        // common initialisation code

    }

}


public class Derived : Base

{

    public Derived(Parameter p) : base(p)

    {

 

    }

}

-----------------------------------------------------

What is a static constructor?

Static constructors are introduced with C# to initialize the static data of a class. CLR calls the static constructor before the first instance is created.


The static constructor has the following features:


No access specifier is required to define it.

You cannot pass parameters in static constructor.

A class can have only one static constructor.

It can access only static members of the class.

It is invoked only once, when the program execution begins.


------------------------------------------------------

Can you declare an overridden method to be static if the original method is not static?

No. Two virtual methods must have the same signature.

--------------------------

Abstract Class:


A class can extend only one abstract class

The members of abstract class can be private as well as protected.

Abstract classes should have subclasses

Any class can extend an abstract class.

Methods in abstract class can be abstract as well as concrete.

There can be a constructor for abstract class.

The class extending the abstract class may or may not implement any of its method.

An abstract class can implement methods.


Interface


A class can implement several interfaces

An interface can only have public members.

Interfaces must have implementations by classes

Only an interface can extend another interface.

All methods in an interface should be abstract

Interface does not have constructor.

All methods of interface need to be implemented by a class implementing that interface.

Interfaces cannot contain body of any of its method.


** An interface is a contract: The person writing the interface says, "hey, I accept things looking that way", and the person using the interface says "OK, the class I write looks that way". An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern. Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there isn't any expensive look-up to do. It's great when it matters, such as in embedded devices.

Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them. Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It's more about a person saying, "these classes should look like that, and they have that in common, so fill in the blanks!".

----------------------------------------------------------

destructor

A destructor is a special method for a class and is invoked automatically when an object is finally destroyed. The name of the destructor is also same as that of the class but is followed by a prefix tilde (~).


A destructor is used to free the dynamic allocated memory and release the resources. You can, however, implement a custom method that allows you to control object destruction by calling the destructor.


The main features of a destructor are as follows:


Destructors do not have any return type

Similar to constructors, destructors are also always public

Destructors cannot be overloaded.

-----------------------------------------------------------

What is Cohesion in OOP?

In OOP we develop our code in modules. Each module has certain responsibilities. Cohesion shows how much module responsibilities are strongly related. Higher cohesion is always preferred. Higher cohesion benefits are:


Improves maintenance of modules

Increase reusability

-------------------------------------------------------------

What is Coupling in OOP?

OOP Modules are dependent on each other. Coupling refers to level of dependency between two software modules. Two modules are highly dependent on each other if you have changed in one module and for supporting that change every time you have to change in dependent module. Loose Coupling is always preferred. Inversion of Control and dependency injections are some techniques for getting loose coupling in modules.



****Cohesion refers to what the class (or module) can do.

Low cohesion would mean that the class does a great variety of actions - it is broad, unfocused on what it should do.

High cohesion means that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.

As for coupling, it refers to how related or dependent two classes/modules are toward each other. For low coupled classes, changing something major in one class should not affect the other. High coupling would make it difficult to change and maintain your code; since classes are closely knit together, making a change could require an entire system revamp.


------------------------------------------------------------

INTERFACE

An interface is a contract that guarantees to a client how a class or struct will behave. When a class implements an interface, it tells any potential client “I guarantee I’ll support the methods, properties, events, and indexers of the named interface.”


When a class implements an interface, it must implement all the methods of that interface; in effect the class says “I agree to fulfill the contract defined by this interface.”


own language example:

Interface (TV screen with volumme keys. This is an interface it tells to end user that the volume key will help to increase decrese the volume. This is a contract that guarentees of volume operation to end user)


same time the class(who actually implements the interface guarentees that he will develop or fulfill of volume control option that has been committed to end user through interface)

-------------------------

Aggregation refers to an act of grouping items or things as a whole

composition he way in which a whole or mixture is made up. 

No comments:

Post a Comment

Baisic Useful Git Commands

  Pushing a fresh repository Create a fresh repository(Any cloud repository). Open terminal (for mac ) and command (windows) and type the be...