Sealed Class

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Sealed Class,Abstract Class and

Method Overriding
What is Sealed Class ???
• sealed keyword applies restrictions on the class and method. 

• If you create a sealed class, it cannot be derived.

• If you create a sealed method, it cannot be overridden further


more.

• A class can be sealed by using the sealed keyword.

• The keyword tells the compiler that the class is sealed, and
therefore, cannot be extended.
• The following is the syntax  and Example of a sealed class :

sealed class class_name sealed class Demo


{ {
// data members
//code…..
// methods . . .
}
}
Example1:
using System;
sealed class SealedClass
{
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main()
{
SealedClass slc = new SealedClass();
int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}
Sealed method
• The sealed method in C# cannot be
overridden further.

• It must be used with override keyword in


method.
Example 1
using System;
public class Animal
{
public virtual void eat()
{ Console.WriteLine("eating..."); }
public virtual void run()
{ Console.WriteLine("running..."); }
}
public class Dog: Animal
{
public override void eat()
{ Console.WriteLine("eating bread..."); }
public sealed override void run()
{ Console.WriteLine("running very fast...");}
}
public class BabyDog : Dog
{
public override void eat()
{ Console.WriteLine("eating biscuits..."); }
public override void run()
{ Console.WriteLine("running slowly..."); }
}
public class TestSealed
{
public static void Main()
{
BabyDog d = new BabyDog();
d.eat();
d.run();
}
}
Example 2
using System;
public class Animal
{
public virtual void eat()
{ Console.WriteLine("eating..."); }
public virtual void run()
{ Console.WriteLine("running..."); }
}
public class Dog: Animal
{
public override void eat()
{ Console.WriteLine("eating bread..."); }
public sealed override void run()
{ Console.WriteLine("running very fast...");}
}
public class BabyDog : Dog
{
public override void eat()
{ Console.WriteLine("eating biscuits..."); }
}
public class TestSealed
{
public static void Main()
{
BabyDog d = new BabyDog();
d.eat();
d.run();
}
}
Why Sealed Classes?
• Sealed class is used to stop a class to be
inherited. You cannot derive or extend any
class from it.

• We can provide security using sealed class.

• Many .NET classes are Sealed : StringBuilder


and so on
Abstract Class
• Abstraction in C# is the process to hide the internal
details and showing functionality only.

• Abstraction can be achieved by two ways:


– Abstract class
– Interface

• Abstract class and interface both can have abstract


methods which are necessary for abstraction.
What is Abstract Method ?
• A method which is declared abstract and has no
body is called abstract method.
• It can be declared inside the abstract class only.
• Its implementation must be provided by
derived classes.
• For example:
– public abstract void draw();  
– An abstract method in C# is internally a virtual
method so it can be overridden by the derived class.
What is Abstract class ?
• In C#, abstract class is a class which is declared abstract.

• It can have abstract and non-abstract methods.

• It cannot be instantiated. Its implementation must be


provided by derived classes.

• Here, derived class is forced to provide the


implementation of all the abstract methods.
• Syntax
modifier abstract class className
{
//declare fields (which can contain assignments)
modifier dataType variableName;
//declare methods modifier
abstract dataType methodName();
}
modifier class childClass : className
{
override modifier dataType methodName()
}
Example
using System;
abstract class Animal class Program
{
protected string nm; {
public abstract void animalSound();
public void sleep() static void Main()
{
Console.WriteLine("Zzz"); {
}
} Dog d1 = new Dog();
class Dog : Animal
{ d1.animalSound();
public Dog()
{ d1.sleep();
this.nm="Tomy";
} }
public override void animalSound()
{ }
Console.WriteLine("The Dog name:"+nm);
Console.WriteLine("The Dog says:wow wow wow");
}
}
Method Overriding
• If derived class defines same method as defined in
its base class, it is known as method overriding in
C#.

• It is used to achieve runtime polymorphism.

• It enables you to provide specific implementation of


the method which is already provided by its base
class.
• Keywords in Method Overriding
• There are the following 3 types of keywords
used in C# for method overriding:
1. virtual keyword: This modifier or keyword use within
base class method. It is used to modify a method in
base class for overridden that particular method in the
derived class.

2. override: This modifier or keyword use with derived


class method. It is used to modify a virtual or abstract
method into derived class which presents in base class.

3. base Keyword: This is used to access members of the


base class from derived class. It basically used to access
constructors and methods or functions of the base
class.
1. public virtual int myValue()  
{  
     -  
     -  
     -   
}  

2. public override int myValue()  
{  
      -  
      -  
      -  
}  

3. base.myValue();  
Example
using System;
class baseClass
{
public virtual void Greetings()
{ Console.WriteLine("baseClass Saying Hello!"); }
}
class subClass : baseClass
{
public override void Greetings()
{
base.Greetings();
Console.WriteLine("subClass Saying Hello!");
}
}
class Program
{
static void Main()
{
baseClass obj1 = new subClass();
obj1.Greetings();
Console.ReadLine();
}
}

You might also like