Difference between revisions of "Abstract"

From Logic Wiki
Jump to: navigation, search
m (1 revision imported)
Line 8: Line 8:
  
 
if all the members are abstract class can be defined as [[interface]]
 
if all the members are abstract class can be defined as [[interface]]
 +
 +
<pre class="brush:csharp;">
 +
public abstract class Shape
 +
{
 +
  public abstract void Draw();
 +
 +
</pre>
 +
 +
Draw method cannot have a method body,
 +
 +
Derived class must implement all abstract members in the base abstract class
 +
 +
Abstract classes cannot be instantiated
 +
 +
=== Why to use Abstract ? ===
 +
When you want to provide some common behaviour, while forcing other developers to follow your design
 +
 +
=== Virtual vs Abstract ===
 +
Virtual can be overridden but not a must and it can have a body.
 +
Abstract must be overridden in derived classes and cannot have a body.
 +
 +
=== Interface vs Abstract ===
 +
in Abstract classes there can be implementations
 +
 +
---------------
 +
Related Subjects : [[interface]]  [[Polymorphism]]

Revision as of 10:36, 13 June 2017


Once you define the name but not implement the body and all child classes should implement their own bodies under defined name it's called abstract.

if you set a method as abstract in a class, you have to define class itself as abstract

if all the members are abstract class can be defined as interface

public abstract class Shape
{
  public abstract void Draw();
}  

Draw method cannot have a method body,

Derived class must implement all abstract members in the base abstract class

Abstract classes cannot be instantiated

Why to use Abstract ?

When you want to provide some common behaviour, while forcing other developers to follow your design

Virtual vs Abstract

Virtual can be overridden but not a must and it can have a body. Abstract must be overridden in derived classes and cannot have a body.

Interface vs Abstract

in Abstract classes there can be implementations


Related Subjects : interface Polymorphism