Anda di halaman 1dari 24

Sealed Class

A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent accidental inheritance of the class. It is not permitted to use the abstract modifier with a sealed class.

// cs_sealed_keyword.cs // Sealed classes using System; sealed class MyClass { public int x; public int y; }

class MainClass { public static void Main() { MyClass mC = new MyClass(); mC.x = 110; mC.y = 150; Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); }

Abstract modifier
The abstract modifier can be used with classes, methods, and properties. Abstract classes have the following features: An abstract class cannot be instantiated. An abstract class may contain abstract methods and accessors. It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.

Abstract methods
Abstract methods have the following features: An abstract method is implicitly a virtual method. Abstract method declarations are only permitted in abstract classes. Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ })following the signature.

For example: public abstract void MyMethod(); The implementation is provided by an overriding method, which is a member of a non-abstract class. It is an error to use the static, virtual, or override modifiers in an abstract method declaration.

Abstract properties
Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax. It is an error to use the abstract modifier on a static property. An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

// abstract_keyword.cs // Abstract Classes using System; abstract class MyBaseC // Abstract class { protected int x = 100; protected int y = 150; public abstract void MyMethod(); // Abstract method public abstract int GetX

{ get; } public abstract int GetY // Abstract property { get; }} class MyDerivedC: MyBaseC {

x++; y++; } public override int GetX // overriding property { get { return x+10; }} public override int GetY // overriding

{ get { return y+10; }} public static void Main() { MyDerivedC mC = new MyDerivedC(); mC.MyMethod();

Console.WriteLine("x = {0}, y = {1}", mC.GetX, mC.GetY); } } Output x = 111, y = 161 In the preceding example, if you attempt to instantiate the abstract class by using a statement like this: MyBaseC mC1 = new MyBaseC(); // Error you will get the following error message: Cannot create an instance of the abstract class 'MyBaseC'.

new keyword
In C#, the new keyword can be used as an operator or as a modifier. new operator Used to create objects on the heap and invoke constructors. new modifier Used to hide an inherited member from a base class member.

virtual keyword
The virtual keyword is used to modify a method or property declaration, in which case the method or the property is called a virtual member. The implementation of a virtual member can be changed by an overriding member in a derived class. When a virtual method is invoked, the run-time type of the object is checked for an overriding member.

The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. By default, methods are non-virtual. You cannot override a non-virtual method. You cannot use the virtual modifier with the following modifiers: static abstract override

Interface:Multiple Inheritance
Using system; interface addition{ int Add(); } interface Multiplication{ int Mul(); } Class Computation : Addition,Multiplication { int x,y;

public Computation (int x, int y) //Constructor { this.x=x; this.y=y; } public int Add() { return (x+y); } public int Mul() { return (x*y); } }

class InterfaceTest1 { psvM() { Computation com= new Computation(10,20); Addition add=(Addition) com; //Casting Console.WriteLine(Sum =+add.Add)); Addition add=(Addition) com; //Casting Console.WriteLine(Sum =+add.Add)); Multiplication Multiplication mul=(Multiplication) com; //Casting Console.WriteLine(Product = +mul.Mul));

Anda mungkin juga menyukai