You are on page 1of 14

2-04-015

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.

What is the benefit of OOP over procedural programming?


Procedural programming is based upon the modular approach in which the
larger programs are broken into procedures. Each procedure is a set of instructions
that are executed one after another. On the other hand, OOP is based upon objects.
An object consists of various elements, such as methods and variables.

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

Walk, doing examination, talk etc. are behaviors /methods

In OOP objects are structured in terms of classes

Difference between object refrence veriable and object

Student s1 = new student here s1 is an object reference veriable to an object on the heep.

A parent class refference veriable can point to a child class object

Fields
Fields are attributes of an object in a class like string FirstName, string LastName, int age.

This key word


This key word is used to refer to instance of this class
In C# objects are reference types and other data type variables are value types.
When an object is created using the new operator, memory is allocated for the class in the heap,
the object is called an instance and its starting address will be stored in the object in stack
memory.

Class?
It is a facility provided by OO model to create new types according to our
requirement. “Class is a user defined data type”

Class contains objects and objects contains attributes and behavior.

Some more details


So according to the defined attributes and behaviors we can create so many objects that is also called
instantiate an object. Look at This program

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.

Basic concepts of OOP?


Abstraction

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”

So according to abstraction we’ll choose only those attribute and behavior of an


object that are relevant to our problem.

Encapsulation:
Encapsulation is the packing of data and behavior in single component
Inheritance:
Inheritance represents IS A RELATIONSHIP

For example a student is a person

In general we can say DERIEVED CLASS IS A KIND OF BASE CLASS

Or a derived class is a specialization of its base class like surgeon is a specialization of


doctor, surgeon contains all attributes and behaviors plus surgeons attributes and
behaviors.

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

Ability to redefine methods for derived classes

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]:

picture ..which has overriden ur dad's old bike

Why there is a need of method overloading


(I)Because without overloading we have to remember all method names with signature and
(ii)We achieve consistency in the naming of methods / functions which logically perform
very similar tasks, and differ slightly in by accepting different parameters. For example,
MessageBox.Show in .NET has 21(!) overloads. Imagine having 21 methods that all do the same
thing.
MessageBox.ShowWithTextOnly
MessageBox.ShowWithTextAndCaption
MessageBox.ShowWithTextAndCaptionsAndIcon
MessageBox.ShowWithTextAndCaptionsAndIconAndButtons

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

Less commonly known as operator ad hoc polymorphism—is a specific case of


polymorphism, where different operators have different implementations
depending on their arguments. Operator overloading is generally defined by
the language, the programmer, or both.

What is virtual key word?

Virtual keyword is used for method overriding, which method we want to override in
child class we declare it virtual in base class.

Question: why we’ll override child class method

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

Constructor is a special method contains following properties

Constructor is used to initialize the class fields

(i) Same name as class


(ii) No return type
(iii) May or may not have parameters
(iv) No need and to calling constructor like other methods it invokes
automatically whenever we we’ll create an object

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

Customer C1 = new Customer(“umair”, “arshad”)

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

Constructor cannot take any parameter.

Instance of this class/ to an object of this class

I have created a project for understanding classes, constructors, methods and objects
which is located here below

F:\Renewal\KudvenkatC#\CsharpOOPtraining\ClassConstructorPart19

Resources for preparing this

https://www.youtube.com/watch?v=e4pxTC1YBsc.

vu cs201 part 26 class

->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.

Abstract classes and interfaces all about them


Interface
“Interface is just like a class (syntactically) but the difference is that it
contains only declaration not implementation”

Some details about interfaces

->Interface members don’t contain access modifiers, they are public by default.

->Interfaces can’t contain fields


Why interface

The core purpose of interface is to build loosely coupled applications which


means change in one component has minimal effect on other components of
application. By using Interfaces testability and extensibility becomes easy.

How interface creates loosely coupled application

In bellow OrderProcessor class is dependent on TextCalculator class i.e


OrderProcessor uses field/property of type TextCalculateor this type of coupling is
tight coupling,

Whereas if we declare TextCalculator as Interface then this is loose coupling


because ITextCalculator is just declaration, it has no code, so we provide the
implementation of that interface by other class

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

Public abstract class A


{
// Class members here.
}

Concrete class

Concrete class is a derived which provides the implementation of abstract class


Reference
Interface vs abstract class and the Difference between them

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)

When to use interface and abstract class


Read this

Abstract Class & Interface: Two Villains of Every Interview

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 class/ data member/methods?


There is a need of thorough understanding of this concept at class and
data or method level and write the concept here for further review

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

Student s1 = new student();

And then call Display method with s1 instance as

S1.Display();

A static class is similar to a class that is both abstract and sealed.


There are two types of data members of a class static and instance, instance
members belong to specific instance of class whose value changes per object
bases whereas the value of static data member does not change per object
bases .for example in circle class we should declare pi data member as static an
radios as instance

When we use static keyword in .Net ?

We can use them when we need to maintain information


applicable to the entire class

Characteristics of static class:

A static class is similar to normal class with these differences

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 are constant data members?


Constant is something that doesn't change. We use the keyword const to make
program elements constant. Constant variables must be initialized while
declared.

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.

In object-oriented programming, a friend function, that is a "friend" of a given class, is


a function that is given the same access as methods to private and protected data. A friend
function is declared by the class that is granting access, so friend functions are part of the
class interface, like methods.

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

only can access public or internal methods or objects.

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

oriented) while C follows procedural style programming.

(ii) In C data security is less, but in C++ you can use modifiers for your class

members to make it inaccessible from outside.

(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.

Most of the times compilers will do optimization to the code to speed


up the program, and volatile key word prevent the compiler to do any
optimization.
Association, aggregation and composition

Association

Association is a weak relationship between objects like room and furniture


Both can exists without each other

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

eliminate one thing from furniture then it will separately exist.

Composition

It is a strong relationship like human body and part objects. A Company is

an aggregation of People. A Company is a composition of Accounts. When a

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.

You might also like