I.T. Discussion Community!
-Collapse +Expand
PHP
Search PHP Group:

Advanced
-Collapse +Expand PHP Store
PRESTWOODSTORE
   ► KBTo/From GuidesReferenceOOP Details  Print This    All Groups  

Abstraction (Cross Ref > OOP Details)

Abstraction

General Info: Abstract Class / Abstract Member

An abstract class member is a member that is specified in a class but not implemented. Classes that inherit from the class will have to implement the abstract member. Abstract members are a technique for ensuring a common interface with descendant classes. An abstract class is a class you cannot instantiate. A pure abstract class is a class with only abstract members.

Languages Focus

Abstraction is supported at various levels with each language. A language could enforce abstraction at the class level (either enforcing a no-instantiation rule or a only abstract members rule), and with class members (member methods and/or properties).

ASP Classic:   Not Supported

C#:   abstract, override

C# supports abstract class members and abstract classes using the abstract modifier.

An abstract class is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

An abstract member is either a method (implicitly virtual), property, indexer, or event in an abstract class. You can add abstract members ONLY to abstract classes using the abstract keyword. Then you override it in a descendant class with Override.

Syntax Example:
abstract public class Cyborg : System.Object
{
  abstract public void Speak(string pMessage);
}
public class Series600 : Cyborg
{
  public override void Speak(string pMessage)  
  {
    MessageBox.Show(pMessage);  
  }
}

An Abstract Example

The following demonstrates the abstract method above. The following form code assumes a form with a button.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CR_Abstraction
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Series600 MyKiller = new Series600();
      MyKiller.Speak("I'll be back.");
      MyKiller.Greet();

    }
  }
 
  abstract public class Cyborg : System.Object
  {
    abstract public void Speak(string pMessage);
    abstract public void Walk();
    public void Greet()
    {
      MessageBox.Show("Hello");
    }
  }
 
  public class Series600 : Cyborg
  {
    public override void Speak(string pMessage)
    {
      MessageBox.Show(pMessage);
    }

    public override void Walk()
    {
         //Implement walk here.
    }
}
}

Abstract Properties

C# does support setting a property to abstract with the abstract keyword. A single property line with the abstract keyword along with indicating empty get; and set; methods is all that's required. In most cases, you'll probably also want to define the property's supporting member field as protected for visibility in descendant classes.

For example, we could enhance our classes above as follows:

public abstract class Cyborg : System.Object
{
protected string cyborgName; //Suggested member field

public abstract string CyborgName {get; set;} //Abstract property.

  public abstract void Speak(string pMessage);
  public abstract void Walk();

public virtual void Greet()
  {
    MessageBox.Show("Hello human");
  }
}

public class Series600 : Cyborg
{
public override string CyborgName
{
get
{
return cyborgName;
}
set
{
cyborgName = value;
}
}
 
public override void Speak(string pMessage)
{
MessageBox.Show(pMessage);
}
 
public override void Walk()
{
//Implement walk here.
}

  public override void Greet()
{
MessageBox.Show("Hello, I am " + CyborgName + ".");
}
}

 

Now you can use our newly modified class as follows:

Series600 MyKiller = new Series600();
 
MyKiller.CyborgName = "John";
MyKiller.Greet();
MyKiller.Speak("I'll be back.");

First Class Requirement

Visual Studio requires that designers use the first class in the file. Notice above that our Cyborg and Series600 classes come after the Form1 class. This is not normally an issue because classes are usually put in their own class file. For demos, I like to put the classes in a simple project with a single form with a single button on it that exercises the exercise.

Summary

Abstraction is an important aspect of your software design and C# implements a robust set of abstract features. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.



Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: True or False?

You can add abstract members ONLY to abstract classes using the abstract keyword. Then you override it in a descendant class with Override.

Answer:
  • True
  • False
  • More Info

    Definition:  Abstract Class / Abstract Member
    Code:  C# Abstraction (abstract, override)

    C++:   =0 in a virtual method

    AbstractMemberFunction is a pure virtual function makes this class Abstract class indicated by the "=0"and NonAbstractMemberFunction1 is a virtual function.

    Syntax Example:
    class AbstractClass {
    public:
    virtual void AbstractMemberFunction() = 0;
      virtual void NonAbstractMemberFunction1();

    };

    Summary

    Abstraction is an important aspect of your software design. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

    More Info

    Definition:  Abstract Class / Abstract Member
    Code:  C++ Abstraction (=0 in a virtual method)

    Corel Paradox:   Not Supported

    Delphi:   abstract, override

    Delphi for Win32 supports abstract class members using the abstract keyword. You can even instantiate instances of a class that contains abstract members. Then you override each abstract member in a descendant class with Override.

    Delphi does not support setting an entire class as abstract. You can create an abstract class (a class with one or more abstract methods), but there is no way to tell the compiler to not allow the instantiation of the abstract class.

    Delphi does not support abstract member properties directly. To implement an abstract properity, make use of abstract methods. That is, you can read a GetPropertyX abstract function and write to a SetPropertyX abstract procedure. In effect, creating  an abstract property.

    Syntax Example:
    TCyborg = class(TObject)
    public
      procedure Speak(pMessage: String); virtual; abstract;
      procedure Walk; virtual; abstract;
    end;
     
    TSeries600 = class(TCyborg)
    public
      procedure Speak(pMessage: String); override;
      procedure Walk; override;
    end;

    Now let's dig deeper...

    Virtual Abstract versus Dynamic Abstract

    For abstract methods, you must specify either regular virtual with the virtual keyword or dynamic virtual with the dynamic keyword. In Delphi for Win32, virtual methods are optimized for speed and dynamic methods are optimized for size. The Delphi help indicates to use virtual for most situations.

    It is true that the compiler could make virtual the default and therefore optional but requiring one or the other is consistent with Object Pascal's strong typing. However, with Delphi Prism, the use of the virtual keyword with abstract methods is optional.

    An Abstract Example with an Abstract Property

    The following demonstrates the abstract method above and also contains an abstract property (or rather, a regular property that makes use of an abstract method). The following form unit assumes a form with a button.

    unit AbstractionUnit;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
     
      TCyborg = class(TObject)
      private
        FCyborgName: String;
      protected
        procedure SetCyborgName(const Value: String); virtual; abstract;
      public
        property CyborgName: String read FCyborgName write SetCyborgName;
        procedure Speak(pMessage: String); virtual; abstract;
        procedure Walk; virtual; abstract;
      end;
     
      TSeries600 = class(TCyborg)
      protected
        procedure SetCyborgName(const Value: String); override;
      public
        procedure Speak(pMessage: String); override;
        procedure Walk; override;
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    procedure TSeries600.Speak(pMessage: String);
    begin
      ShowMessage(pMessage);
    end;
     
    procedure TSeries600.SetCyborgName(const Value: String);
    begin
      FCyborgName := Value;
    end;
     
    procedure TSeries600.Walk;
    begin
      //Implement here.
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var
      MyKiller: TSeries600;
    begin
      MyKiller := TSeries600.Create;
      MyKiller.CyborgName := 'John';
      MyKiller.Speak('I am ' + MyKiller.CyborgName + '.');
      MyKiller.Speak('I''ll be back.');
    end;
     
    end.

    Summary

    Abstraction is an important aspect of your software design. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

    More Info

    Definition:  Abstract Class / Abstract Member
    Code:  Delphi Abstraction (abstract, override)

    Delphi Prism:   abstract, override

    Prism supports abstract class members and abstract classes using the abstract keyword.

    An abstract class is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

    An abstract member is either a method (method, procedure, or function), a property, or an event in an abstract class. You can add abstract members ONLY to abstract classes using the abstract keyword.

    Alternatively, you can use the empty keyword in place of abstract if you wish to instantiate the abstract class. Then you override it in a descendant class with Override.

    Syntax Example:
    Cyborg = public abstract class(System.Object)
    public
    //You can put "virtual; abstract;"
      //but it's implied with just "abstract;"
      method Speak(pMessage: String); abstract; 
    method Walk; virtual; abstract;
    end;
     
    Series600 = public class(Cyborg)
    public
    procedure Speak(pMessage: String); override;
    procedure Walk; override;
    end;

    An Abstract Example

    The following demonstrates the abstract method above and also contains an abstract property (or rather, a regular property that makes use of abstract methods). The following main unit assumes a form with a button.

    namespace CR_Abstraction;
     
    interface
     
    uses
    System.Drawing,
    System.Collections,
    System.Collections.Generic,
    System.Linq,
    System.Windows.Forms,
    System.ComponentModel;
     
    type
    ///
    /// Summary description for MainForm.
    ///

    MainForm = partial class(System.Windows.Forms.Form)
    private
    method button1_Click(sender: System.Object; e: System.EventArgs);
    protected
    method Dispose(disposing: Boolean); override;
    public
    constructor;
    end;
     
      Cyborg = public abstract class(System.Object) 
    protected
    //Abstract methods for property.
    method GetCyborgName: String; abstract;
    method SetCyborgName(Value: String); abstract;

    public
    //Abstract property, regular property that uses abstract methods.
    property CyborgName: String read GetCyborgName write SetCyborgName;
    method Speak(pMessage: String); abstract;
    method Walk; virtual; abstract;
    end;
     
      Series600 = public class(Cyborg)
    private
    FCyborgName: String;
    protected
    method GetCyborgName: String; override;
    method SetCyborgName(Value: String); override;

    public
    method Speak(pMessage: String); override;
    method Walk; override;
    end;
     
    implementation
     
    {$REGION Construction and Disposition}
    constructor MainForm;
    begin
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();
      //
    // TODO: Add any constructor code after InitializeComponent call
    //
    end;
     
    method MainForm.Dispose(disposing: Boolean);
    begin
    if disposing then begin
    if assigned(components) then
    components.Dispose();
     
          //
    // TODO: Add custom disposition code here
    //
    end;
      inherited Dispose(disposing);
    end;
    {$ENDREGION}
     
    method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
    begin
    var MyKiller: Series600;
     
      MyKiller := New Series600;
     
      MyKiller.CyborgName := 'John';
    MyKiller.Speak('I am ' + MyKiller.CyborgName + '.');
    MyKiller.Speak('I am alive.');
    end;
     
    method Series600.Speak(pMessage: String);
    begin
    MessageBox.Show(pMessage);
    end;
     
    method Series600.Walk;
    begin
    //Impliment walk here.
    end;
     
    method Series600.GetCyborgName: String;
    begin
    Result := FCyborgName;
    end;
     
    method Series600.SetCyborgName(Value: String);
    begin
    FCyborgName := Value;
    end;
     
    end.

    Summary

    Abstraction is an important aspect of your software design and Prism implements a robust set of abstract features. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

    More Info

    Definition:  Abstract Class / Abstract Member
    Code:  Delphi Prism Abstraction (abstract, override)

    Java:   abstract

    Java supports marking a full class as abstract as well as class members. A subclass must either implement the abstract members or you must declare the subclass abstract (which delays the implementation to it's subclass).

    Syntax Example:
    public abstract class Dog {
      abstract void Bark();
    }

    Summary

    Abstraction is an important aspect of your software design and Java implements a robust set of abstract features. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

    More Info

    Definition:  Abstract Class / Abstract Member
    Code:  Java Abstraction (abstract)

    VB.Net:   MustInherit, MustOverride, Overrides

    VB.Net supports abstract class members and abstract classes using the MustInherit and MustOverride modifiers.

    An abstract class is indicated with a MustInherit modifier and is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

    An abstract member is either a method (implicitly virtual), property, indexer, or event in an abstract class. You can add abstract members ONLY to abstract classes using the MustOverride keyword. Then you override it in a descendant class with Overrides.

    Syntax Example:
    Public MustInherit Class Cyborg
      Public MustOverride Sub Speak(ByVal pMessage As String)
    End Class

    Public Class Series600
      Inherits Cyborg

      Public Overrides Sub Speak(ByVal pMessage As String)
        MessageBox.Show(pMessage)
      End Sub
    End Class

    An Abstract Example

    The following demonstrates the abstract method above. The following assumes a form with a button.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object,_
     ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyKiller As New Series600

    MyKiller.Greet()
    MyKiller.Speak("I am alive.")

    End Sub
    End Class

    Public MustInherit Class Cyborg
    Public MustOverride Sub Speak(ByVal pMessage As String)
    Public MustOverride Sub Walk()

      'You can implement methods in an abstract class!
    Public Overridable Sub Greet()
    MessageBox.Show("Hello human")
    End Sub
    End Class
     
    Public Class Series600
    Inherits Cyborg

      Public Overrides Sub Greet()
    MessageBox.Show("Hello")
    End Sub

    Public Overrides Sub Speak(ByVal pMessage As String)
    MessageBox.Show(pMessage)
    End Sub

      Public Overrides Sub Walk()
    'Implement walk here.
    End Sub
    End Class

    Abstract Properties

    VB.Net does support setting a property to abstract with the MustOverride keyword. A single property line with the MustOverride keyword is all that's required. In most cases, you'll probably also want to define the property's supporting member field as protected for visibility in descendant classes.

    For example, we could enhance our classes above as follows:

    Public MustInherit Class Cyborg
      //You can declare your member field either here
    //or in the descendant class (probably better here though).
    Protected FCyborgName As String

    //This is all you have to do in the abstract class!
    Public MustOverride Property CyborgName() As String

    Public MustOverride Sub Speak(ByVal pMessage As String)
    Public MustOverride Sub Walk()
       
    'You can implement methods!
    Public Overridable Sub Greet()
    MessageBox.Show("Hello human")
    End Sub
    End Class
      
    Public Class Series600
    Inherits Cyborg

      Public Overrides Property CyborgName() As String
    Get
    Return FCyborgName
    End Get
        Set(ByVal value As String)
    FCyborgName = value
    End Set
    End Property


      Public Overrides Sub Greet()
    MessageBox.Show("Hello, my name is " & CyborgName)
    End Sub

    Public Overrides Sub Speak(ByVal pMessage As String)
    MessageBox.Show(pMessage)
    End Sub

      Public Overrides Sub Walk()
    'Implement walk here.
    End Sub
    End Class

     

    Now you can use our new class as follows:Dim MyKiller As New Series600

    MyKiller.CyborgName = "John"
    MyKiller.Greet()
    MyKiller.Speak("I am alive.")

    Summary

    Abstraction is an important aspect of your software design and Prism implements a robust set of abstract features. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

    More Info

    Definition:  Abstract Class / Abstract Member
    Code:  VB.Net Abstraction (MustInherit, MustOverride, Overrides)





    Follow PrestwoodBoards on: 


    ©1995-2019 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech