OOP – What is abstract ?

The abstract modifier can be used with classes, methods, properties, indexers, and events.

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes.

Abstract classes have the following features:

  1. An abstract class cannot be instantiated.
  2. An abstract class may contain abstract methods and accessors.
  3. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
  5. Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.

Abstract methods have the following features:

  • An abstract method is implicitly a virtual method.
  • Abstract method declarations are only permitted in abstract classes.
  • 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 braces ({ }) following the signature. For example:public abstract void MyMethod();
  • The implementation is provided by an overriding method, which is a member of a non-abstract class.
  • It is an error to use the static or virtual modifiers in an abstract method declaration.
  • Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
  • It is an error to use the abstract modifier on a static property.
  • An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
  • An abstract class must provide implementation for all interface members.

An abstract class that implements an interface might map the interface methods onto abstract methods.

Example:

interface I
{
void M();
}

abstract class C: I
{
public abstract void M();
}

In this example, the class MyDerivedC is derived from an abstract class MyBaseC. The abstract class contains an abstract method, MyMethod(), and two abstract properties, GetX() and GetY().

Example:
// abstract_keyword.cs
// Abstract Classes

using System;
abstract class MyBaseC // Abstract class
{
protected int x = 100;
protected int y = 150;
public abstract void MyMethod(); // Abstract method

public abstract int GetX // Abstract property
{
get;
}

public abstract int GetY // Abstract property
{
get;
}
}

class MyDerivedC: MyBaseC
{
public override void MyMethod()
{
x++;
y++;
}

public override int GetX // overriding property
{
get
{
return x+10;
}
}

public override int GetY // overriding property
{
get
{
return y+10;
}
}

public static void Main()
{
MyDerivedC mC = new MyDerivedC();
mC.MyMethod();
Console.WriteLine(“x = {0}, y = {1}”, mC.GetX, mC.GetY);
}
}

Output
x = 111, y = 161

In the preceding example, if you attempt to instantiate the abstract class by using a statement like this:

MyBaseC mC1 = new MyBaseC(); // Error

you will get the following error message:
Cannot create an instance of the abstract class ‘MyBaseC’.

Share this:
Share

OOP – What is 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.
Share this:
Share

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