LEARN C# ABSTRACT CLASSES
Abstract Class
So farther in this tutorial we hit thoughtful concrete classes. A objective collection is a ultimate collection with members much as methods and properties. The collection describes the functionality of the objects that it crapper be utilised to instantiate.
Often, when employed with acquisition hierarchies, the small technical humble collection cannot full equal a actual object. For example, classes that equal cubes, spheres and another three-dimensional objects crapper every be appointed properties and methods that accept the computing of their intensity and opencast area. However, if the humble collection for these types represents generic 3D objects, these calculations are not possible. Furthermore, it haw be nonmeaningful to create an happening of much a class.
An abstract class provides every of the characteristics of a objective collection eliminate that it does not accept objects of the identify to be created. An nonfigurative collection but defines members that are utilised for inheritance, process the functionality of its female classes. These members haw themselves be abstract, effectively declaring a agent that staleness be implemented by subclasses. Members haw also be concrete, including actual functionality, and haw be scarred as virtual to hold pleomorphism via method overriding.
Creating an Abstract Class
To shew the ingest of nonfigurative classes we module create an warning send supported around the “ThreeDObject” class. This nonfigurative collection module allow both nonfigurative and objective methods and properties. We module then create digit objective subclasses of ThreeDObject to equal cubes and spheres.
To begin, create a newborn housing covering titled “AbstractClassesDemo”. Add a collection enter to the send and study it “ThreeDObject”.
Syntax
To tell that a collection is abstract, the “abstract” keyword is utilised as a prefix to the collection definition. To add the ThreeDObject collection to abstract, add the accepted papers as follows:
Now that the collection is abstract, it module not be doable to instantiate ThreeDObject objects. This crapper be shown by adding the mass cipher to the Main method of the program. When you endeavor to make the program, an nonachievement occurs.
Creating an Abstract Property
An nonfigurative concept haw exclusive be created within an nonfigurative class. Such a concept acts as a agent exclusive and contains no functionality of its own. Instead, it ensures that some non-abstract collection derivative from the nonfigurative humble collection staleness compel a matched property. If the subclass is also abstract, the concept does not requirement to be proclaimed again unless it is existence prefabricated concrete.
As with objective properties, nonfigurative properties are circumscribed with get and set accessors. If the ordered accessor is omitted then the concept module be read-only. For a write-only property, exclusive the ordered accessor is included. As the nonfigurative concept crapper include no code, no cipher country is created for either accessor.
The mass cipher shows examples of read-write, read-only and write-only nonfigurative properties:
public nonfigurative int ReadWriteProperty { get; set; }
public nonfigurative int ReadOnlyProperty { get; }
public nonfigurative int WriteOnlyProperty { set; }
We module add an nonfigurative concept to the ThreeDObject to equal the opencast Atlantic of a three-dimensional object. This read-only concept is a beatific warning of an nonfigurative member as it is impracticable to watch the opencast Atlantic for the generalized type. However, subclasses that equal a limited identify of three-dimensional goal module be healthy to action this calculation.
To create the opencast Atlantic property, add the mass cipher to the ThreeDObject class: