Abstraction Encapsulation
Abstraction
refers to modelling of complex entity through abstracts taking only relevant details and skipping irrelevant details.
It hides implementation details. It helps in reducing code complexity and standardization.
Achieved using Abstract classes and Interfaces
For example creating interface of product service and only declaring what it can do. How is later implemented by relegated classes
Encapsulation
Refers to wrapping data and behavior of entity and giving public access points to these.
Achieved using class access Modifiers getters setters
It helps in data integrity and modularity. Avoids accidental overwrites of data through validations in setters.
For example creating Patient class to protect PHI data
Inheritance
Extending existing types to create specialised types giving hierarchy.
It helps in reusability
For example creating admin from user. Admin is a type of user
Polymorphism
Refers to ability of entity to take different forms based on context.
Achieved using overloading and overriding
Helps in generic coding flexibility
For example a search function being able to function differently based on filter types
Over loading vs Over ridding
Shadowing
Method Hiding
Child methods will be hidden from parents during polymorphism
Child methods will not be invoked by parent class during polymorphism
Only happens when parent is pointing towards child instance
It is implementation using new keyword on same signature method of parent in child class
Useful in liskov problem when child doesn’t implement everything parent does.
Operator overloading
What is abstract classes
Partially defined parent class. Some implementations defined and some which is left for child to implement
It can be a base class but can’t have instances. Runtime safety.
Create child, implement abstract to create instances
Abstract class. Abstract method
Why do we need shadowing
Hiding is a hack to achieve safe polymorphism by following LSP when a child class doesn’t implement all methods of base class and needs to be hidden so that base pointing to child can’t invoke it and use the base method.
Shadowing is a reactive measure/hack when face a liskov problem due to child not implementing all methods of parent which gives unexpected behaviour runtime errors
Hack when child class doesn’t implement all methods of parent
Liskov problem and happens due to wrong abstraction . It causes unexpected or runtime errors
Shadowing Hiding vs Overriding
Overriding is type of polymorphism. Parent class can invoke child methods which are overriden when parent points to a child instance.
In shadowing, parent class can’t invoke child method because it is hidden from the parent. Only instance child class can invoke those methods
Nested class use cases
Class definition inside another class
Deeper logical group classes than Namespaces
When nested class is only useful to enclosing class.
For example prescription validation is only rental order
Can nested class access outer class variables
No. Only structural connection. No instance connection.
We need to pass an instance and then access
Can nested have any access Modifiers in nested class
Technically yes.
If the usage to is to only logical group : public
If only used for related and hidden : private
Partial classes
Can be written in two different physical files but compiler will compile into one class
We need to use partial class
Useful when we need to organise complex code into different files.
DO NOT TELL PARALLEL DEVELOPMENT BY MULTIPLE
Abstract method is virtual method ?
Yes. Child can override. It is implicitly virtual
Is it compulsory to implement abstract methods
Yes. If not, compile error
Why can’t simple base class replace abstract class
We can’t leave some without implementation. We have to write dummy implementation which is tech debt. Not clean code
What are interfaces
Interface is a contract
With the consumers of the interface.
Interface supports multiple inheritance which Helps in versioning by implementing multiple interfaces.
Helps in creating loosely coupled applications
Consumer MUST implement all properties and methods defined by interface. Else compile error
Contract between developer who is writing contract so he can use the concrete classes safely because another created the concrete strictly following contract rules. Else compile error and impact is shown
Contract creator will use interface reference = to concrete class and invoke the methods of interface
Private methods ?
Logic
Only pure signature
No access Modifiers. Private gives Compile error
Now default implementation can be given
Interface vs Abstract class
Interface only signatures but abstract can some definition some signature
Interface to plan abstraction. After defining Interface we can create abstract class if there are related classes implementing it and have same logic.
Abstract class to implement abstraction. Common logic of related classes go in abstract class where the class itself shouldn’t exist as an entity in code.
Interface supports multiple inheritance but abstract only supports single inheritance.
Constructor and order
Special methods which are automatically invoked when instance created. Used to initialize variables
Parent then child constructor order
What is the goal of SOLID principles
Minimize dependencies
Write reliable code
Code easy to understand maintain test and refactor
Initializer order
First child then parent
Static constructor order
Child then parent
Then
Instance constructor of child then instance constructor of parent
When static constructor fires
First time the class is accessed