Object Oriented Programming Notes
Object Oriented Programming Notes
What is OOP?
Object-oriented programming (OOP) is a programming language model organized
around objects rather than "actions" and data rather than logic. Historically, a
program has been viewed as a logical procedure that takes input data, processes it,
and produces output data.
Access modifiers are not used in procedural programming, which implies that the
entire data can be accessed freely anywhere in the program. In OOP, you can
specify the scope of a particular data by using access modifiers - public,
private, internal, protected, and protected internal.
Object?
Any tangible or intangible entity (real world entity which can have some
characteristics or which can perform some work) can be considered as an object in
OO model.
Object can have some attributes and depicts some behavior i.e. in case of student
Name age, roll number etc. are attributes of student object and
Student s1 = new student here s1 is an object reference veriable to an object on the heep.
Fields
Fields are attributes of an object in a class like string FirstName, string LastName, int age.
Class?
It is a facility provided by OO model to create new types according to our
requirement. “Class is a user defined data type”
And by object reference variable and dot operator we can call any function from class s1.Display() in
the above example.
Class is the blueprint for objects and objects are structured in terms of class.
Encapsulation
Inheritance
Polymorphism
Abstraction:
Abstraction is the process of generalization
In OOP abstraction says that “include only those details to the system that are
required to make the system functional”
Encapsulation:
Encapsulation is the packing of data and behavior in single component
Inheritance:
Inheritance represents IS A RELATIONSHIP
Syntax
// Derived class
class Rectangle: Shape
{
}
Polymorphism:
In general polymorphism means different forms of single entity, just like carbon can have
two forms coal and diamond. In the same way in OOP a “different behavior of a method is
call polymorphism
It allows you to invoke derived class methods through base class reference at runtime
There are most famously two types of polymorphism that are listed below but there is
another type called ad-hoc polymorphism/operator overloading
Method overloading
Static polymorphism/compile time polymorphism: Commented [U1]: in compile time polymorphism the
Methods with the same name but different signature in the same class is target function is selected at compile time, means
that the compiler at compile time very well know that
called method overloading which function is going for calling
Your dad has a bike and ur his son want to use his bike but it is outdated and u want to
modify it the way you want to and ride it...that is, ur dad(class) has a function called Bike and Commented [U2]: Return type, number of parameters, type of
parameters and sequence of parameters
ur using the same Bike(function) but modified/changed and ur using it(overriding). Same
bike but now it is changed.... and when we say son it is ur modified bike that shud come into Commented [U3R2]:
Suppose you create a method sum(int i, int j), which add two number.
After a while you realize that you require a method for adding 3 numbers or 4 numbers etc.
so it will make a requirement that there is lots of methods with different number of arguments and
performing same kind of functionality on these argument.
Without overloading you have to remember the name of method which is having exact set of
parameter.
But using overloading makes it simple just one name with different parameter.
int sum (int i, int j) {}
int sum (int a, int b, int c) {} for more understanding have a look at Program at This Link.
Method overriding
Dynamic polymorphism/runtime polymorphism/ late binding
In simple words redefining base class methods in child class is method overriding
Methods with the same name and same signature but in different classes
(Base and its child classes) is called method overriding
Virtual keyword is used in base class method and overrode is at child class as Commented [U4]: // Base Class
class A
Method overriding involves in inheritance
{
We can call base class method from child class object because all the base class public virtual void show()
methods will be present at its child classes {
Console.WriteLine("Hello: Base Class!");
A base class reference variable can point to a child class object and vice versa Console.ReadLine();
means we can assign a child class object to a base class reference variable. }
}
The invoked method version is determined by the class object. If the child class object is
used to invoke the method, then the child class version of the method is executed. If the // Derived Class
parent class object is used to invoke the method, then the parent class version of the
class B : A
method is executed {
public override void show()
{
Console.WriteLine("Hello: Derived Class!");
Why there is a need of method overriding or why we redefine base class method in child Console.ReadLine();
class }
}
Override base class method in child class means we change the implementation of base class method in Commented [U5]: For example Student is a class
child class (when we inherit class B from class A that means B contains all methods and attributes of class A plus some new things of B Student s1 = new student();
So s1 is an instance of class or we can say s1 is an object reference
because B is a specialization of A.) for the sake of understanding we take an example. We have a base class called variable
Employee containing the method CalculateSalary(); salary calculation is done by only basic salry. Know
we create a child class called sales employee now in that class we will override the CalculateSalary();
method is basic salary + bonus. For more understanding Watch this.
Operator overloading
Virtual keyword is used for method overriding, which method we want to override in
child class we declare it virtual in base class.
http://www.onlinebuff.com/article_oops-principle-polymorphism-in-c-with-an-
example_17.html
Method hiding
NOTE: if base class and its child class has same methods (same names and same signature) then base
class method will be hide.
Cross question: when hide if we call the method from child class reference variable I think then hide.
Constructor
Constructor is used to initialize class fields
It contains the same name as class, it has no return type but can take parameters
After watching kudvankat tutorial about classes and constructors I concluded tha
constructor is used to assign the value of object’s attribute. In other words initialize
class fields. For example we have a custormer class with attributes firstName and
LastName. We assign the values for these attributes for newly created object of
customer class. In main method we create instance of class as
Destructor
It is used to clean up the resources that class holds for its life time. Destructor have
the same name as that of class with ~ sign
I have created a project for understanding classes, constructors, methods and objects
which is located here below
F:\Renewal\KudvenkatC#\CsharpOOPtraining\ClassConstructorPart19
https://www.youtube.com/watch?v=e4pxTC1YBsc.
->The C# language doesn't allow multiple inheritance, which is the ability to create
a class based on more than one class. Multiple inheritance is allowed only if the
bases are interfaces. To create multiple inheritances, separate the names of
interface, with a comma.
-> We know that you cannot declare fields like those we have used in other
classes. Instead, if you want some type of member variable, you can create a
property. If you create a property in an interface, you cannot define that property.
->Interface members don’t contain access modifiers, they are public by default.
Link
Interface Syntax
Abstract classes?
An abstract class is meant to be used as the base class from which other classes are derived.
The derived class is expected to provide implementations for the member functions that are not
implemented in the base class. A derived class that implements all the missing functionality is
called a concrete class Read more about abstract class
Concrete class
Read
There is a link
Why there is a need of abstract classes in OOP i.e. the core purpose of abstract classes
Abstract classes in OOP is actually carries on the abstraction phenomenon of OOP. Abstract class is
actually the abstraction at class level. We know abstract class is for base class so in that base class we
draw the main layout or can say Skelton of our program and in derived classes from that base class
implements our solution.
Cross question: now the question is the why we need interface however we already have abstract
class concept the answer is some of functionalities that abstract class not fulfils. I.e. multiple inheritance
some differences between the two instead of the same basic purpose (abstraction and separation of
declaration and implementation)
Signature of method?
(i) Name of method (ii) its return type (iii) Type, number and sequence of
parameters.
Method Hiding
“Method hiding is the concept of hiding base class method from its child class it is
achieved using new key word in child class method” Read more
Static keyword:
Static keyword. Static Classes and class members are used to create data members and Commented [U6]: Data members and member functions of
class
methods that can be accessed without creating an instance of the Class. ... When we
declared a variable or method as static, what really happens is that all the instances of the
class share the value of same static variable.
For example take a look at This link suppose the Display function is static then we’ll call the
that function as
Student.Display();
But if it’s not static then we’ll have to create first instance of student class as
S1.Display();
1. Contains only static members means all the methods and members must
be static
2. Cannot be instantiated by using new keyword
3. By default it is sealed class and therefore cannot be inherited.
4. It can have default constructor or
5. Can have only one constructor without any parameter
6. Access modifiers are not allowed on static constructors
7. Cannot have instantiate constructors
8. Methods can be called by using class name dot (.) method name.
http://www.c-sharpcorner.com/UploadFile/36bc15/static-keyword-in-C-Sharp/
http://csharp-video-tutorials.blogspot.com/2013/08/part-20-static-and-instance-class.html
New operator?
New operator is used to dynamic memory allocation. Commented [U7]: I not understand by dynamic memory
allocation and what’s the antonym of it by concept is it is it static
memory allocation
This operator?
This pointer is a pointer accessible only within the non static member functions
of a class. It points to the object for which the member function is
called. Static member functions do not have this pointer.
What is prototype?
A function prototype or function interface is a declaration of a function that
specifies the function's name and type signature (arity, parameter types, and Commented [U8]: Arity means: Number of
/parameters/arguments.
return type), but omits the function body
Friend functions?
There are no friend functions in c#. It’s a c++ paradigm question. And I have to know that the friendship
is one way or two way.
Access specifiers
Access Modifiers (Access Specifiers) describes the scope of accessibility of an Object and its
members. All C# types and type members have an accessibility level. We can control the scope
of the member object of a class using access specifiers
public :
private :
protected :
internal :
protected internal
Protected.
Members declared as protected cannot be accessed from outside the class except a
child class. This access specifier has significance in the context of inheritance. Note:
the question is that can base class access any object or method of its child class or
Internal:
The internal access modifiers can access within the program that
contain its declarations and also access within the same assembly
level but not from another assembly.
Protected internal
The "protected internal" access modifier is a union of both the "protected" and "internal"
modifiers.
The type or member can be accessed by any code in the assembly in which it is
declared, OR from within a derived class in another assembly. Access from another
assembly must take place within a class declaration that derives from the class in which the
protected internal element is declared, and it must take place through an instance of the
derived class type.
Cross Question:
The above definition not satisfying because is internal can access anywhere in-between an assembly
then why we need protected
Cross question:
Access modifiers I saw applied in class level mostly not in object/method level.
What is difference between C and C++?
(I) C++ is Multi-Paradigm (not pure OOP, supports both procedural and object
(ii) In C data security is less, but in C++ you can use modifiers for your class
(iii) C follows top-down approach (solution is created in step by step manner, like
each step is processed into details as we proceed ) but C++ follows a bottom-up
approach ( where base elements are established first and are linked to make
complex solutions ).
Inline functions?
Inline functions are special functions, for which the compiler replaces
the function call with body/definition of function. It is a request to the
compiler to make it inline.
What is the use of volatile keyword in c++? Give an example.
Association
Aggregation
Aggregation is special form of association in which ownership exists but life cycle
doesn’t depend each other if one object destroy the others life would not affect
Like furniture. Furniture contains many things like sofa, bed, dressing etc if we
Composition
Company ceases to do business its Accounts cease to exist but its People continue to
exist.
Type
Struct
Why are strings in C# immutable?
Immutable means string values cannot be changed once they have been created. Any modification to a
string value results in a completely new string instance, thus an inefficient use of memory and
extraneous garbage collection. The mutable System.Text.StringBuilder class should be used when string
values will change.