What is class?
what is object?
What is encapsulation.?
what is inheritance ?
what is polymorphism ?
Polymorphism is one of the primary characteristics (concept) of object-oriented programming.
Poly means many and morph means form.
The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.
In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”.
But in C# no need to use any keyword while overloading a method either in same class or in derived class.
While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument.
Example for Method Overloading
Output
30
25.7
60
Note
Method overloading provides more than one form for a method. Hence it is an example for polymorphism.
In case of method overloading, compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation it self. Hence method overloading is an example for compile time polymorphism.
What is an Abstract Class?
http://www.c-sharpcorner.com/UploadFile/d0e913/abstract-class-interface-two-villains-of-every-interview/
What is an Interface
what is object?
What is encapsulation.?
what is inheritance ?
what is polymorphism ?
Types of Polymorphism
- Compile time Polymorphism(Method overloading)
- Run time Polymorphism (Methodoverridding )
Method Overloading in C#.NET
The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.
In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”.
But in C# no need to use any keyword while overloading a method either in same class or in derived class.
While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument.
Example for Method Overloading
using System;
namespace ProgramCall
{
class Class1
{
public int Sum(int A, int B)
{
return A + B;
}
public float Sum(int A, float B)
{
return A + B;
}
}
class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;
}
}
class MainClass
{
static void Main()
{
Class2 obj = new Class2();
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10, 15.70f));
Console.WriteLine(obj.Sum(10, 20, 30));
Console.Read();
}
}
}
namespace ProgramCall
{
class Class1
{
public int Sum(int A, int B)
{
return A + B;
}
public float Sum(int A, float B)
{
return A + B;
}
}
class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;
}
}
class MainClass
{
static void Main()
{
Class2 obj = new Class2();
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10, 15.70f));
Console.WriteLine(obj.Sum(10, 20, 30));
Console.Read();
}
}
}
Output
30
25.7
60
Note
Method overloading provides more than one form for a method. Hence it is an example for polymorphism.
In case of method overloading, compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation it self. Hence method overloading is an example for compile time polymorphism.
Method Overriding in C#.NET
Creating a method in derived class with same signature as a method in base class is called as method overriding.
Same signature means methods must have same name, same number of arguments and same type of arguments.
Method overriding is possible only in derived classes, but not within the same class.
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
Examples for Method Overriding in C#
London
Example - 2 implementing abstract method
Output
London
Same signature means methods must have same name, same number of arguments and same type of arguments.
Method overriding is possible only in derived classes, but not within the same class.
When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
Examples for Method Overriding in C#
using System;
namespace methodoverriding
{
class BaseClass
{
public virtual string YourCity()
{
return "New York";
}
}
class DerivedClass : BaseClass
{
public override string YourCity()
{
return "London";
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass();
string city = obj.YourCity();
Console.WriteLine(city);
Console.Read();
}
}
}
Outputnamespace methodoverriding
{
class BaseClass
{
public virtual string YourCity()
{
return "New York";
}
}
class DerivedClass : BaseClass
{
public override string YourCity()
{
return "London";
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass();
string city = obj.YourCity();
Console.WriteLine(city);
Console.Read();
}
}
}
London
Example - 2 implementing abstract method
using System;
namespace methodoverridingexample
{
abstract class BaseClass
{
public abstract string YourCity();
}
class DerivedClass : BaseClass
{
public override string YourCity() //It is mandatory to implement absract method
{
return "London";
}
private int sum(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass();
string city = obj.YourCity();
Console.WriteLine(city);
Console.Read();
}
}
}
namespace methodoverridingexample
{
abstract class BaseClass
{
public abstract string YourCity();
}
class DerivedClass : BaseClass
{
public override string YourCity() //It is mandatory to implement absract method
{
return "London";
}
private int sum(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass();
string city = obj.YourCity();
Console.WriteLine(city);
Console.Read();
}
}
}
Output
London
What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
http://www.c-sharpcorner.com/UploadFile/d0e913/abstract-class-interface-two-villains-of-every-interview/
What is an Interface
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
|
No comments:
Post a Comment