WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
Abstrcation
1. Abstraction
• Abstract means something that is not concreate
• Concrete classes are those that can be instantiated, that is,
an object of the class can be created
• Class A { … } // A is a concrete class
• A a = new A() // can create object of type class A
• Abstraction is achieved in C# by classes that are defined as
abstract
• An abstract class cannot be instantiated
Saifut
1
2. Abstraction
• The word before the class indicates that the class is abstract
• abstract class A { … } // class A is abstract
• A a = new A(); // Error, cannot create object from abstract class
• An abstract class can have attributes, properties and
methods
• Additionally an abstract class can have abstract methods
• Abstract methods only have the header without a body or
implementation
Saifut
2
3. Abstraction
• Account is an abstract class
• It has a private instance variable called balance
• It has a constructor to set the value of balance
• It has Deposit() method that is not abstract
• It also has an abstract method Withdraw()
Saifut
3
abstract class Account
{
private double balance;
public Account(double balance) { this.balance = balance; }
public double BALANCE {
get { return balance; }
set { balance = value; }
}
public void Deposit(double amt) { balance += amt; }
public abstract void Withdraw(double amt);
}
4. Abstraction
• The Saving class inherits from Account class
• Any method that is abstract in the base class must be implemented in
the derived or inheriting class
• Another class Checking may have a different implementation of
Withdraw() as shown below
Saifut
4
class Saving : Account
{
private double penalty;
public Saving(double balance, double penalty) : base(balance)
{
this.penalty = penalty;
}
public override void Withdraw(double amt)
{
BALANCE -= amt - penalty;
}
}
5. Abstraction
Saifut
5
class Checking : Account
{
public Checking(double balance) : base(balance)
public override void Withdraw()
{
BALANCE -= amt ;
}
}
6. Abstraction
• Why use abstract classes
• There is a need for a base class to hold common instance variables and
methods
• Each type of Account will have a balance
• Object of the base class should not be created
• There cannot be a generic account.
• In our example an account must be either Checking or Saving (there could be other types
of accounts as well, such, student, investment etc.)
• There is a need for a method that each inheriting class must implement.
However there is no default implementation of the method
• Withdraw() method needs to be implemented by each account type
• Account class does not know how withdraw for each account type should work
Saifut
6
7. Abstraction
• Let’s look at another example
We want to create different kinds of shapes. However, there is a
common attribute called sides, which holds the number of sides of the
shape. In addition we want to calculate the area of the shape. Notice
that the generic shape does not know how to calculate the area. Also
note that we cannot create a generic shape. A shape must be some
geometrical shape, like a circle, triangle or rectangle etc.
The class Shape fits the need for an abstract class.
Saifut
7
8. Abstraction
Saifut
8
abstract class Shape
{
protected int sides;
public Shape(int n) { sides = n; }
public double Area();
}
public class Circle : Shape
{
public Circle() : base(1) { }
public override double Area(double r)
{
return 3.14 * r * r;
}
}
public class Rectangle : Shape
{
public Rectangle() : base(4) { }
public override double Area(double l,
double w)
{
return l * w;
}
}
class Triangle : Shape
{
public Triangle() : base(3) { }
public override double Area(double b,
double h)
{
return (b * h)/2;
}
}