OOPS Concepts
Types of Variables in C# j
1. Non Static / Instance variables
If variables are declared with out static keyword they become instance variables
Instance is required for accessing Instance variables because memory gets allocated at the time instances are created.
Instance variables are initialized from 0 to n times (depending upon instance variables creation) in life cycle of a class
2. Static Variables
if a variables is declared explicitely using static keyword or declared inside a static block
can be printed directly as declared at the time of class initialization
Static variables are initialized only one time in life cycle of a class
3. Constant
declared with keyword const and can’t be modified once after the declaration, so its must to intialize constant varibles at the time of declaration
4. Readonly
Inheritance
1. parent class constructor should be accessible to child class, i:e constuctor of base should be public for inheritance to work only becuase of which parent class variables will be initialized and can be consumed further
child class consutructor can remain private till its not derived (but good if its declared public for the sake of good coding practice)
2. Parent can’t access child class methods
3. parent class varibles can refer child class instances, but from that reference variable child class methods can’t be called.
4. Evey class in .net wheather predefined or userdefined inherits from obhect class (equals, gethascode, tostring, gettype), object class is also called defaut parent class
5. Types of inheritanes (single, multiple)
6. in c# there is no supoort of multiple inheritances through classess
7. child class constructor will implecitely call base class constructor if its parameterless but when paretn class constructor is parameterized it has to be explicitely called using base keyword
Method Overloading (Multiple behaviour)
1. multilple method declaration in a class with same name but with diffrent parameter type and number, it don’t consider return type.
2. Method is selected on the basis of parameter type3.
3. method overloading is required for polymorphism(oops feature), so that behviour can be diffrent for diffrent input from same method name.
4. It’s an approch of defining a method with multiple behaviour, where behavious is dependent of no of parameters and their type
Method overiding (changing behviour of parent method if child don’t like it, becuse parent has given you and opportunity for that using virtual keyword)
1. Approch of reimplementing a parent class method under the child class with the same signature.
2. while overiding child needs to take permission from parent, which can be given by parent by declaring the method as virtual in parent (virtual means overridable)
and method can be overriden in chile using override modifier along with method name
3. after overiding child method is called (changing the behaviour of parent class uder child class)
Method hiding/shadowing (without permission)
new keyword