Hiding Methods

 Let us imagine a super class and a sub class having two methods with same name and same arguments.
 If you call the method from the client code, which method would be called? (either super class method or sub class method?) Ans: Sub Class Method only.
 This is called as “Hiding Methods”. Here, the super class method is hidden by sub class method.
 But, a compile time warning will be displayed in the sub class method definition to use a keyword called “new”.
 The “new” keyword hides the super class method, that contains same name and same arguments set.
 Of course, this is done by default, but to implement a strong programming, “new” keyword is recommended to be used when you want to hide the super class method with intention.

Hiding Methods with “new” keyword

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDemo
{
class US
{
public void ShowCountry()
{
Console.WriteLine("This is United States.");
}
}
class India : US
{
public new void ShowCountry()
{
Console.WriteLine("This is India.");
}
}
class Program
{
static void Main(string[] args)
{
India i = new India();
i.ShowCountry();
Console.Read();
}
}
}
method hiding

Virtual Methods and Method Overriding

 The similar operation as above can be implemented with two keywords called “virtual” and “override”.
 The “virtual” keyword should be used in the “super class method” definition and the “override” keyword should be used in the “sub class method” definition.
 This concept can be called as “Method Overriding”.
 In fact, there is no much difference between “Hiding methods” and “Method Overriding”.
 This is similar to “virtual functions” in C++; but “override” keyword is not required in C++.
 The access modifiers used in the base and derived classes for the virtual methods should be same.

Virtual Methods and Method Overriding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDemo
{
class US
{
public virtual void ShowCountry()
{
Console.WriteLine("This is United States.");
}
}
class India : US
{
public override void ShowCountry()
{
Console.WriteLine("This is India.");
}
}
class Program
{
static void Main(string[] args)
{
India i = new India();
i.ShowCountry();
Console.Read();
}
}
}
method hiding

Calling base class versions of methods

 In the above two applications, the base class method named “ShowCountry()” is not executed.
 If you want to call the base class version, simply use “base” keyword in the sub class method.
 The “base” keyword simply represents the respective super class.
Syn:
base.superclassmethod();

Demo on “base” keyword

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseKeywordDemo
{
class US
{
public virtual void ShowCountry()
{
Console.WriteLine("This is United States.");
}
}
class India : US
{
public override void ShowCountry()
{
base.ShowCountry();
Console.WriteLine("This is India.");
}
}
class Program
{
static void Main(string[] args)
{
India i = new India();
i.ShowCountry();
Console.Read();
}
}
}
method hiding

Abstract Classes and Methods

Abstract Methods
 The abstract method is same as “pure virtual functions” in C++.
 The abstract method can be declared with “abstract” keyword like this:

public abstract void samplemethod();

 The abstract method doesn’t contain method definition; it contains only method declaration as above.
 The abstract methods can be declared only within abstract classes.
Abstract Classes
 A class that is declared with “abstract” keyword is called as “abstract class”.
abstract class classname
{

}
 Rule: If a class contains at least one abstract method, that class should be declared as abstract class.
 The abstract class can contain abstract methods, non-abstract class and normal data members also.
 Note: You can’t create an object for the abstract class. It can be inherited from another non-abstract class.
 The non-abstract class, that inherits the abstract class, should implement the definition(s) (with “override” keyword) for all of the abstract methods declared in the abstract class.
 Note: The access modifiers used in the base and derived classes for the abstract methods should be same.

Implementation Syntax
abstract class abstractclassname
{
//data members if any
//non-abstract methods if any
accessmodifier abstract returntype methodname(arguments);
}
class derivedclassname : abstractclassname
{
accessmodifier override returntype methodname(arguments)
{
//method body
}
}

: Abstract Classes and Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractDemo
{
//abstract class
abstract class MyBaseClass
{
public abstract void FirstMethod();
public void SecondMethod()
{
Console.WriteLine("This is Non-Abstract Method");
}
}
//non-abstract class
class MyDerivedClass : MyBaseClass
{
public override void FirstMethod()
{
Console.WriteLine("This is Abstract Method");
}
}
class Program
{
static void Main(string[] args)
{
MyDerivedClass s = new MyDerivedClass();
s.FirstMethod();
s.SecondMethod();
Console.Read();
}
}
}
method hiding