Fahad Hussain MCS, MSCS, Dae (Cit) : Computer Science Instructor
Fahad Hussain MCS, MSCS, Dae (Cit) : Computer Science Instructor
Fahad Hussain MCS, MSCS, Dae (Cit) : Computer Science Instructor
Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)
The main aim of OOP is to bind together the data and the functions that operate
on them so that no other part of the code can access this data except that
function.
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
1. What is class
2. What is object
3. What is namespaces
4. What is function/Method
5. What is Constructor and its types
6. What is Destructors
In simple words, it is a prototype or building block structure for starting work in the OOP approach!
Object:
An Object is an identifiable entity with some characteristics and behavior. An Object is an
instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
Namespaces
Namespaces in C# are used to organize too many classes so that it can be easy to handle the
application. We use System.Console where System is the namespace and Console is the
class. To access the class of a namespace, we need to use namespacename.classname. We
can use using keyword so that we don't have to use complete name all the time.
Copy Constructor
This constructor will creates an object by copying
variables from another object. Its main use is to
initialize a new instance to the values of an existing
instance.
Points To Remember :
It can’t be called directly.
When it is executing then the user has no control.
It does not take access modifiers or any parameters.
It is called automatically to initialize the class before the first instance created.
• Constructor of a class must have the same name as the class name in
which it resides.
• A constructor can not be abstract, final, and Synchronized.
• Within a class, you can create only one static constructor.
• A constructor doesn’t have any return type, not even void.
• A static constructor cannot be a parameterized constructor.
• A class can have any number of constructors.
• Access modifiers can be used in constructor declaration to control its
access i.e. which other class can call the constructor.
• In c#, destructors can be used only in classes and a class can contain only one destructor.
• The destructor in class can be represented by using tilde (~) operator
• The destructor in c# won’t accept any parameters and access modifiers.
• The destructor will invoke automatically, whenever an instance of class is no longer needed.
• The destructor automatically invoked by garbage collector whenever the class objects that
are no longer needed in application.
Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
1. Public: The type or member can be accessed by any other code in the same assembly or another assembly that
references it.
2. Private: The type or member can be accessed only by code in the same class or struct.
3. Protected: The type or member can be accessed only by code in the same class, or in a class that is derived from
that class.
4. Internal : The type or member can be accessed by any code in the same assembly, but not from another assembly.
5. Protected Internal : 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.
6. Private Protected: It access modifier is a combination of the private and protected keywords. We can access
members inside the containing class or in a class that derives from a containing class, but only in the same
assembly(project). Therefore, if we try to access it from another assembly, we will get an error. (C# 7.0)
In Java
Public Private Protected Default
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Inheritance
Object-Oriented programming language is code reuse. This reusability is possible due to the
relationship b/w the classes. Object oriented programming generally support 4 types of
relationships that are: inheritance , association, composition and aggregation. All these
relationship is based on "is a" relationship, "has-a" relationship and "part-of" relationship.
Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)
try – A try block is used to encapsulate a region of code. If any code throws an exception within that
try block, the exception will be handled by the corresponding catch.
catch – When an exception occurs, the Catch block of code is executed. This is where you are able to
handle the exception, log it, or ignore it.
finally – The finally block allows you to execute certain code if an exception is thrown or not. For
example, disposing of an object that must be disposed of.
throw – The throw keyword is used to actually create a new exception that is the bubbled up to a try
catch finally block.