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. 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. 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. 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.
6.
7.
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 "having multiple forms"). Polymorphism is briefly described as "one interface, many implementations." 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. Polymorphism Compile time polymorphism Run time polymorphism Method Hiding Virtual and Overridden Methods Operators overloading Functions/Methods overloading
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. 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("A::Foo () "); } } class B : A {} class Test { static void Main(string[] args) { A a = new A(); a.Foo(); // output --> "A::Foo()" B b = new B(); b .Foo(); // output --> "A::Foo()" } } }
12. Casting using System; namespace Polymorphism { class A { public void Foo() { Console.WriteLine("A::Foo () "); } } class B : A { public void Foo() { Console.WriteLine("B::Foo () "); } } class Test { static void Main(string[] args) { A a; B b; a = new A(); b = new B(); a.Foo(); // output --> "A::Foo()" b.Foo(); // output --> "B::Foo()" a = new B(); a.Foo(); // output --> "A::Foo()" }} }
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("A::Foo () "); } } class B : A { public override void Foo() { Console.WriteLine("B::Foo () "); } } class Test { static void Main(string[] args) { A a; B b; a = new A(); b = new B(); a.Foo(); // output --> "A::Foo()" b.Foo(); // output --> "B::Foo()" a = new B(); a.Foo(); // output --> "B::Foo()" } } }
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("A::Foo () "); } } class B : A { public new void Foo() { Console.WriteLine("B::Foo () "); } } class Test { static void Main(string[] args) { A a; B b; a = new A(); b = new B(); a.Foo(); // output --> "A::Foo()" b.Foo(); // output --> "B::Foo()" a = new B(); a.Foo(); // output --> "A::Foo()" } } }
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 "know" 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. 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).