360 likes | 468 Vues
OOP’S Concepts in C#.Net. Four Basic concepts. Abstraction Encapsulation Polymorphism Inheritance. Abstraction. Abstraction is of two types:- 1.Abstract Class:- -class is not instantiated -new class() causes compiler error
E N D
Four Basic concepts • Abstraction • Encapsulation • Polymorphism • Inheritance
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(); }
Contd.. • Abstract methods -methods have no implementation -methods are automatically virtual -class are automatically virtual
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() {myclassmycls = new myclass();mycls.nonabmethod();mycls.abmethod(); } }
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!"); } } }
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.
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;}}
Contd. • public static int Main(string[] args){Department d = new Department();d.SetDepartname("ELECTRONICS");Console.WriteLine("The Department is :"+d.GetDepartname());return 0;}
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.
Example • using system;public class Department {private string departname;public string Departname{get{return departname;}set {departname=value;}}}
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
READ ONLY PROPERTY: • using system;public class ReadDepartment {private string departname;public ReadDepartment(string avalue){departname=avalue;}public string Departname{get{return departname;}}}
public class ReadDepartmain{public static int Main(string[] args){ReadDepartment d= new ReadDepartment("COMPUTERSCIENCE");Console.WriteLine("The Department is: {0}",d.Departname);return 0;} }
WRITE ONLY PROPERTY: • using system;public class WriteDepartment {private string departname;public string Departname{set{departname=value;Console.WriteLine("The Department is :{0}",departname);}}}
public class WriteDepartmain{public static int Main(string[] args){WriteDepartment d= new WriteDepartment();d.departname="COMPUTERSCIENCE";return 0;} }
Inheritance • Inheritance is the process of creation new classes from already existing classes • C# supports two types of Inheritance mechanisms :- Implementation Inheritance Interface Inheritance
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.
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
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
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
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.
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.
Types Of Polymorphism Compile Time Polymorphism Method Overloading Run Time Polymorphism: Method Overriding
Method Overloading • Method with same name but with different arguments is called method overloading. • E.g class A1 { void hello() { Console.WriteLine(“Hello”); } void hello(string s) { Console.WriteLine(“Hello {0}”,s); } }
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”); } }
Class child : parent{ override void hello() { Console.WriteLine(“Hello from Child”); } }static void main(){ parent objParent = new child(); objParent.hello();}
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’
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
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"); } }
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"); } };
Contd.. Animal a2 = new Dog(); a2.Talk(); a2.Sing(); a2.Greet(); //Output Animal constructor Dog constructor Animal talk Dog song Animal says Hello