Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.


I am not the author of this file but wanted to collect it for study purpose.

  • Inicia sesión para ver los comentarios


  1. 1. Polymorphism Compile time polymorphism Run time polymorphism Method Hiding Virtual and Overridden Methods Operators overloading Functions/Methods overloading Inheritance Class/ Abstract Class Interface
  2. 2. What is inheritance? Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. If class A inherits from class B, then B is called Base Class of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding Base Class are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the Base Class. However, subclasses are not limited to the state and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. In the literature you may also find other terms for "superclass" and "subclass". Superclasses are also called parent classes or base classes. Subclasses may also be called child classes or just derived classes.
  3. 3. Inheritance Example Like a car, truck or motorcycles have certain common characteristics- they all have wheels, engines and brakes. Hence they all could be represented by a common class Vehicle which encompasses all those attributes and methods that are common to all types of vehicles. However they each have their own unique attributes; car has 4 wheels and is smaller is size to a truck; whereas a motorcycle has 2 wheels. Thus we see a parent-child type of relationship here where the Car, Truck or Motorcycle can inherit certain Characteristics from the parent Vehicle; at the same time having their own unique attributes. This forms the basis of inheritance; Vehicle is the Parent, Super or the Base class. Car, Truck and Motorcycle become the Child, Sub or the Derived class.
  4. 4. Abstract Classes Classes can be declared as abstract. This is accomplished by putting the keyword abstract before the keyword class in the class definition. The abstract keyword enables you to create classes and class members solely for the purpose of inheritance public abstract class A { // Class members here. } An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example: public abstract class A { public abstract void DoWork(int i); }
  5. 5. <ul><li>Abstract classes have the following features: </li></ul><ul><li>An abstract class cannot be instantiated. </li></ul><ul><li>An abstract class may contain abstract methods and accessors. </li></ul><ul><li>It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited. </li></ul><ul><li>A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors. </li></ul><ul><li>Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation. </li></ul><ul><li>Abstract methods have the following features: </li></ul><ul><li>An abstract method is implicitly a virtual method. </li></ul><ul><li>Abstract method declarations are only permitted in abstract classes. </li></ul><ul><li>Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no curly braces ({ }) following the signature. For example: </li></ul><ul><li>public abstract void MyMethod(); </li></ul><ul><li>The implementation is provided by an overriding method override, which is a member of a non-abstract class. </li></ul><ul><li>It is an error to use the static or virtual modifiers in an abstract method declaration. </li></ul><ul><li>Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax. </li></ul><ul><li>It is an error to use the abstract modifier on a static property. </li></ul><ul><li>An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier. </li></ul><ul><li>An abstract class must provide implementation for all interface members. </li></ul>
  6. 6. <ul><li>What is an Interface ? </li></ul><ul><li>An Interface is a reference type and it contains only abstract members. Interface's members can be Events , Methods , Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants , data fields , constructors, destructors and static members . All the member declarations inside interface are implicitly public. </li></ul><ul><li>Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface , all methods defined by that interface must appear in its source code before the class will successfully compile. some features of interface in C#. </li></ul><ul><li>An interface cannot inherit from a class. </li></ul><ul><li>An interface can inherit from multiple interfaces. </li></ul><ul><li>A class can inherit from multiple interfaces, but only one class. </li></ul><ul><li>Interface members must be methods, properties, events, or indexers </li></ul><ul><li>All interface members must have public access (the default). </li></ul><ul><li>By convention, an interface name should begin with an uppercase I. </li></ul>
  7. 7. <ul><li>Limitations and Restrictions of Interface </li></ul><ul><li>The essential idea to remember is that an interface never contains any implementation. The following restrictions and imitations are natural consequences of this </li></ul><ul><li>You're not allowed any fields in an interface, not even static ones. A field is an implementation of an object attribute. </li></ul><ul><li>You're not allowed any constructors in an interface. A constructor contains the statements used to initialize the fields in an object, and an interface does not contain any fields! </li></ul><ul><li>You're not allowed a destructor in an interface. A destructor contains the statements used to destroy an object instance. </li></ul><ul><li>You cannot supply an access modifier. All methods in an interface are implicitly public. </li></ul><ul><li>You cannot nest any types (enums, structs, classes, interfaces, or delegates) inside an interface. </li></ul><ul><li>You're not allowed to inherit an interface from a struct or a class. Structs and classes contain implementation; if an interface were allowed to inherit from either, it would be inheriting some implementation </li></ul>
  8. 8. What is Polymorphism in C# and Object Oriented Programming (OOPS) Languages? Object Oriented Programming (OOPS) Languages In object-oriented programming, polymorphism is a generic term that means 'many shapes'. (from the Greek meaning &quot;having multiple forms&quot;). Polymorphism is briefly described as &quot;one interface, many implementations.&quot; polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form. Polymorphism in C# There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism . Compile time polymorphism is functions and operators overloading . Runtime time polymorphism is done using inheritance (Override) and virtual functions . Here are some ways how we implement polymorphism in Object Oriented programming languages
  9. 9. Polymorphism Compile time polymorphism Run time polymorphism Method Hiding Virtual and Overridden Methods Operators overloading Functions/Methods overloading
  10. 10. Function Overloading Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The functions which differ only in their return types cannot be overloaded . The compiler will select the right function depending on the type of parameters passed. Operator Overloading In polymorphism operators can also be overloaded ( Compile time polymorphism ). Operators can be overloaded in order to perform special functions with respect to the class. With the help of operator overloading standard operations such as + , - , * , etc can be applied on the objects of the class.
  11. 11. Inherited Methods A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses     using System;     namespace Polymorphism     {         class A         {             public void Foo() { Console.WriteLine(&quot;A::Foo () &quot;); }         }         class B : A {}         class Test         {             static void Main(string[] args)             {                 A a = new A();                 a.Foo();  // output --> &quot;A::Foo()&quot;                 B b = new B();                 b .Foo();  // output --> &quot;A::Foo()&quot;             }         }   }
  12. 12. Casting     using System;     namespace Polymorphism     {         class A         {               public void Foo() { Console.WriteLine(&quot;A::Foo () &quot;); }         }         class B : A         {               public void Foo() { Console.WriteLine(&quot;B::Foo () &quot;); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> &quot;A::Foo()&quot;                 b.Foo();  // output --> &quot;B::Foo()&quot;                 a = new B();                 a.Foo();  // output --> &quot;A::Foo()&quot;             }} }
  13. 13. Virtual and Overridden Methods Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.     using System;     namespace Polymorphism     {         class A         {             public virtual void Foo() { Console.WriteLine(&quot;A::Foo () &quot;); }         }         class B : A         {             public override void Foo() { Console.WriteLine(&quot;B::Foo () &quot;); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> &quot;A::Foo()&quot;                 b.Foo();  // output --> &quot;B::Foo()&quot;                 a = new B();                 a.Foo();  // output --> &quot;B::Foo()&quot;             }         }      }
  14. 14. Method Hiding Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding . Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:     using System;     namespace Polymorphism     {         class A         {             public void Foo() { Console.WriteLine(&quot;A::Foo () &quot;); }         }         class B : A         {             public new void Foo() { Console.WriteLine(&quot;B::Foo () &quot;); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> &quot;A::Foo()&quot;                 b.Foo();  // output --> &quot;B::Foo()&quot;                 a = new B();                 a.Foo();  // output --> &quot;A::Foo()&quot;             }         }     }
  15. 15. The difference between override and new in C# Case: override This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't &quot;know&quot; that the object was an instance of the derived class. For instance: public class Base { public virtual void SomeMethod() { } } public class Derived : Base { public override void SomeMethod() { } } ... Base b = new Derived(); b.SomeMethod(); will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.
  16. 16. Case:New KeyWord if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this: public class Base { public virtual void SomeOtherMethod() { } } public class Derived : Base { public new void SomeOtherMethod() { } } ... Base b = new Derived(); Derived d = new Derived(); b.SomeOtherMethod(); d.SomeOtherMethod(); Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method. If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).