SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
C# Tutorial
Part 8:
Abstract Class vs
Interface
www.sirykt.blogspot.com
• Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process
to hide the internal details and showing functionality only.
• Abstraction can be achieved by two ways:
• Abstract class
• Abstract class and interface both can have abstract methods which are necessary for
abstraction.
• Abstract Method:
• A method which is declared abstract and has no body is called abstract method. It can be
declared inside the abstract class only. Its implementation must be provided by derived
classes.
• For example:public abstract void draw();
• An abstract method in C# is internally a virtual method so it can be overridden by the derived
class. You can't use static and virtual modifiers in abstract method declaration.
• C# Abstract class
• In C#, abstract class is a class which is declared abstract. It can have abstract and non-
abstract methods. It cannot be instantiated. Its implementation must be provided by derived
classes. Here, derived class is forced to provide the implementation of all the abstract
methods.
• Let's see an example of abstract class in C# which has one abstract method draw(). Its
• Let's see an example of abstract class in C# which has one abstract method draw(). Its
implementation is provided by derived classes: Rectangle and Circle. Both classes have
different implementation.
• using System;
• public abstract class Shape
• { public abstract void draw(); }
• public class Rectangle : Shape
• { public override void draw()
• { Console.WriteLine("drawing rectangle...");
• } }
• public class Circle : Shape
• { public override void draw()
• { Console.WriteLine("drawing circle..."); } }
• public class TestAbstract
• { public static void Main()
• { Shape s;
• s = new Rectangle();
• s.draw();
• s = new Circle();
• s.draw(); } }
• Output:
• drawing ractangle... drawing circle...
• C# Interface
• Interface in C# is a blueprint of a class. It is like abstract class because all the
methods which are declared inside the interface are abstract methods. It cannot
have method body and cannot be instantiated.
• It is used to achieve multiple inheritance which can't be achieved by class. It is
used to achieve fully abstraction because it cannot have method body.
• Its implementation must be provided by class or struct. The class or struct which
implements the interface, must provide the implementation of all the methods
declared inside the interface.
• Note:
• Interface methods are public and abstract by default. You cannot explicitly use
public and abstract keywords for an interface method.Interfaces define properties,
methods, and events, which are the members of the interface. Interfaces contain
only the declaration of the members.
• It is the responsibility of the deriving class to define the members. Interfaces are
declared using the interface keyword. It is similar to abstract class declaration.
Interface statements are public by default
Fully abstract class(interface)
• interface: it maintains only method declarations
• syntax:
• interface iname
• {void esal();
• void tsal(); }
• Interface are similar to abstract classes.
• Interfaces contains only methods without body.
These interfaces are called fully abstract classes.
• Interfaces supports a structure like multiple
inheritance. Interfaces cannot be instantiated .
• Let's see the example of interface in C# which has draw() method. Its implementation is
provided by two classes: Rectangle and Circle.
• using System;
• public interface Drawable
• { void draw(); }
• public class Rectangle : Drawable
• { public void draw()
• { Console.WriteLine("drawing rectangle...");
• } }
• public class Circle : Drawable
• { public void draw()
• { Console.WriteLine("drawing circle...");
• } }
• public class TestInterface
• { public static void Main()
• { Drawable d;
• d = new Rectangle();
• d.draw();
• d = new Circle();
• d.draw(); } }
• Output:
• drawing ractangle... drawing circle...
• Note:
• Interfaces define properties, methods, and events,
which are the members of the interface.
• Interfaces contain only the declaration of the
members. It is the responsibility of the deriving
class to define the members.
• Interfaces are declared using the interface
keyword. It is similar to abstract class declaration.
• Interface statements are public by default
• Sealed class :
• Which class gives the full functionality that classes are called sealed classes.
sealed class there is no child class.
• Sealed is a keyword. sealed class restricts the inheritance. sealed classes having
instance variable. Sealed methods are not overidable
• Note:
• Sealed classes are used to restrict the inheritance feature of object oriented
programming. Once a class is defined as a sealed class, the class cannot be inherited
• example on sealed class:
• using System;
• namespace interfaceexample
• { sealed class emp
• { int sal, bonus, ta, ts;}}
Feature Interface Abstract class
Multiple
inheritance
A class may inherit several interfaces. A class may inherit only one abstract class.
Default
implementation
An interface cannot provide any code, just
the signature.
An abstract class can provide complete, default
code and/or just the details that have to be
overridden.
Access Modfiers
An interface cannot have access modifiers
for the subs, functions, properties etc
everything is assumed as public
An abstract class can contain access modifiers
for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral
abilities of a class. In other words both
Human and Vehicle can inherit from a
IMovable interface.
An abstract class defines the core identity of a
class and there it is used for objects of the
same type.
Homogeneity
If various implementations only share
method signatures then it is better to use
Interfaces.
If various implementations are of the same kind
and use common behaviour or status then
abstract class is better to use.
Speed
Requires more time to find the actual
method in the corresponding classes.
Fast
Adding
functionality
(Versioning)
If we add a new method to an Interface
then we have to track down all the
implementations of the interface and define
implementation for the new method.
If we add a new method to an abstract class
then we have the option of providing default
implementation and therefore all the existing
code might work properly.
Fields and An abstract class can have fields and constrants
SIRYKT
Sharing Knowledge is Learning
For more updates
For more visit our website www.sirykt.blogspot.com

More Related Content

PDF
8 abstract classes and interfaces
PPTX
interface in c#
PPTX
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
PPTX
Interfaces c#
PPT
Java Programming - Abstract Class and Interface
PPTX
Interface in java
PPT
PPTX
Java interfaces
8 abstract classes and interfaces
interface in c#
C# Interface | Interfaces In C# | C# Interfaces Explained | C# Tutorial For B...
Interfaces c#
Java Programming - Abstract Class and Interface
Interface in java
Java interfaces

What's hot (20)

PPTX
Abstract & Interface
PPTX
Understanding Interfaces
PPTX
Interfaces in java
PPT
Chapter 9 Interface
PPTX
Java interface
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
PPT
Interfaces In Java
PPTX
Lecture 18
PDF
Interface
PPT
Java interfaces
PPTX
Java interfaces
PPT
Java interface
PPTX
abstract class and interface.Net
PPTX
Interface java
PPTX
Java interface
PPT
Interfaces
PPTX
java interface and packages
Abstract & Interface
Understanding Interfaces
Interfaces in java
Chapter 9 Interface
Java interface
Interface in java ,multiple inheritance in java, interface implementation
Java OOP Programming language (Part 6) - Abstract Class & Interface
Interfaces In Java
Lecture 18
Interface
Java interfaces
Java interfaces
Java interface
abstract class and interface.Net
Interface java
Java interface
Interfaces
java interface and packages
Ad

Similar to 8abstact class in c# (20)

PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPTX
2CPP14 - Abstraction
PPT
Inheritance
PPTX
Abstraction
PDF
Abstraction in Java: Abstract class and Interfaces
PPT
12.2 Abstract class and Interface.ppt
PPTX
Pi j3.2 polymorphism
PPTX
Abstract classes & interfaces
PPTX
Inheritance.pptx
PPTX
BCA Abstraction.pptx
PPTX
INTERFACES. with machine learning and data
PPT
A1869984431_21789_28_2018_Abstract Class.ppt
PPTX
it is the quick gest about the interfaces in java
PPTX
Better Understanding OOP using C#
PPT
Interface in java By Dheeraj Kumar Singh
PPTX
Interface
PPTX
A class which is declared with the abstract keyword is known as an abstract c...
PPTX
Java interfaces
PPTX
Presentation 1st
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ABSTRACT CLASSES AND INTERFACES.ppt
2CPP14 - Abstraction
Inheritance
Abstraction
Abstraction in Java: Abstract class and Interfaces
12.2 Abstract class and Interface.ppt
Pi j3.2 polymorphism
Abstract classes & interfaces
Inheritance.pptx
BCA Abstraction.pptx
INTERFACES. with machine learning and data
A1869984431_21789_28_2018_Abstract Class.ppt
it is the quick gest about the interfaces in java
Better Understanding OOP using C#
Interface in java By Dheeraj Kumar Singh
Interface
A class which is declared with the abstract keyword is known as an abstract c...
Java interfaces
Presentation 1st
21UCAC31 Java Programming.pdf(MTNC)(BCA)
Ad

More from Sireesh K (20)

PPTX
Cn10
PPTX
chanakya neeti
PPTX
chanakya neeti
DOCX
What is mvc
PPTX
PPTX
31cs
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
PPTX
Cn10
chanakya neeti
chanakya neeti
What is mvc
31cs

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Institutional Correction lecture only . . .
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Institutional Correction lecture only . . .
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

8abstact class in c#

  • 1. C# Tutorial Part 8: Abstract Class vs Interface www.sirykt.blogspot.com
  • 2. • Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only. • Abstraction can be achieved by two ways: • Abstract class • Abstract class and interface both can have abstract methods which are necessary for abstraction. • Abstract Method: • A method which is declared abstract and has no body is called abstract method. It can be declared inside the abstract class only. Its implementation must be provided by derived classes. • For example:public abstract void draw(); • An abstract method in C# is internally a virtual method so it can be overridden by the derived class. You can't use static and virtual modifiers in abstract method declaration. • C# Abstract class • In C#, abstract class is a class which is declared abstract. It can have abstract and non- abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. Here, derived class is forced to provide the implementation of all the abstract methods. • Let's see an example of abstract class in C# which has one abstract method draw(). Its
  • 3. • Let's see an example of abstract class in C# which has one abstract method draw(). Its implementation is provided by derived classes: Rectangle and Circle. Both classes have different implementation. • using System; • public abstract class Shape • { public abstract void draw(); } • public class Rectangle : Shape • { public override void draw() • { Console.WriteLine("drawing rectangle..."); • } } • public class Circle : Shape • { public override void draw() • { Console.WriteLine("drawing circle..."); } } • public class TestAbstract • { public static void Main() • { Shape s; • s = new Rectangle(); • s.draw(); • s = new Circle(); • s.draw(); } } • Output: • drawing ractangle... drawing circle...
  • 4. • C# Interface • Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated. • It is used to achieve multiple inheritance which can't be achieved by class. It is used to achieve fully abstraction because it cannot have method body. • Its implementation must be provided by class or struct. The class or struct which implements the interface, must provide the implementation of all the methods declared inside the interface. • Note: • Interface methods are public and abstract by default. You cannot explicitly use public and abstract keywords for an interface method.Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. • It is the responsibility of the deriving class to define the members. Interfaces are declared using the interface keyword. It is similar to abstract class declaration. Interface statements are public by default
  • 5. Fully abstract class(interface) • interface: it maintains only method declarations • syntax: • interface iname • {void esal(); • void tsal(); } • Interface are similar to abstract classes. • Interfaces contains only methods without body. These interfaces are called fully abstract classes. • Interfaces supports a structure like multiple inheritance. Interfaces cannot be instantiated .
  • 6. • Let's see the example of interface in C# which has draw() method. Its implementation is provided by two classes: Rectangle and Circle. • using System; • public interface Drawable • { void draw(); } • public class Rectangle : Drawable • { public void draw() • { Console.WriteLine("drawing rectangle..."); • } } • public class Circle : Drawable • { public void draw() • { Console.WriteLine("drawing circle..."); • } } • public class TestInterface • { public static void Main() • { Drawable d; • d = new Rectangle(); • d.draw(); • d = new Circle(); • d.draw(); } } • Output: • drawing ractangle... drawing circle...
  • 7. • Note: • Interfaces define properties, methods, and events, which are the members of the interface. • Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. • Interfaces are declared using the interface keyword. It is similar to abstract class declaration. • Interface statements are public by default
  • 8. • Sealed class : • Which class gives the full functionality that classes are called sealed classes. sealed class there is no child class. • Sealed is a keyword. sealed class restricts the inheritance. sealed classes having instance variable. Sealed methods are not overidable • Note: • Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited • example on sealed class: • using System; • namespace interfaceexample • { sealed class emp • { int sal, bonus, ta, ts;}}
  • 9. Feature Interface Abstract class Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class. Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type. Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use. Speed Requires more time to find the actual method in the corresponding classes. Fast Adding functionality (Versioning) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. Fields and An abstract class can have fields and constrants
  • 11. For more updates For more visit our website www.sirykt.blogspot.com