the abstract class abstractionmodifier can be used with classes and methods
The purpose of defining an abstract class is to provide a generic form that can be shared by its subclasses.
Subclasses can extend abstract classes according to their needs.
Abstract classes cannot be instantiated.
Abstract methods have no function body.
The abstract method must give a specific implementation in the subclass.
A class becomes an abstract class in the following cases:
★ When one or more methods of a class are abstract methods.
★ When the class is a subclass of an abstract class, and no implementation details or method bodies are provided for all abstract methods.
★ When a class implements an interface and does not provide implementation details or method principals for all abstract methods.
Abstract class Employee {
public int basic = 2000;
public abstract void salary ();//abstract method
}
Class manager:employee{
public override void Salary () {
Console.WriteLine ("Salary equals" +basic*5); }
}
Class Worker:employee {
public override void Salary () {
Console.WriteLine ("Salary equals" +basic*2); }
}
"abstract classes and abstract methods"
- The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that the class can only be a base class for other classes and cannot be instantiated. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from an abstract class.
Quote Abstract class classone{
Class implementations-defining abstract methods, defining abstract accessors, etc.
}
- Characteristics of abstract Classes:
- Abstract classes cannot be instantiated.
- Abstract classes can contain abstract methods and abstract accessors.
- You cannot modify an abstract class with the sealed modifier.
- A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.
- Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain an implementation. Abstract methods have the attributes:
- Abstract methods are implicit virtual methods that allow only abstract method declarations to be used in abstract classes.
- Abstract method declarations do not provide the actual implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly braces ({}) after the signature.
- The method implementation has an overriding method implementation in a non-abstract class.
- The static or virtual modifier cannot be used in an abstract method declaration.
- In a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier.
Abstract class Shapesclass {abstract public int area ();
}
Class Square:shapesclass {
int x, y;
public override int Area () {
return x * y;
}
The methods in an abstract class are not necessarily abstract, and abstract classes can also accommodate methods that have specific implementations or are called concrete methods. But classes that contain abstract methods are necessarily abstract classes.
"Abstract keyword"
The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can be a base class only for other classes. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from an abstract class.
Abstract class shapesclass{abstract public int area ();
}
Class square:shapesclass{
int x, y;
Not providing a area method results
In a compile-time error.
public override int Area () {
return x * y;
}
}
abstract classes have the following characteristics:
- Abstract classes cannot be instantiated.
- Abstract classes can contain abstract methods and abstract accessors.
- You cannot modify an abstract class with the sealed (C # Reference) modifier, which means that an abstract class cannot be inherited.
- A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.
Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain an implementation.
abstract methods have the following characteristics:
- An abstract method is an implicit virtual method.
- Only abstract method declarations are allowed in abstract classes.
- Because an abstract method declaration does not provide an actual implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly braces ({}) after the signature.
public abstract void MyMethod ();
- The implementation is provided by an override method of override, which is a member of the non-abstract class.
- It is an error to use the static or virtual modifier in an abstract method declaration.
Abstract properties behave as abstract methods, except in the syntax of declarations and invocations.
- It is an error to use the abstract modifier on a static property.
- In a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier.
An abstract class must provide an implementation for all interface members.
an abstract class that implements an interface can map an interface method to an abstract method.
Interface I {void M ();
}
Abstract class C:i {
public abstract void M ();
}
"The difference between a virtual method and an abstract method"
Virtual method
- Modify with Virtual
- To have a method body, even a semicolon
- You can override the quilt class
- In addition to sealed class read-out can be rewritten
Abstract Methods
- To modify with an abstract
- Method body is not allowed
- You must override the quilt class
- Only in abstract classes
Abstract classes, abstract methods, and virtual methods in C #