Search This Blog

Q36-Q40

Q36. Explain LSP, Liskov substitution Principle with example?
Q37. Explain DIP, Dependency Inversion Principle with example? 
Q38. What is Static keyword in C#?
Q39. What is IOC design principle?
Q40. Explain cycle of Garbage collector. 
-------------------------------------------------------------------------------------------------------------------------
Q36. Explain LSP, Liskov substitution principle with example?

Answer:
For example and implementation check out:
easy and interesting example

Liskov Substitution Principle:
  • Derived types must be completely substitutable for their base types. 
  • It is extension of Open Close Principle.
  •  Basically, any child class should be able to do anything the parent can do.

Implementation guidelines
  • No new exceptions can be thrown by the subtype. 
  • Clients should not known which specific subtype they are calling. 
  • New derived classes just extend without replacing the functionality of old classes. 

this is the same example of concept B() = new D();
https://youtu.be/gnKx1RW_2Rk

-------------------------------------------------------------------------------------------------------------------------
Q37. Explain DIP, Dependency Inversion Principle with example? 

Answer:
Dependency Inversion Principle: (DIP)
High level modules should not depend on low level modules, but both should depend on abstraction. 
Abstraction should not depend on details, but details should depend on abstractions. 

Adaptor design pattern is an example of DIP. 
https://youtu.be/5WHKNOTqwsA

-------------------------------------------------------------------------------------------------------------------------
Q38. What is Static keyword in C#?

Answer:

The static keyword in C# language is used to declare static classes, static class members , static properties and even static constructor.

Static class
  • Static class contain only static variables, static methods and static constructor. 
  • Object cannot be created for static class. 
  • Static class are sealed class means you cannot inherit static class from another class. 
  • Data members of static class can be directly accessed by using its class name. 
  • Static class and its members remain available till the lifetime of application. 
Non-Static Class
  • Non-Static class can have one or more static members and properties
Static Properties- Non-Static Class
  • Static properties or fields of a non-static class is shared across all the instances. So, changes done by one instance would reflect in others.
Static Methods- Non-Static Class
  • Static methods can be called without creating an object. You cannot call static methods using an object of the non-static class.
  • The static methods can only call other static methods and access static members. You cannot access non-static members of the class in the static methods.
Static Constructor - Non-Static Class
  • A non-static class can have both static and non-static constructor. Static constructor would be called once during the application starting but we can keep on calling non static constructor as much we want. 

-------------------------------------------------------------------------------------------------------------------------
Q39. What is IOC design principle?

Answer:
As the name suggests, it is used to invert different kinds of controls in object-oriented design to achieve loose coupling. Here, controls refer to any additional responsibilities a class has, other than its main responsibility. This include control over the flow of an application, and control over the flow of an object creation or dependent object creation and binding.
IoC is all about inverting the control. To explain this in layman's terms, suppose you drive a car to your work place. This means you control the car. The IoC principle suggests to invert the control, meaning that instead of driving the car yourself, you hire a cab, where another person will drive the car. Thus, this is called inversion of the control - from you to the cab driver. You don't have to drive a car yourself and you can let the driver do the driving so that you can focus on your main work.

-------------------------------------------------------------------------------------------------------------------------
Q40. Explain cycle of Garbage collector. 

Answer:
Garbage Collection is the process of running the garbage collector to reclaim memory that is no longer accessible to the program. his process happens entirely in the background (by the CLR), and you never have to know it.

Garbage collection occurs when any one or more of the following conditions is true :
  • When the system runs out of physical memory.
  • When the GC.Collect method is called manually.
  • When allocated objects in memory need more space
GC supports the concept of generations. It helps to organize short-lived and long-lived objects in a managed heap. There are three generations:

Generation 0: When an object is allocated on the heap, it belongs to generation 0. It is the young generation, which contains short-lived objects like temporary variables. If newly allocated objects are larger in size, they will go on the large object heap in a generation 2 collection. GC occurs mostly in generation 0.

Generation 1: When objects survive from a garbage collection of generation 0, they go to generation 1. Objects in generation 1 serve as a buffer between short-lived and long-lived objects.

Generation 2: When objects survive from a garbage collection of generation 1, they go to generation 2. Objects in generation 2 serve as long-lived objects. If objects still survived in generation 2, they remain in generation 2 till they’re alive.


No comments:

Post a Comment