Sealed
Sealed
• using System;
• public class web {
• class GFG {
• string name = “Programming"; • static void Main()
• public virtual void showdata()
• {
• {
• Console.WriteLine("Website Name: " + name); • stream E = new
• } stream();
• }
• class stream : web {
• E.showdata();
• string s = "Computer Science"; •
• public override void showdata()
• {
• }
• base.showdata(); • }
• Console.WriteLine("About: " + s);
• }
• }
• Note:
• Method overriding is possible only in derived classes. Because a
method is overridden in the derived class from the base class.
• A non-virtual or a static method can’t be overridden.
• Both the override method and the virtual method must have the
same access level modifier.
• The overridden base method cannot be a sealed method
Hiding Methods
• C# also provides a concept to hide the methods of the base class from
derived class, this concept is known as Method Hiding. It is also known
as Method Shadowing.
• you can hide the implementation of the methods of a base class from the
derived class using the new keyword.
• Method Hiding/Shadowing is also an approach of re-implementing the
parent class methods under the child class exactly with the same
signature (same name and same parameters).
• The child classes can re-implement any method of its parent class
methods even if they are not declared as virtual. That means here the
child class re-implements the parent class methods without taking any
permission from its parent.
• using System; • class Program
• namespace MethodHiding
• {
• {
• public class Parent • static void Main(string[]
• { args)
• public virtual void Show()
• {
• {
• Console.WriteLine("Parent Class Show Method"); • Child obj = new Child();
• }
• public void Display()
• obj.Show();
• { • obj.Display();
• Console.WriteLine("Parent Class Display Method"); Output:
• }
• Console.ReadKey();
• } • }
• public class Child : Parent
• {
• }
• } • }
• using System;
• namespace MethodHiding • //Method Hiding/Shadowing
• { • public new void Display()
• public class Parent • {
• { • Console.WriteLine("Child Class Display Method");
• public virtual void Show() • }
• {
• }
• Console.WriteLine("Parent Class Show Method");
• class Program
• }
• {
• public void Display()
• static void Main(string[] args)
• {
• {
• Console.WriteLine("Parent Class Display Method");
• }
• Child obj = new Child();
• } • obj.Show();
• public class Child : Parent • obj.Display();
• { • Console.ReadKey();
• //Method Overriding • }
• public override void Show() • }
• { • }
• Console.WriteLine("Child Class Show Method");
• } • Output:
• Hiding an inherited member using new does not remove the member,
it only makes the member inaccessible in the derived class.
• Class A • The new F() in B has private
• { access, its scope does not
• public static void F() extend to the class C.
• }
• Class B:A
• Thus the call F() in C, is valid,
• { invokes F() of A class.
• new private static void F()
• {
• }
• }
• Class C:B
• {
• Static void F1()
• {
• F();
• }
• }
Sealed class
• Sealed classes are used to restrict the users from inheriting the class. A class can be
sealed by using the sealed keyword.
•
• The keyword tells the compiler that the class is sealed. No class can be derived from
a sealed class.
• sealed class class_name
• {
• // data members
• // methods
• .
• .
• .
• }
• using System;
• namespace SealedClass {
• sealed class Animal {
•
• }
•
• class Dog : Animal {
•
• }
• class Program {
• static void Main (string [] args) {
•
• Dog d1 = new Dog();
• Console.ReadLine(); Since a sealed class cannot be inherited, the program
• } generates the following error:
• }
error CS0509: 'Dog': cannot derive from sealed type 'Animal'
• }
• using System;
• sealed class SealedClass {
• public int Add(int a, int b)
• {
• return a + b;
• }
• }
•
• class Calculator{
• static void Main(string[] args)
• {
• // Creating an object of Sealed Class
• SealedClass slc = new SealedClass();
•
• // Performing Addition operation
• int total = slc.Add(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• }
• }
• using System;
• sealed class SealedClass { sealed class Calculation:SealedClass {
• public int Add(int a, int b) public int Sub(int a, int b)
• { {
return a - b;
• return a + b; }
• } }
• }
•
• class Calculator{
• static void Main(string[] args)
• {
• Calculation slc = new Calculation ();
•
• int total = slc.Add(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• total = slc.Sub(6, 4);
• Console.WriteLine("Total = " + total.ToString());
• }
• }
Sealed Method
• During method overriding, if we don't want an overridden method
to be further overridden by another class, we can declare it as
a sealed method.
• However, a method can be sealed in the classes in which they have
been inherited.
example of a sealed method in a derived class
• using System;
• class Printer {
•
• // compiler error : 'Officejet.show()' : cannot override inherited member 'LaserJet.show()' because it is sealed.
• override public void print()
• {
• Console.WriteLine("Officejet printer printing....");
• }
• }
• // Driver Class
• class Program {
• // Driver Code
• static void Main(string[] args)
• {
• Printer p = new Printer();
• p.show();
• p.print();
• Instance variables:
• int pan to store personal account number
• String name to store name
• double tax income to store annual taxable income
• double tax to store tax that is calculated
• Member functions:
• input ( ) Store the pan number, name, taxable income
• calc( ) Calculate tax for an employee
• display ( ) Output details of an employee.
• Project to implement run time polymorphism by creating a base class
with two methods. In the first method, call the second method.
Inherit a class and override the second method. create an object of
the derived class.