SlideShare uma empresa Scribd logo
1 de 20
Oops
Class – collection of objects
Object – instance of a class
Encapsulation – process of binding data members and member functions into single un
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class Program
{
static void Main(string[] args)
{
rectangle r = new rectangle();
r.length = 3.5;
r.width = 4.6;
r.Display();
}
}
}
Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features.
.Net has five access Specifiers
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member
functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member func
Public
A1 A2
C1 C3
√ √
C2 C4
√ √
Protected internal
A1 A2
C1 C3:C2
√ √
C2 C4
√ ×
Private
A1 A2
C1 C3
√ ×
C2 C4
× ×
Protected
A1 A2
C1 C3
√ ×
C2:C1 C4
√ ×
Internal
A1 A2
C1 C3
√ ×
C2 C4
√ ×
Inheritance:
Inheritance is a process of deriving the new class from already existing class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape
{
public int getArea()
{
return (width * height);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.setWidth(9);
r.setHeight(5);
r.getArea();
Console.WriteLine(r.getArea());
}
}
}
Polymorphism:
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is of two types:
Compile time polymorphism/Overloading – method name must be same parameters and return type may be varied
Function overloading
Operator overloading
Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
Function Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i);
}
void print(double f)
{
Console.WriteLine("Printing float: {0}", f);
}
void print(string s)
{
Console.WriteLine("Printing string: {0}", s);
}
class Program
{
static void Main(string[] args)
{
Printdata p = new Printdata();
p.print(2);
p.print(3.6);
p.print("pavan");
Console.ReadKey();
}
}
}
}
Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Box
{
private double length; // Length of a box
private double width; // Width of a box
private double height; // Height of a box
public double getVolume()
{
return length * width * height;
}
public void setLength( double len )
{
length = len;
}
public void setWidth( double wid )
{
width = wid;
}
public void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.width = b.width + c.width;
box.height = b.height + c.height;
return box;
}
}
class Program
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setWidth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setWidth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}
Runtime polymorphism/Overriding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class BaseClass
{
public virtual void Method1()
{
Console.Write("Base Class Method");
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived Class Method");
}
}
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
}
Overriding in C#
1. Virtual – This keyword is used with a base class which signifies that the method
of a base class can be overridden.
2. Override – This keyword is used with a derived class which signifies that derived
class overrides a method of a base class.
3. Base – This keyword is used in a derived class to call the base class method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 1 – Without Virtual and Override Keywords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 2 (a)- With Virtual and Override Keywords
Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line()
{
Console.WriteLine("Object is being created");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
}
Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact
same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line() // constructor
{
Console.WriteLine("Object is being created");
}
~Line() //destructor
{
Console.WriteLine("Object is being deleted");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
}
Interface : should be used if you want to imply a rule on the components which may
or may not be related to each other
Abstract Class : should be used where you want to have some basic or default behavior
or implementation for components related to each other
Pros:
Interface: Allows multiple inheritance
provides abstraction by not exposing what exact kind of object is being used in the contex
provides consistency by a specific signature of the contract
Abstract Class: faster then interface
has flexibility in the implementation (you can implement it fully or partially) can be
easily changed without breaking the derived classes
Cons:
Interface : Must implement all the contracts defined
cannot have variables or delegates
once defined cannot be changed without breaking all the classes
Abstract Class : cannot be instantiated does not support multiple inheritance
Abstract class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
abstract class M1
{
public int add(int a, int b)
{
return (a + b);
}
}
class M2 :M1
{
public int mul(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
M2 ob = new M2();
int result = ob.add(10, 20);
Console.WriteLine("the result is {0}", result);
int muls = ob.mul(10, 20);
Console.WriteLine("the result is {0}", muls);
Console.ReadLine();
}
}
}
Delegates
C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method.
The reference can be changed at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
delegate int NumberChanger(int n);
namespace ConsoleApplication1
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
class Program
{
static void Main(string[] args)
{ //create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
}
Sealed Class
Class is defined as sealed class, this class cannot be inherited.
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
Sealed classes are primarily used to prevent derivation.
Because they can never be used as a base class,
some run-time optimizations can make calling sealed class members slightly faster.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
}
Partial Class
Splitting up of a class in to two or more sub classes is called as partial classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public partial class MyTest
{
private int a;
private int b;
public void getAnswer(int a, int b)
{
this.a = a;
this.b = b;
}
}
public partial class MyTest
{
public void PrintCoOrds()
{
Console.WriteLine("Integer values: {0},{1}", a, b);
Console.WriteLine("Addition: {0}", a + b);
Console.WriteLine("Mulitiply: {0}", a * b);
}
}
class Program
{
static void Main(string[] args)
{
MyTest ts = new MyTest();
ts.getAnswer(12, 25);
ts.PrintCoOrds();
Console.Read();
}
}
}

Mais conteúdo relacionado

Semelhante a Presentation.pptx

Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
Umar Farooq
 

Semelhante a Presentation.pptx (20)

Interface
InterfaceInterface
Interface
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
Global objects in Node.pdf
Global objects in Node.pdfGlobal objects in Node.pdf
Global objects in Node.pdf
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
04 threads
04 threads04 threads
04 threads
 
Thread
ThreadThread
Thread
 
using System.docx
using System.docxusing System.docx
using System.docx
 
Xamarin: Namespace and Classes
Xamarin: Namespace and ClassesXamarin: Namespace and Classes
Xamarin: Namespace and Classes
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
Tech talk
Tech talkTech talk
Tech talk
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
Java file
Java fileJava file
Java file
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Presentation.pptx

  • 1. Oops Class – collection of objects Object – instance of a class Encapsulation – process of binding data members and member functions into single un using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class rectangle { public double length; public double width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }
  • 2. class Program { static void Main(string[] args) { rectangle r = new rectangle(); r.length = 3.5; r.width = 4.6; r.Display(); } } } Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features. .Net has five access Specifiers Public -- Accessible outside the class through object reference. Private -- Accessible inside the class only through member functions. Protected -- Just like private but Accessible in derived classes also through member functions. Internal -- Visible inside the assembly. Accessible through objects. Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member func
  • 3. Public A1 A2 C1 C3 √ √ C2 C4 √ √ Protected internal A1 A2 C1 C3:C2 √ √ C2 C4 √ × Private A1 A2 C1 C3 √ × C2 C4 × × Protected A1 A2 C1 C3 √ × C2:C1 C4 √ × Internal A1 A2 C1 C3 √ × C2 C4 √ ×
  • 4. Inheritance: Inheritance is a process of deriving the new class from already existing class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class class Rectangle : Shape { public int getArea() { return (width * height); } }
  • 5. class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); r.setWidth(9); r.setHeight(5); r.getArea(); Console.WriteLine(r.getArea()); } } } Polymorphism: When a message can be processed in different ways is called polymorphism. Polymorphism means many forms. Polymorphism is of two types: Compile time polymorphism/Overloading – method name must be same parameters and return type may be varied Function overloading Operator overloading Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
  • 6. Function Overloading using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Printdata { void print(int i) { Console.WriteLine("Printing int: {0}", i); } void print(double f) { Console.WriteLine("Printing float: {0}", f); } void print(string s) { Console.WriteLine("Printing string: {0}", s); } class Program { static void Main(string[] args) { Printdata p = new Printdata(); p.print(2); p.print(3.6); p.print("pavan"); Console.ReadKey(); } } } }
  • 7. Operator Overloading using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Box { private double length; // Length of a box private double width; // Width of a box private double height; // Height of a box public double getVolume() { return length * width * height; } public void setLength( double len ) { length = len; } public void setWidth( double wid ) { width = wid; } public void setHeight( double hei ) { height = hei; }
  • 8. // Overload + operator to add two Box objects. public static Box operator+ (Box b, Box c) { Box box = new Box(); box.length = b.length + c.length; box.width = b.width + c.width; box.height = b.height + c.height; return box; } } class Program { static void Main(string[] args) { Box Box1 = new Box(); // Declare Box1 of type Box Box Box2 = new Box(); // Declare Box2 of type Box Box Box3 = new Box(); // Declare Box3 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setWidth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setWidth(13.0); Box2.setHeight(10.0);
  • 9. // volume of box 1 volume = Box1.getVolume(); Console.WriteLine("Volume of Box1 : {0}", volume); // volume of box 2 volume = Box2.getVolume(); Console.WriteLine("Volume of Box2 : {0}", volume); // Add two object as follows: Box3 = Box1 + Box2; // volume of box 3 volume = Box3.getVolume(); Console.WriteLine("Volume of Box3 : {0}", volume); Console.ReadKey(); } } }
  • 10. Runtime polymorphism/Overriding using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class BaseClass { public virtual void Method1() { Console.Write("Base Class Method"); } } // Derived class public class DerivedClass : BaseClass { public override void Method1() { Console.Write("Derived Class Method"); } } class Program { static void Main(string[] args) { // calling the overriden method DerivedClass objDC = new DerivedClass(); objDC.Method1(); // calling the baesd class method BaseClass objBC = (BaseClass)objDC; objDC.Method1(); } } }
  • 11. Overriding in C# 1. Virtual – This keyword is used with a base class which signifies that the method of a base class can be overridden. 2. Override – This keyword is used with a derived class which signifies that derived class overrides a method of a base class. 3. Base – This keyword is used in a derived class to call the base class method.
  • 12. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Bird // base class { public void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public new void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } } Example 1 – Without Virtual and Override Keywords
  • 13. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Bird // base class { public void fly() // base class method { Console.WriteLine("Birds are flying"); } } class Peacock : Bird // derived class { public new void fly() // derived class method { Console.WriteLine("Peacock is flying"); } } class Program { static void Main(string[] args) { Bird b = new Peacock(); b.fly(); Console.ReadLine(); } } } Example 2 (a)- With Virtual and Override Keywords
  • 14. Constructors A class constructor is a special member function of a class that is executed whenever we create new objects of that class. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength(double len) { length = len; } public double getLength() { return length; } class Program { static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); Console.ReadKey(); } } } }
  • 15. Destructors A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Line { private double length; // Length of a line public Line() // constructor { Console.WriteLine("Object is being created"); } ~Line() //destructor { Console.WriteLine("Object is being deleted"); } public void setLength(double len) { length = len; } public double getLength() { return length; } class Program { static void Main(string[] args) { Line line = new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line : {0}", line.getLength()); } } } }
  • 16. Interface : should be used if you want to imply a rule on the components which may or may not be related to each other Abstract Class : should be used where you want to have some basic or default behavior or implementation for components related to each other Pros: Interface: Allows multiple inheritance provides abstraction by not exposing what exact kind of object is being used in the contex provides consistency by a specific signature of the contract Abstract Class: faster then interface has flexibility in the implementation (you can implement it fully or partially) can be easily changed without breaking the derived classes Cons: Interface : Must implement all the contracts defined cannot have variables or delegates once defined cannot be changed without breaking all the classes Abstract Class : cannot be instantiated does not support multiple inheritance
  • 17. Abstract class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { abstract class M1 { public int add(int a, int b) { return (a + b); } } class M2 :M1 { public int mul(int a, int b) { return a * b; } } class Program { static void Main(string[] args) { M2 ob = new M2(); int result = ob.add(10, 20); Console.WriteLine("the result is {0}", result); int muls = ob.mul(10, 20); Console.WriteLine("the result is {0}", muls); Console.ReadLine(); } } }
  • 18. Delegates C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; delegate int NumberChanger(int n); namespace ConsoleApplication1 { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } class Program { static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } }
  • 19. Sealed Class Class is defined as sealed class, this class cannot be inherited. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SealedClass sealedCls = new SealedClass(); int total = sealedCls.Add(4, 5); Console.WriteLine("Total = " + total.ToString()); } } // Sealed class sealed class SealedClass { public int Add(int x, int y) { return x + y; } } } }
  • 20. Partial Class Splitting up of a class in to two or more sub classes is called as partial classes. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public partial class MyTest { private int a; private int b; public void getAnswer(int a, int b) { this.a = a; this.b = b; } } public partial class MyTest { public void PrintCoOrds() { Console.WriteLine("Integer values: {0},{1}", a, b); Console.WriteLine("Addition: {0}", a + b); Console.WriteLine("Mulitiply: {0}", a * b); } } class Program { static void Main(string[] args) { MyTest ts = new MyTest(); ts.getAnswer(12, 25); ts.PrintCoOrds(); Console.Read(); } } }