Sealed Class :
A class which can’t be inherited by any other class is
referred as a Sealed Class. To make a class Sealed we need to apply sealed
modifier on it.
Syntax -
sealed class Class1
{
- members
}
class Class2 :
Class1 //
invalid
|
Note – a sealed class if required can be consumed from other
classes by creating its object.
Sealed Methods :
A method which cannot be overridden under a child class is
referred as a sealed method. By default every method is sealed method because
overriding a method is not possible unless it was declared as virtual.
If at all a method is declared as
virtual within a class any of its child class in the linear hierarchy has a
chance to override the method.
Eg.
class Class1
public virtual void
Display()
class Class2 : Class1
public override void
Display()
class Class3 : Class2
public override void Display()
|
A virtual method of a class can be
sealed by its child class so that further overriding of the method by Childs of
child class will not be possible.
class Class1
public virtual void
Display()
class Class2 : Class1
public sealed override
void Display()
class Class3 : Class2
public override void
Display() // invalid
|
Abstract Methods and Abstract Class –
Abstract Method :
A method without any method body is referred as an abstract
method, what it has, is only Declearetion of the method. We need to use
abstract modifier to declare an abstract method.
Abstract Class :
The class under which these abstract
methods are declared is referred as abstract class. Need to declare the class
also using abstract modifier.
Ex-
abstract class
Example
{
public abstract void
Add (int x , int y)
}
|
- The concept of abstract methods is near
similar to the concept of method overriding, where in overriding if at all a
class declared a method as virtual it can be re implemented by its child
classes using the override modifier, which is only optional for a child class,
whereas if any class declared a method as abstract implementation of the method
has to be provided by its child classes using overrode modifier only, which is
mandatory for child classes to provide the implementation.
An abstract class can contain abstract and non- abstract members in it, if at all a class contains abstract members in it until and unless the abstract members are implemented under the childe class, childe class cannot consume non-abstract members of parent.
Note – An abstract class is never useable to itself because we
cannot create the object of an abstract class.
- Add a class AbsParent.cs and write the
following code:
abstract class AbsParent
{
public void Add(int x, int y)
{
Console.WriteLine(x
+ y);
}
public void sub(int x, int y)
{
Console.WriteLine(x
- y);
}
public abstract void Mul(int x, int y);
public abstract void Div(int x, int y);
}
}
|
- Add a class AbsChild.cs and write the
following code:
class AbsChild : AbsParent
{
public override void Mul(int x, int y)
{
Console.WriteLine(x
* y);
}
public override void Div(int x, int y)
{
Console.WriteLine(x
/ y);
}
static void
{
AbsChild obj = new AbsChild();
obj.Add(100, 50); obj.sub(20, 10);
obj.Mul(57, 29); obj.Div(1200, 600);
Console.ReadLine();
}
}
|
- To test this rewrite the code under main
method of class AbsChild.cs
as following:
{
AbsChild obj = new AbsChild();
AbsParent p = obj;
p.Add(100, 50); p.sub(20, 10);
p.Mul(57, 29); p.Div(1200, 600);
Console.ReadLine();
}
|
0 Comments