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.


Q31-Q35

Q31.  What are SOLID principles?
Q32. Why one should follow SOLID Principles?
Q33. Explain SRP, Single Responsibility Principle with example?
Q34. Explain ISP, Interface Segregation Principle with example?
Q35. Explain OCP, Open Close Principle with example?
--------------------------------------------------------------------------------------------------------------------------
Q31.  What are SOLID principles?

Answer:
There are 5 design principles to make your software flexible and maintainable. 
S - Single Responsibility Principle
O - Open Close Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D - Dependency Inversion Principle
--------------------------------------------------------------------------------------------------------------------------
Q32. Why one should follow SOLID Principles?

Answer:
1. Maintainability - When project matures the maintainability become difficult with time. Old developers are no longer available and documentation are longer in sync. Hence we should design a application which can be easily maintain by new developers.
2. Testability - Test driven application are very important for large scale applications. 
3. Flexibility and Extensibility - During development of project at times the requriments changes completely. Hence our application must be flexible enough to handle the changes. 
4. Loose coupling 
--------------------------------------------------------------------------------------------------------------------------
Q33. Explain SRP, Single Responsibility Principle with example?

Answer:
Single Responsibility Principle 
A class should have one, and only one, reason to change. class should be having one and only one responsibility.
Each class and module must focus only on single task at a time. 
There can be as many members in a class as long as they related to single responsibility. 

Example : we have user login and registration page. There are 4 activitites that can be done
1. User can login. (Login)
2. User can register. (Register)
3. User will get email notification in case of registration. (emailNotification)
4. Log the exceptions. (logs)

There could be two approach to this 1) Create a single Interface will all responsibility 2) create multiple interface keeping together only similar work 
Second approach is SRP

we can create 3 Interface
Interface IUser to implement Login and Register
Interface Iemail to implement emailNotification
Interface Ilogger to implement logs

https://howtodoinjava.com/best-practices/5-class-design-principles-solid-in-java/#:~:text=SOLID%20Principles%20in%20Java%20%5Bwith%20Examples%5D%201%20Single,Segregation%20Principle.%20...%205%20Dependency%20Inversion%20Principle.%20

--------------------------------------------------------------------------------------------------------------------------
Q34. Explain ISP, Interface Segregation Principle with example?

Answer:
Interface Segregation Principle
Clients should not be forced to implement unnecassary methods which they will not use. We should have multiple interfaces than 1 fat interface. 

Example : we have user login and registration page. There are 4 activitites that can be done
1. User can login. (Login)
2. User can register. (Register)
3. User will get email notification in case of registration. (emailNotification)
4. Log the exceptions. (logs)

There could be two approach to this 1) Create a single Interface will all responsibility 2) create multiple interface keeping together only similar work 
Second approach is SRP

we can create 3 Interface
Interface IUser to implement Login and Register
Interface Iemail to implement emailNotification

Interface Ilogger to implement logs


https://www.youtube.com/watch?v=CWrRwC8iB30&list=PL6n9fhu94yhXjG1w2blMXUzyDrZ_eyOme&index=3
--------------------------------------------------------------------------------------------------------------------------
Q35. Explain OCP, Open Close Principle with example?

Answer:
Open Close Principle
Classes and modules  are open for extension but closed for modification. Means any new functionality must be added by adding new classes and modules instead of making changes in old classes and modules. 
The simplest way to implement OCP is to implement the new functionality on new derived class. This will use the concept of Virtual/ Override concept.

example. We have to calculate salary of an employee based on weather he is permanent employee or temporary employee. 
1st way is to create a class and a method to calculate bonus. put if condition within bonus methods if employee is permanent/ temporary. 
2nd way (OCP). Create an absctract class with bonus method in it. make it virutal. create two more classes. one of permanent employees and 2nd of temporary employees. inherit above abstract class and implement the method as per requirement. 


https://youtu.be/wo06oCBuYYI