- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private and final methods in C#
Private Methods
To set private methods, use the private access specifier.
Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
Final Methods
For final methods, use the sealed modifier.
When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.
Let us see an example −
The following example won’t allow you to override the method display() because it has a sealed modifier for the ClassTwo derived class.
ClassOne is our base class, whereas ClassTwo and ClassThree are derived classes.
Example
class ClassOne { public virtual void display() { Console.WriteLine("baseclass"); } } class ClassTwo : ClassOne { public sealed override void display() { Console.WriteLine("ClassTwo: DerivedClass"); } } class ClassThree : ClassTwo { public override void display() { Console.WriteLine("ClassThree: Another Derived Class"); } }
Above, under ClassThree derived class we have tried to override the sealed method. This will show an error since it is not allowed when you use the sealed method.
- Related Articles
- Private and final methods in Java Programming
- Private Methods in C#
- Private Methods in Java 9 Interfaces
- Can I overload private methods in Java?
- @SafeVarargs annotation for private methods in Java 9?
- Can we override private methods in Java\n
- Can we override final methods in Java?
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- Private and Protected Members in C++
- Can we use private methods in an interface in Java 9?
- Private Constructors and Singleton Classes in C#
- Difference Between Private and Protected in C++
- What is the drawback of creating true private methods in JavaScript?
- final, finally and finalize in C#
- Why do we need private methods in an interface in Java 9?
