SlideShare a Scribd company logo
1 of 8
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
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
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);
}
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;
}
}
Abstraction
Saifut
5
class Checking : Account
{
public Checking(double balance) : base(balance)
public override void Withdraw()
{
BALANCE -= amt ;
}
}
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
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
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;
}
}

More Related Content

Similar to Abstrcation

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptx
MahmoodAlashqar
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrption
Mahi Mca
 

Similar to Abstrcation (20)

04cpp inh
04cpp inh04cpp inh
04cpp inh
 
Constructor
ConstructorConstructor
Constructor
 
A1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.pptA1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
 
Classes
ClassesClasses
Classes
 
7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptx
 
Class
ClassClass
Class
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrption
 

More from abdullah619 (12)

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Lists
Lists Lists
Lists
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Exception handling
Exception handlingException handling
Exception handling
 
Polumorphism
PolumorphismPolumorphism
Polumorphism
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)
 
5. linked list
5. linked list5. linked list
5. linked list
 

Recently uploaded

Recently uploaded (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital Businesses
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
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; } }