Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP’S Concepts in C#.Net

Similar presentations


Presentation on theme: "OOP’S Concepts in C#.Net"— Presentation transcript:

1 OOP’S Concepts in C#.Net

2 Four Basic concepts Abstraction Encapsulation Polymorphism Inheritance

3 Abstraction Abstraction is of two types:- 1.Abstract Class:-
-class is not instantiated -new class() causes compiler error e.g. abtsract public class employee { string Name; void printName(); }

4 Contd.. Abstract methods -methods have no implementation
-methods are automatically virtual -class are automatically virtual

5 using System; namespace OOPs {           /// Abstract class      abstract class Abstract1      {           public Abstract1()           {                // TODO: Add constructor logic here           }            //non-abstract method            public void nonabmethod()      {           System.Console.WriteLine("Non Abstract Method!");      }           //abstract method           public abstract void abmethod(); //they are implicitly virtual it is not contain the body of the method           public static void Main()           {                myclass mycls = new myclass();                mycls.nonabmethod();                mycls.abmethod();           }      }

6 Contd… class myclass : Abstract1 //class derived from abstract class           {                public override void abmethod() //must implement the abstract method derived in class           {                System.Console.WriteLine("Abstract Method!");           }      } }

7 Encapsulation Encapsulation is the procedure of covering up of data and functions into a single unit (called class). By using this, Encapsulation provides a way to protect data from accidental corruption. In java,there are get() and set() methods are available same like that in C# accessor and mutator methods are used for encapsulation.

8 Example of using accessor and mutator
using system; public class Department { private string departname; // Accessor. public string GetDepartname() { return departname; } // Mutator. public void SetDepartname( string a) { departname=a; } }

9 Contd. public static int Main(string[] args) { Department d = new Department(); d.SetDepartname("ELECTRONICS"); Console.WriteLine("The Department is :"+d.GetDepartname()); return 0; }

10 By using properties also we can achieve encapsulation in C#.net
Properties are a new language feature introduced with C# Properties in C# helps in protect a field in a class by reading and writing to it The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item.

11 Example using system; public class Department { private string departname; public string Departname { get { return departname; } set { departname=value; } } }

12 public class Departmentmain { public static int Main(string[] args) { Department d= new Department(); d.Departname="Communication"; Console.WriteLine("The Department is :{0}",d.Departname); return 0; } } Thus we are manipulate our read and write both methods of class This method has another two types:- Read only property Write only property

13 READ ONLY PROPERTY: using system; public class ReadDepartment { private string departname; public ReadDepartment(string avalue) { departname=avalue; } public string Departname { get { return departname; } } }

14 public class ReadDepartmain { public static int Main(string[] args) { ReadDepartment d= new ReadDepartment("COMPUTERSCIENCE"); Console.WriteLine("The Department is: {0}",d.Departname); return 0; } }

15 WRITE ONLY PROPERTY: using system; public class WriteDepartment { private string departname; public string Departname { set { departname=value; Console.WriteLine("The Department is :{0}",departname); } } }

16 public class WriteDepartmain { public static int Main(string[] args) { WriteDepartment d= new WriteDepartment(); d.departname="COMPUTERSCIENCE"; return 0; } }

17 Inheritance Inheritance is the process of creation new classes from already existing classes C# supports two types of Inheritance mechanisms :- Implementation Inheritance Interface Inheritance

18 What is Implementation Inheritance?
When a class (type) is derived from another class(type) such that it inherits all the members of the base type it is Implementation Inheritance.

19 What is Interface Inheritance?
When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance

20 Contd.. By using ‘abstract ’and ‘interface’ keywords in base class we can inherit that class C# does not support multiple implementation inheritance which is supported by C++ but it supports multiple interface inheritance

21 super v/s base In java if the constructor of base class is called in derived class ‘super’ keyword is used Thus in C# .’base ’keyword is used With help of ‘base ‘,we can call method of base class in derived class

22 BUT? If we don’t want that then?
In C#,there will be facility of Sealed classes Once a class is defined as sealed class, this class cannot be inherited. The sealed modifier is used to define a class as sealed.This is very useful when the classes have static members.

23 Polymorphism Polymorphism means same operation may behave differently on different classes. Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.

24 Types Of Polymorphism Compile Time Polymorphism Method Overloading
Run Time Polymorphism: Method Overriding

25 Method Overloading Method with same name but with different arguments is called method overloading. E.g class A { void hello() { Console.WriteLine(“Hello”); } void hello(string s) { Console.WriteLine(“Hello {0}”,s); } }

26 Method Overriding Method overriding occurs when child class declares a method that has the same type arguments as a method declared by its superclass. E.g Class parent { virtual void hello() { Console.WriteLine(“Hello from Parent”); } }

27 Class child : parent { override void hello() { Console
Class child : parent { override void hello() { Console.WriteLine(“Hello from Child”); } } static void main() { parent objParent = new child(); objParent.hello(); }

28 Virtual methods In C#,for method overriding ‘virtual’ keyword is used
If in base class,method is virtual then it is overridden by derived class by using ‘overrides’

29 Shadow implementation
If in child class any method has modifier ‘new’ which is of base class then it is called ‘shadow implementation’ It means method is not overriding

30 Example class Animal { public Animal() {
Console.WriteLine("Animal constructor"); } public void Greet() Console.WriteLine("Animal says Hello"); public void Talk() Console.WriteLine("Animal talk"); public virtual void Sing() Console.WriteLine("Animal song");

31 class Dog : Animal { public Dog() Console.WriteLine("Dog constructor"); } public new void Talk() Console.WriteLine("Dog talk"); public override void Sing() Console.WriteLine("Dog song"); };

32 Contd.. Animal a2 = new Dog(); a2.Talk(); a2.Sing(); a2.Greet();
//Output Animal constructor Dog constructor Animal talk Dog song Animal says Hello


Download ppt "OOP’S Concepts in C#.Net"

Similar presentations


Ads by Google