OOP – What is static polymorphism?

  • The decision is made at compile time.
  • Which method is to be called is decided at compile-time only.
  • Method overloading is an example of this.
  • Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
  • Method overloading is a concept where a class can have more than one method with the same name and different parameters.
  • Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.
Share this:
Share

OOP – What is dynamic or runtime polymorphism?

  • Run-time polymorphism is achieved by method overriding.
  • Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
  • By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
  • Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by reference variable.
  • Compiler would not be aware whether the method is available for overriding the functionality or not. So compiler would not give any error at compile time. At runtime, it will be decided which method to call and if there is no method at runtime, it will give an error.

Share this:
Share

OOP – What is Polymorphism-Static Polymorphism-Dynamic Polymorphism-Runtime Polymorphis-Function overriding-function overloading-Virtual method

What is Polymorphism

  • Polymorphism means one object behaving as multiple forms.
  • One function behaves in different forms.
  • In other words, “Many forms of a single object is called Polymorphism.”

Person behaves as a SON in house, at the same time that person behaves like an EMPLOYEE in the office.
With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.

There are two types of polymorphism:

  • Static or compile time polymorphism
  • Dynamic or runtime polymorphism

Polimorphism, Runtime Polimorphism, Static Polimorphism

Static Polymorphism:

  • The decision is made at compile time.
  • Which method is to be called is decided at compile-time only.
  • Method overloading is an example of this.
  • Compile time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.
  • Method overloading is a concept where a class can have more than one method with the same name and different parameters.
  • Compiler checks the type and number of parameters passed on to the method and decides which method to call at compile time and it will give an error if there are no methods that match the method signature of the method that is called at compile time.

Dynamic or Runtime Polymorphism:

  • Run-time polymorphism is achieved by method overriding.
  • Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.
  • By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.
  • Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by reference variable.
  • Compiler would not be aware whether the method is available for overriding the functionality or not. So compiler would not give any error at compile time. At runtime, it will be decided which method to call and if there is no method at runtime, it will give an error.

Virtual Method:

  • Virtual method is a method whose behavior can be overridden in derived class.
  • Virtual method allows declare a method in base class that can be redefined in each derived class.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member.
  • The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.
  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

Virtual method solves the following problem:

  • In OOP, when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class, the method call behavior is ambiguous.
  • In C#, polymorphism is explicit – you must have a virtual (or abstract) modifier on the base class method (member) and an override on the derived class method, which you probably already know.
  • If you don’t put a modifier on a base class method, polymorphism can’t ever happen. If you then add a non-modified method to the derived class with the same signature as the non-modified base class method, the compiler will generate a Warning message.

Difference between Method Overriding and Method Hiding:

  • Method overriding allows a subclass to provide a specific implementation of a method that is already provided by base class.
  • The implementation in the subclass overrides (replaces) the implementation in the base class.
  • The important thing to remember about overriding is that the method that is doing the overriding is related to the method in the base class.



When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class

Share this:
Share

XML – Rules of XML serialization

  1. XML Serialization serializes the public fields and properties of a class, or the parameters and return values of methods into an XML stream.
  2. XML Serialization does not include methods, indexers, private fields, or read-only properties (except read-only collections). Because XML is an open standard, the resulting XML stream can be processed by any application on any platform. For example: ASP.NET Web Services use XML Serialization to create XML streams to pass as data throughout the Internet or Intranets. Conversely, deserialization takes such streams and constructs an object.
  3. The following items can be serialized using XmlSerialzer:
    • Public read/write properties.
    • Public fields.
    • Classes that implement ICollection or IEnumerable.
    • XmlElement objects.
    • XmlNode objects.
    • DataSet objects.
  4. XmlSerialzer gives complete control over serializing an object into XML. For example, XmlSerialzer enables you to:
    • Specify whether a field or a property should be encoded as an element or as an attribute.
    • Specify which XML namespace to use.
    • Specify the name of an element or attribute if a field / property name is inappropriate.

XML Serialization Considerations

Following should be considered when using XmlSerialzer class:

  1. Type identity and assembly information is not included. In other words, XML serialization does not maintain type fidelity. To maintain type fidelity use binary serialization instead.
  2. Only public properties and fields can be serialized. To serialize non-public data use binary serialization instead.
  3. A class must have a default constructor to be serialized with the XmlSerialzer class.
  4. Methods cannot be serialized.
  5. XmlSerialzer class can serialize classes that implement IColleciton and IEnumerable differently if they meet certain requirements.
Share this:
Share

C# – Rules of Private Constructor

  1.  private Constructor is used when you don’t want to create an object for the class.These are commonly used in classes that contain static members only.If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
  2. We cannot inherit from classes that are having only private constructors. The reason is suppose if you are inheriting from a base class which has only private constructors and if you create an object for the derived class then the base class constructor will be called. Because the base class contains only private constructors and  due to ‘private’ access modifier  it is not accessible from the derived  class. So it will give you an error with a syntax as follows ‘Error 1 ’ConsoleApplication2.Program.Program()’ is inaccessible due to its protection level’
  3. Where do you find Private constructors are useful?

    Private constructors can also be useful for:  classes containing only static utility methods  classes containing only constants  type safe enumerations.

Share this:
Share