Object Oriented Programming Concept.
Abstraction
show surface of relevant data about object.
definition of the method which the developers use it for their
further implementaion by overriding the methods in the class.
An instance of an abstract class cannot be created.
Abstract can only be inherited
Encapsulation
mechanism of organizing code into small collection(class) of relevant
member/method.
hiding of member and method within an object.
Inheritance(Code reusable)
Process of deriving class from base class and inherit its attributes.
Derived class inherit base class member/method without redefine.
Polymorphism
Allow methods to be represeted in multiple forms with different params
each derived class has its own implementation of function which is same
as base function.
CLASS (member/method)
-Member (instance/static) : field , properties(Get/Set)
-Method : method, constructor, event
Abstract class (Incomplete class, so can't be instantiated)
-Must have at least one abstract method, can have non-abstract method with implementation.
-Abstract method has no body, only defnition
-Abstract method must be inherited and implemented in derived class
Interface Class
-Not a class
-All methods are implicitly abstract and no implemention.
-Abstract class with ALL method are abstract (public), all method must be overrrided
-A class can implement > 1 interfaces, But can only inherit 1 abstract class
Virtual method
-Virtual method in base class allow it to be override in derived class
eg. protected virtual int met_name() (b)
protected override void met_name() (d)
Sealed Class
- Class CAN NOT be inherited. NOT GOOD used for base class
- Prevent inheritance.
Internal Class
-the class that can't be accessed from anot
Protected internal class
-the class can only be accessed from same assembly.
-but i can be acccessed from its derived class at the other assembly......
ReadOnly
-Its value can only be set either in declaration OR Constructor.
-Not in class method OR constructor calling method
Overload methods
-A class contains two methods with same name and return type, BUT different parameters
-CAN'T have same parameter number, BUT different return type
-A compiler automatically select the most appropriate method based on the parameters supplied.
Base Class method can be accessed without creating instance.
- It is static method . classname.method()
- It is being called within derived class IF no method name being declared the same in base class.
- If there is a method (virtual/ override), it can be called in derived class using base.method().
Access modifier (public, private, protected, internal, protected internal) (Inheritance)
1. Can privated & protected method of A class be accessed by B's
b outside the class?
* No, only Public can be accessed by B's b outside the class.
2. Can protected method of A be called in B?
* Yes..it doesnt' need to have object to call.
3. Can A's a access B's public method?
* No, unless B's method is overried from A's virtual method
If outside the class, A a=new B() , a has reference to class B. ( A's obj can access both A and B methods/member)
4. Can B's method be same name as A's method? eg. public void print()
* No , unless B's print() has NEW keyword to hide A's print()
eg. public new void print()
5. In B's method , can we call A's print() if B has NEW void print()
as A?
* Yes, by using Base.print()
6. Member/Method of Base class can be accessed in Derived class without
instance object?
* Yes, but access modifer must be Protected or Public.
outside class, object can only access PUBLIC member or method.
7. When derived class object is instantiated. Which class constructor being
called first?
* Base class constructor(non-parameter constructor)
first followed by derived class constructor even if it has parameter.
8. When Base Class (B) has virtual method and Derived class (D) override method,
an object of Deriveclass is instantiated and cast to BaseClass, which method will be called when
object access method.
eg.
Derived D=New Derived();
Based B= (Based) D; /* Based B=new Derived()*/
B.method();
* Derivedclass method will be called (Virtual/ Override)
* BaseClass method will be called ( void/new)
* B can't access members of Derived Class (No Virtual/Override)
Polymorphism
1. When can a derived class override a base class member?
A derived class can override a base class member only if the base class member is declared as virtual or abstract.
2. What is the difference between a virtual method and an abstract method?
A virtual method must have a body where as an abstract method should not have a body.
3. Can fields inside a class be virtual?
No, Fields inside a class cannot be virtual. Only methods, properties, events and indexers can be virtual.
Operator
&= bitwise AND
1 & 1 => 1 , otherwise 0
^ =bitwise exclusive
OR
If A & B != then =>1 , otherwise 0
eg. 1 ^ 1=0 , 1^ 0 => 1
| =bitwise inclusive
OR
if 0 | 0 => 0, otherwise 1
eg. 1|0 => 1 , 1|1=>1 , 0|1 =>1
255 = 11111111
&
122 = 01111010
------------------
ANS : 01111010 (122)
|