Object Oriented Programming

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Object Oriented Programming : 

Object Oriented Programming

Slide 2: 

A constructor is a special type of method that is invoked when you create a new instance of a class. The name of a constructor is the same as the name of the class that contains it. Constructor

Slide 3: 

The two types of constructors are: Instance constructors: They are called whenever an instance of a class is created. These constructors are used to initialize the data members of the class. Static constructors: They are used to initialize the static variables of a class. These variables are created using static keyword and they store values that can be shared by all the instances of a class. Types of Constructors

Slide 4: 

A constructor is special member function within the class which is executed when an object of the class is created. The Need of Constructors

Static Constructor : 

Like any other static members, we can also have static constructor. A static constructor is called before any object of the class is created. It is usually used to assign initial values to static data member. A static constructor is declared by prefixing a static keywords to the constructor definition. It can not have any parameters. We can not use any access modifier on static constructor. A class can have only one static constructor. Static Constructor

Slide 6: 

A constructor can be modified to accept the user-supplied values at run time. Objects can be initialized using the default constructor with values hard-coded in the program. But there might be a requirement where the variables need to be initialized with user supplied values. Constructors with Parameters

Slide 7: 

Constructors can also be parameterized, and therefore, they can be overloaded. Overloaded constructors are commonly used in C# to provide flexibility while creating an object. Constructor Overloading

Slide 8: 

Destructors are special methods that are used to release the instance of a class from memory. A class can have only one destructor. The purpose of the destructor is to perform the required memory cleanup action. The .NET Framework automatically runs the destructor to destroy objects in the memory. Implementing Destructors

Slide 9: 

A destructor has the same name as its class but is prefixed with a ~ , which is the symbol of tilde. Destructors cannot be inherited or overloaded. Garbage collection is a process that automatically frees the memory of objects that are no more in use. The decision to invoke the destructor is made by a special program of C# known as the garbage collector. The process of garbage collection happens automatically. It ensures that: Objects get destroyed Only unused objects are destroyed Declaration of Destructors

Slide 10: 

C# provides the following methods to release the instance of a class from memory: Finalize(): It is a special method that is called from the class to which it belongs or from the derived classes. The Finalize() destructor is called after the last reference to an object is released from the memory. Dispose(): This method is called to release a resource, such as a database connection, as soon as the object using such a resource is no longer in use. The IDisposable interface contains the Dispose() method. Therefore, to call the Dispose() method, the class must implement the IDisposable interface. Declaration of Destructors (Contd.)

Slide 11: 

Identifying the Life Cycle of an Object Let us understand the life cycle of an object with the help of the following code: using System; //Life Cycle of an Object namespace Objects { class TestCalculator { TestCalculator() { Console.WriteLine("Constructor Invoked"); }

Slide 12: 

Identifying the Life Cycle of an Object (Contd.) The Calc1 object has function scope. Therefore, its constructor is executed after the execution of Main() begins. The destructor of all the object is invoked when the garbage collector is invoked. ~TestCalculator() { Console.WriteLine ("Destructor Invoked"); } public static void Main(string[] args) { Console.WriteLine("Main() Begins"); TestCalculator Calc1 = new TestCalculator();

Slide 13: 

Identifying the Life Cycle of an Object ~Console.WriteLine("Inner Block Begins "); TestCalculator Calc2 = new TestCalculator(); Console.WriteLine("Inner Block Ends"); } Console.WriteLine("Main() ends"); } } } The Calc2 object has block scope. Therefore, its constructor is executed after the inner block begins.

Slide 14: 

C# enables you to create abstract classes that are used to provide partial class implementation of an interface. Abstract classes contain abstract methods, which can be implemented by the derived class. Polymorphism can be implemented by using abstract classes and virtual functions. There are certain rules governing the use of an abstraction class: Cannot create an instance of an abstract class. Cannot declare an abstract method outside an abstract class. Cannot be declared sealed. Abstract Classes

Slide 15: 

Abstract Methods Abstract methods are methods without any body. The implementation of an abstract method is done by the derived class. When a derived class inherits the abstract method form the abstract class, it must override the abstract methods. This requirement is enforced at compile time, and is also called dynamic polymorphism. The syntax for using the abstract method is as follows: [access-modifiers] abstract return-type method name (parameters]); The abstract method is declared by adding the abstract modifier to the method.

Slide 16: 

Virtual Functions When you have a function defined in a class which you want to allow to be implemented by the inherited classes, you can use virtual function. The virtual function could be implemented by the inherited classes in their own way and the call to the method is decided at the run time.

Slide 17: 

Sealed Classes You could restrict users form inheriting the class by sealing the class using the sealed keyword. The sealed keyword tells the compiler that the class is sealed, and therefore, cannot be extended. The following is an example of a sealed class: sealed class FinalClass { private int x; public void Method1() { } } A method can also be sealed and in that case the method cannot be overridden.

Slide 18: 

In Object-Oriented Programming (OOPs), polymorphism allows one interface to be used for multiple functions. Polymorphism reduces the complexity within the functions of a class of a program. Polymorphism can either be static or dynamic. Introducing Polymorphism

Slide 19: 

Static polymorphism refers to an entity, which exists in various forms simultaneously. C# uses approaches to implement static polymorphism. These are: Function overloading: This approach allows using the same name for two or more functions. Each redefinition of a function must use different types of parameters, sequence of parameters, or a number of parameters. Static Polymorphism

Slide 20: 

In dynamic polymorphism, the decision about function execution is made at run time. Dynamic polymorphism is more useful than static polymorphism as it provides much more flexibility for manipulating the objects. C# uses two approaches to implement dynamic polymorphism: Abstract classes: Are the special type of base classes that consist of abstract class members. Virtual functions: Are the functions that do not really exist, however, appear to be present in some parts of the program. Dynamic Polymorphism

Slide 21: 

Interfaces Interfaces define properties, methods, and events, which are known as the members of the interface. Interfaces are fully supported by C#. Interfaces are used when a standard structure of methods is to be followed by the classes, and where classes will implement the functionality. Interfaces separate the definition of objects from their implementation so that the objects can evolve without the risk of introducing incompatibility in existing applications.

Slide 22: 

Working with Interfaces Working with interfaces includes interface declaration and implementation of interface by the classes. You can declare interfaces using the interface keyword. Interface statements are public, by default. You can declare only methods, functions, and properties in interfaces. You cannot declare a variable in interfaces. Interfaces declare methods, which are implemented by classes. A class can inherit from single class but can implement form multiple interfaces.

Slide 23: 

Inheriting Interfaces A class or a structure that implements interfaces also implements the base interfaces of the inherited interface.