Abstract

Applies to classes, methods, properties


Abstract Classes

An abstract class cannot be instantiated
An abstract class can only be used as a base class
An abstract class can contain abstract methods and properties
cannot be instantiated
advantage is that it enforces certain hierarchies for all the sub classes
can provide complete, default code and/or just the details that need to be overridden
advantage is that it allows implementations to share common code


Abstract Interfaces

Interfaces
An abstract modifier introduces a new virtual method but does not provide an implementation for it.
An abstract method is only allowed in an abstract class.
The implementation must be implemented in a derived class.
An abstract modifier is also a virtual modifier by default [[copy and paste from classes top level page]]


Interface MyInterface 
{
   void method_Name();
}
public class BaseClass : MyInterface
{
   public abstract public void method_Name(); }

public class DerivedClass : BaseClass
{
   public override void method_Name()
   {
      //implementation
   }
}

Abstract Properties



Abstract Method

A method must be overridden
An abstract method or property can only be declared in abstract classes
An abstract method or property does not contain any implementation
An abstract method or property cannot be used with static, virtua or override.
Derived classes must implement these abstract methods and properties

public abstract class Shape 
{
   public abstract string Name()
}

public class Triangle : Shape
{
   public override string Name()
   {
      return "triangle";
   }
}




© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext