

During compile time the compiler is not able to recognize the overriding method, hence, it doesn’t throw any error. To achieve method overriding both the base class and derived class should have the same name and parameter.

Method overriding is achieved using inheritance. It allows the user to create an abstract class with partial interface implementation. Method overriding is an example of dynamic polymorphism. Runtime polymorphism or dynamic polymorphism occurs when both method name and method signature have the same name and parameters. #2) Dynamic Polymorphism or Runtime Polymorphism Hence, based on the parameters passed, the first method will add two integers and the next one will concatenate two strings. Again the system will look for the method that accepts two string parameters. In the second part, we passed the string parameter. When we pass a parameter with the integers system, it will look for the method named “print” which accepts two integer parameters and it will execute that ignoring other methods with the same name.

There are two “print” methods with the same name. At first, we pass two integers as parameters, then we have passed two strings as parameters. In the above example, we have called the same method “print” twice using different parameters. In method overloading, the system first checks the parameter used and based on the set of parameter it decides to call the appropriate method.Ĭonsole.WriteLine("Printing int: ", (i+j) ) Ĭonsole.WriteLine("Printing String: ", (a+b)) It is achieved by keeping the method name the same but passing different sets of parameters. It is known as compile-time polymorphism as the method calling decision is made at the time of compiling. Method overloading is one of the ways in which compile-time polymorphism is achieved. Polymorphism is basically divided into two parts:Ĭompile-time polymorphism is also known as Static polymorphism. It allows the class in C# to have multiple implementations with the same name. Poly stands for many and Morph means forms. Polymorphism is derived from the Greek dictionary, it means one with many forms. This allows the same as the internal and the only difference is that a child class can inherit this class and reach its members even from another project. Another similar internal accessibility is protected internal. Internal: Internal provides accessibility from within the project. Protected: Protected accessibility allows the member to be accessed from within the class and from another class that inherits this class. This has one of the most restricted visibility. Private: The private members can only be accessed by the member within the same class. This access specifier has the least visibility restriction. Public: The public keyword allows its members to be visible from anywhere inside the project. The most commonly used visibility is public and private.
#Oops concepts code#
It allows you to make data visible for a particular part of the code and hide it from another part. The access specifiers define the visibility of the class and its values. Access Specifier is used for defining the visibility and accessibility of the class member in C#.Ĭ# contains the following access specifiers. It allows only relevant information available and visible outside and that too only to specific members.Įncapsulation is implemented by using access specifiers. It’s like encircling a logical item within a package. By using the encapsulation program, you can hide the members of one class from another class. In this tutorial, we will be focusing more on other major core OOPS concepts:Įncapsulation is an object-oriented programming concept that allows programmers to wrap data and code snippets inside an enclosure.
