■ Overloading
•Method overloading is the process of defining multiple methods with the same name but different signatures (parameter lists) within the same class
• Overloading is all about defining multiple behaviour to a method..
• Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form.
• Method overloading is a type of compile-time polymorphism.
• If method signature (number, types, and order of parameters) is same and the return type is different, then we can't overload method it will throw compile time error
Example -:
class Demo{
//adding three integer values
public int Add(int a, int b, int c){
int sum = a + b + c;
return sum;
}
// adding three double values.
public double Add(double a,
double b, double c)
{
double sum = a + b + c;
return sum;
}
This will throw compile time error as method signature is same but return type is different
• you can't define more than one method with same name and same signature(number, types, and order of parameters)
• The return type of the methods can be the same or different, but overloading is not determined by the return type.
• Method overloading is possible both within the same class and across parent-child classes.
➤ Different ways of doing overloading methods
1) The number of parameters in two methods.
public int Add(int a, int b) {
int sum = a + b;
return sum;
}
public int Add(int a, int b, int c) {
int sum = a + b + c;
return sum;
}
2) The data types of the parameters of methods.
public int Add(int a, int b, int c){
int sum = a + b + c;
return sum;
}
public double Add(double a,
double b, double c) {
double sum = a + b + c;
return sum;
}
3) The Order of the parameters of methods.
public double Add(int a, double b) {
return a + b;
}
public double Add(double a, int b) {
return a + b;
}
_____________________________
_____________________________
■ Overriding
• Creating a method in the derived(child) class with the same signature(same name, return type, and parameters) as a method in the base(parent) class is called as method overriding.
• Overriding is all about changing the behaviour of parent’s method under the
child class.
• In simple words, Overriding is a feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.
• Method overriding is a type of run-time polymorphism.
• parent class Method is called overridden method and the method which override the method from parent class are called overriding method.
• The base class method must be marked with the virtual keyword, and the method in the derived class must be marked with the override keyword.
• A non-virtual or a static method can’t be overridden.
• The access modifier of the overriding method in the derived class should be the same or less restrictive than the access modifier of the method in the base class.
• declaring a method as virtual allows it to be overridden in any derived class
➤ Overriding can achieve by 2 ways
1) Using the 'virtual' and 'override' Keywords
2) Using Abstract Methods -> abstract methods are implicitly virtual and must be overridden in any non abstract derived class
Ex :-
public class BaseClass {
public virtual void Display() {
Console.WriteLine("Base Class Display Method");
}
}
public class DerivedClass : BaseClass {
public override void Display() {
Console.WriteLine("Derived Class Display Method");
}
}
class Program
{
static void Main(string[] args)
{
BaseClass obj1 = new BaseClass();
obj1.Display(); // Output: Base Class Display Method
DerivedClass obj2 = new DerivedClass();
obj2.Display(); // Output: Derived Class Display Method
//The reference to the DerivedClass object is assigned to the obj3 variable, which is of type BaseClass.
BaseClass obj3 = new DerivedClass();
obj3.Display(); // Output: Derived Class Display Method
}
}
____________________________________________
➤ Calling the Base Class Method
The derived class method can call the base class method using the 'base' keyword.
➤ Sealing an Overridden Method
If you want to prevent further overriding of a method, you can use the 'sealed' keyword.
public class DerivedClass : BaseClass
{
public sealed override void Display()
{
Console.WriteLine("Derived Class Display Method");
}
}
public class FurtherDerivedClass : DerivedClass
{
// This will cause a compile-time error
// public override void Display()
// {
// Console.WriteLine("Further Derived Class Display Method");
// }
}
____________________________________________
➤ Assigning a Derived Class Object to a Base Class Reference
Concept :-When you create an object of a derived class (Apple) and assign it to a reference of the base class (Fruit), you can use that base class reference to call methods defined in the base class.
➔ How it works
When you create an object of a derived class (Apple) and assign it to a reference of the base class (Fruit), you can use that base class reference to call methods defined in the base class.
If any method is overridden in the Apple(derived) class and the method in the Fruit(base) class is marked as virtual, the overridden method in Apple(derived) will be called.
➔ We can assign derived class object to a base class reference in 2 ways and both will produce exactly same result
1) Directly Assigning a Derived Class Object to a Base Class Reference
BaseClass baseobj=new DerivedClass();
2) Assigning a Derived Class Object to a Base Class Reference Through an Intermediate Derived Class Reference
DerivedClass deriveobj=new DerivedClass();
BaseClass baseobj= derivedobj
Let's understand with Example
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main(string[] args)
{
Animal obj = new Dog(); // Using the base class type to refer to a derived class object
obj.Speak(); // This will call the Speak method in the Dog class
}
}
Animal obj = new Dog();
1) 'Animal obj' creates a variable named 'obj' that can hold a reference to an object of type Animal or any of its derived types.
2) obj = new Dog() The reference to the Dog object is assigned to the obj variable
➤ let's understand what's the difference between
"DerivedClass derivedobj = new DerivedClass(); "
&
"BaseClass baseobj = new DerivedClass(); "
1)
➔ derivedObj is of type DerivedClass
➔ The reference variable baseobj is of type BaseClass, but it points to an instance of DerivedClass
2)
➔ derivedObj can access all public and protected members (methods, properties, fields) defined in DerivedClass, including those inherited from BaseClass.
➔ baseobj can can only access the members defined in BaseClass, even though it is actually an instance of DerivedClass
➤ So now question is if baseobj can only access base class members how it is calling overriding(child class) method?
Answer :-
•it is because of virtual and override keyword. • When a method in a base class is marked as virtual, it means that the method is allowed to be overridden in any derived class.
• The override keyword in the derived class indicates that the derived class is providing a new implementation for that method.
• Even though baseobj is a reference of type BaseClass, it points to an instance of DerivedClass.
• So it is able to calling overriding method because of virtual keyword, override keyword & because it points to the instance of derived class, it decides on run time that method overriding have to be called
➤ Why Use This?
• Using a base class type to refer to a derived class object allows you to write code that works with the base class type but can also operate on any derived class objects.
• This is useful for polymorphism, where the exact type of the object may not be known until runtime.
___________________________________
------------------------------
➤ Difference between method overloading and overriding
1) Definition
➔ Method overloading allows you to define multiple methods with the same name but different signatures.
➔ Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its base class.
2) Use case
➔ It is used when methods perform similar tasks but with different inputs.
➔ It is used to change or extend the behavior of an inherited method.
3) Runtime
➔ Method overloading is resolved at compile time.
➔ Method overriding is resolved at run time.
4) Inheritance
➔ Overloading does not require inheritance. ➔ overriding require inheritance.
5) Signature
➔ Methods must have different parameter lists.
➔ The overridden method in the derived class must have the same signature as the method in the base class.
6) Access Modifiers
➔ Methods can have different access modifiers(like public, private, protected, internal).
➔The access level of the overriding method must be the same or less restrictive than the overridden method.
7) static methods
➔ Static methods can be overloaded.
➔ Static methods cannot be overridden .
Comments
Post a Comment