Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views71 pages

SE OOP Lecture8

Uploaded by

Ali Uskuplo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views71 pages

SE OOP Lecture8

Uploaded by

Ali Uskuplo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

OBJECT ORIENTED PROGRAMMING

BS – (SOFTWARE ENGINEERING)

FALL SEMESTER - 2023

LECTURE # 08

COURSE INSTRUCTOR: AMJAD ALI KHAWAJA

1
Lecture Content.
2

 Abstraction
 Real-Time Examples of Abstraction
 Advantages of Abstraction Principle
 How to Implement Abstraction Principle
 Abstract Class & Abstract Method
 Difference between Abstraction and Encapsulation.
 Interface
 Differences between Class, Abstract Class, and Interface
 How to Define an Interface?
 How to Define Abstract Methods in an Interface?
 Interface Rules.
 Explicit Interface Implementation
 How Interface is Different from a Class?
 What are the Similarities Between the Interface and Abstract Class?
 What is the Difference Between Encapsulation and Abstraction?
 What is the Difference Between Interface and Abstract Class ?
 What are the Advantages of using Interface?

Object Oriented Programming Fall-2023


Any Queries From
Previous Lectures

3
Abstraction
4

 The process of representing the essential features without including the background details

is called Abstraction. In simple words, we can say that it is a process of defining a class by

providing necessary details to call the object operations (i.e. methods) by hiding its

implementation details is called abstraction in C#. It means we need to expose what is

necessary and compulsory and we need to hide the unnecessary things from the outside

world.

 Programmatically we can define abstraction as the process of hiding implementation details

of the object’s feature and showing only the essential information of the feature to the user.

Abstraction lets you focus more on what an object does rather than how it does. That means

what are the services available as part of the class, that we need to expose but how the

services are implemented that we need to hide.

Object Oriented Programming Fall-2023


Real-Time Examples
of Abstraction

5
Real-Time Example of Abstraction
6

 To take a real-time example, when we log in to any social networking

site like Facebook, Twitter, LinkedIn, etc. we enter our user id and

password and then we get logged in. Here we don’t know how they are

processing the data or what logic or algorithm they are using for login.

This information is abstracted/hidden from us since they are not

essential to us. This is basically what abstraction is.

Object Oriented Programming Fall-2023


Real-Time Example of Abstraction
7

 Another real-world example of abstraction could be your TV remote. The


remote has different functions like on/off, changing the channel,
increasing/decreasing volume, etc. You can use these functionalities by just
pressing the button. But, the internal mechanism of these functionalities is
abstracted from us as those are not essential for us to know.
 Another example of abstraction is when you send SMS from your mobile,
you only type the text and send the message. But you don’t know the
internal processing or mechanism of the message delivery system. This is
nothing but abstraction.

Object Oriented Programming Fall-2023


Real-Time Example of Abstraction
8

Abstraction shows only important things to the user and hides the internal details,
for example, when we ride a bike, we only know about how to ride bikes but can
not know about how it work? And also we do not know the internal functionality
of a bike.

Object Oriented Programming Fall-2023


Real-Time Example of Abstraction
9

Another real life example of Abstraction is ATM Machine; All are performing
operations on the ATM machine like cash withdrawal, money transfer, retrieve
mini-statement…etc. but we can't know internal details about ATM.

Object Oriented Programming Fall-2023


Advantages of Abstraction Principle
10

 The Abstraction Principle reduces the complexity of viewing things. It only


provides the method signature by hiding how the method is actually
implemented.
 The Abstraction Principle helps to increase the security of an application or
program as we are only providing the necessary details to call the method
by hiding how the methods are actually implemented.
 With the Abstraction Principle, the enhancement will become very easy
because without affecting end-users we can able to perform any type of
changes in our internal system.
 Without the Abstraction principle, maintaining application code is very
complex. Abstraction gives one structure to program code.

Object Oriented Programming Fall-2023


How to Implement Abstraction Principle
11

 We can implement the abstraction OOPs principle in

two ways. They are as follows:


 Using Abstract Classes and Abstract Methods

 Using Interface

Object Oriented Programming Fall-2023


Abstract Class &
Abstract Method

12
What are Abstract Classes in C#?
13

A class under which we define abstract methods is known as


an abstract class. As per object-oriented programming, we
need to define a method inside a class. And, we cannot define
the abstract methods directly anywhere, we need to define the
abstract method inside an abstract class only.

Object Oriented Programming Fall-2023


What are Abstract Classes in C#?
14

Suppose, we need to write the above Add abstract method inside a class called Calculator,

then that class needs to be declared using the abstract modifier as follows.

public abstract class Calculator


{
public abstract void Add(int num1, int num2);
}

So, when a class contains any abstract methods, then it must and should be declared using
the abstract modifier and when a class is created using an abstract modifier then it is called
an Abstract class in C#.

Object Oriented Programming Fall-2023


What are Abstract Methods in C#?
15

A method without the body is known as Abstract Method, what


the method contains is only the declaration of the method. That
means the abstract method contains only the declaration, no
implementation. The following method is a non-abstract
method as this method contains a body.

public void Add(int num1, int num2)


{
}

Object Oriented Programming Fall-2023


What are Abstract Methods in C#?
16

 But without writing the method body, if we end the method with a
semicolon as follows, then it is called an Abstract Method.
public void Add(int num1, int num2);

 But, remember if you want to make any method an abstract method,


then you should explicitly use the abstract modifier as follows. And
once you use the abstract modifier, automatically the method is
going to be called an abstract method.

public abstract void Add(int num1, int num2);

Object Oriented Programming Fall-2023


What is the use of the Abstract Method?
17

 If a method is declared as abstract under any class, then the child class of that

abstract class is responsible for implementing the abstract method without fail.

 In inheritance, we see that the Parent class provides some properties to the Child

class for consumption. Here also inheritance comes into the picture, but the point
that you need to remember is that the Parent class is an abstract class, and he will
not be providing any properties to the Child class for consumption, rather it imposes
some restrictions on the Child classes. And children or Child classes have to be
followed or fulfill those restrictions.

Note: Every abstract method declared within an abstract class must and should be
implemented by the Child classes without fail else we will get compile time error.

Object Oriented Programming Fall-2023


Is Abstract Class Containing Only Abstract Methods?
18

No, don’t think that an abstract class can contain only


abstract methods. It can also contain non-abstract
methods. The point that you need to remember is, that
if a class is non-abstract then it contains only non-
abstract methods but if a class is abstract then it
contains both abstract and non-abstract methods in
C#.

Object Oriented Programming Fall-2023


Who will Provide Implementation of Abstract Methods?
19

 The Answer is Child Class. If you have a child class of an abstract class, then it is the

responsibility of the child class to provide the implementation for all the abstract methods of
the parent class. You cannot escape. Every method should be implemented. If you implement
all the abstract methods, then only you can consume the non-abstract method of the Parent
class.

 Generally, what we saw in Inheritance is that the child class can directly consume the members

of the parent class. But here this is not possible. You cannot consume directly. The property is
under restrictions. Until and unless the restrictions are fulfilled by the child class, the child class
cannot consume the members of the parent class.

 So, the point that you need to remember is, that in the child class, you need to implement each

and every abstract method of the parent class, and then only you can consume the non-abstract
methods of the parent class.

Object Oriented Programming Fall-2023


Who will Provide Implementation of Abstract Methods in
C#?
20

 Let us compare this with one real-time example. Suppose, the father promised his son, that if you bring

90% of the mark in the annual exam, then he will be rewarded with a laptop. So, the laptop is going to be

given to you only if you bring 90% in the annual exam. Now, if the son wants to get the laptop, then the

son has to fulfill the requirement set by his father. What is the requirement, the requirement is achieving

90% of the mark. Once the son fulfills the requirement i.e. once the son achieves 90% marks in the annual

exam, then the laptop is given to him until then he will not get the laptop.

 This is exactly the same in the case of an abstract class. Abstract class contains both abstract and non-

abstract methods. You can consider the abstract method as Marks obtained on the annual exam and the

non-abstract method as the laptop. So, if you want to get the laptop (i.e. to consume non-abstract

method), you need to fulfill the requirements i.e. get 90% marks in the annual exam (i.e. implement all the

abstract methods).

Note: To define a method as abstract or class as abstract, we require to use the abstract keyword on them.

Object Oriented Programming Fall-2023


Example to Understand Abstract Class and Abstract Methods in
C#:
21

Object Oriented Programming Fall-2023


Can we create an instance of an abstract class?
22

No. We cannot create an instance of an abstract class.


Whether the abstract class contains any abstract
methods or not, it is not possible to create an instance
of the abstract class. If you try, you will get a compile-
time error as shown in the below image.

Object Oriented Programming Fall-2023


Example -- Can we create an instance of an abstract class in C#?
23

Object Oriented Programming Fall-2023


Can we Declare an Abstract Method as Static?
24

No, we are not allowed to declare an abstract method as static. It leads to Compile
Time Error. If the compiler allows us to declare it as static, it can be invoked
directly using the class name which cannot be executed by CLR at runtime. Hence
to restrict calling abstract methods compiler does not allow us to declare an
abstract method as static. For a better understanding, please have a look at the
below image.

Object Oriented Programming Fall-2023


Can we Declare an Abstract Method as Sealed?
25

No, because it should be allowed to override in subclasses. If we will try to use


sealed then we will get a Compile Time Error. For a better understanding, please
have a look at the below image.

Object Oriented Programming Fall-2023


Can we Declare an Abstract Method as Private?
26

No, because it should be inherited by subclasses. It leads to Compile Time


Error: virtual or abstract members cannot be private. For a better
understanding, please have a look at the below image.

Object Oriented Programming Fall-2023


Why Abstract Class Cannot Be Instantiated?
27

 Because it is not a fully implemented class as its abstract methods cannot be

executed. If the compiler allows us to create the object for an abstract class, we
can invoke the abstract method using that object which cannot be executed by
CLR at runtime. Hence to restrict calling abstract methods, the compiler does
not allow us to instantiate an abstract class.

 Now, let us implement the two abstract methods inside the child class. How to

implement the abstract methods means using the override modifier as follows.

Object Oriented Programming Fall-2023


Point To Remember.
28

Now, you can see there is no more compile-time error. Now, the child class fulfills
the requirements of the parent class by implementing the abstract methods, and
hence the child class can now consume the non-abstract methods of the parent
class. So, you can now, create an instance of the Child class and consume all the
members as follows.

Object Oriented Programming Fall-2023


Why Abstract Class Cannot Be Instantiated in C#?
29

Object Oriented Programming Fall-2023


Can we Create a Reference for the Abstract Class?
30

Yes, we can create a reference for the abstract class in C#. But we cannot create an instance
of an abstract class in C#. For a better understanding, please have a look at the below image.
Here, we created an instance of child class i.e. AbsChild, and then we created a reference of
abstract class i.e. AbsParent which is holding the child class instance and then using the
reference we can also access the members.

Object Oriented Programming Fall-2023


When Should a Class be Declared as Abstract?
31

A class should be declared as abstract in C# in the following 3


cases.

 Case1: If the class has any abstract methods, then we need


to declare the class as abstract. For a better understanding,
please have a look at the following example.

Object Oriented Programming Fall-2023


When Should a Class be Declared as Abstract?
32

Case2: If the child does not provide implementation to any of the parent abstract methods,
then again, the child class needs to be declared as an abstract class. For a better
understanding, please have a look at the following example.

Object Oriented Programming Fall-2023


When Should a Class be Declared as Abstract?
33

Case3: If the child class does not provide implementation to any of the methods of an
interface, then the child class needs to be declared as an abstract class as well as needs to
declare the method as abstract. For a better understanding, please have a look at the
following example.

Object Oriented Programming Fall-2023


Summary of Abstract Class and Abstract Methods
34

 A method that does not have a body is called an abstract method and the

class that is declared by using the keyword abstract is called an abstract


class. If a class contains an abstract method, then it must be declared as
abstract.

 An abstract class can contain both abstract and non-abstract methods. If a

child class of an abstract class wants to consume any non-abstract methods


of its parent, should implement all abstract methods of its parent.

 An abstract class is never usable to itself because we cannot create the

object of an abstract class. The members of an abstract class can be


consumed only by the child class of the abstract class.

Object Oriented Programming Fall-2023


Difference between Abstraction and Encapsulation
35

Abstraction is a process. It is the act of identifying the


relevant qualities and behaviors an object should
possess. Encapsulation is the mechanism by which the
abstraction is implemented.

Object Oriented Programming Fall-2023


Difference between Abstraction and Encapsulation
36

 A method
Abstraction that does not haveEncapsulation
a body is called an abstract
methodsolves
Abstraction andthethe classinthat
problem the is declared
Encapsulationbysolves
using the the
problem in the
keyword
design level. abstract is called animplementation
abstract class. level.If a class
contains
Abstraction an abstract
is used method,Encapsulation
for hiding the then it must be declared
is hiding as
the code and
abstract.
unwanted data and giving only relevant data into a single unit to protect the
 An abstract class can containdata
data. bothfromabstract and non-
outer world.
abstract methods. If a child class
Abstraction is set focus on the object
of an abstract
Encapsulation means hiding class
the
wants to consume any non-abstract
instead of how it does it.
methods
internal details of itsof how an
or mechanics
parent, should implement allobject abstract methods of its
does something.
parent. Encapsulation is inner layout in terms
Abstraction
 An abstract is outerclass
layout is
in terms
neverof usable
of implementation.
to itself because we
design. For Example: - Inner Implementation
cannot create the object of an abstract class. The
For Example: - Outer Look of a iPhone, detail of a iPhone, how Display Screen
members of an abstract classare
like it has a display screen.
can be consumed only by
connect with each other using
the child class of the abstractcircuits class.

Object Oriented Programming Fall-2023


Interface

37
What is an Interface?
38

The Interface in C# is a fully un-implemented class used for declaring


a set of methods of an object. So, we can define an interface as a pure
abstract class which allows us to define only abstract methods. The
abstract method means a method without a body or implementation.

Object Oriented Programming Fall-2023


What is an Interface?
39

 It is used to achieve multiple inheritances which can’t be achieved by class. It is used

to achieve full abstraction because it cannot have a 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.

Object Oriented Programming Fall-2023


Differences between Class, Abstract Class, and Interface
40

1. Class: Contains only the Non-Abstract Methods (Methods with Method Body).

2. Abstract Class: Contains both Non-Abstract Methods (Methods with Method Body) and Abstract
Methods (Methods without Method Body).

3. Interface: Contain only Abstract Methods (Methods without Method Body).

 What are abstract methods, why do we need abstract methods, and how do

implement abstract methods we have already discussed this in our Abstract


Classes and Abstract Methods topic..

 Note: Every abstract method of an interface should be implemented by the

child class of the interface without fail (Mandatory).

Object Oriented Programming Fall-2023


Points to Remember
41

Now, just like a class is having another class as a Parent, a class can also have an
Interface as a Parent. And if a class has an interface as a Parent, the class is
responsible for providing the implementation for all the abstract methods of the
interface. For a better understanding, please have a look at the below diagram.

Object Oriented Programming Fall-2023


Points to Remember
42

 Simply speaking, the Parent Interface imposes restrictions on the Child

Classes. What restrictions? The restrictions are to implement each and


every method of the interface under the child class.

 Generally, a class inherits from another class to consume members of

its Parent. On the other hand, if a class is inheriting from an interface,


it is to implement the members of its Parent (Interface), not for
consumption.

Note: A class can inherit from a class and interface(s) at a time.

Object Oriented Programming Fall-2023


How to Define an Interface?
43

The way we define a class, in the same way, we need to define an interface
in C#. In a class declaration, we need to use the class keyword whereas in
an interface declaration we need to use the interface keyword. Moreover,
in an interface, we can only declare abstract members. For a better
understanding, please have look at the below diagram.

Object Oriented Programming Fall-2023


How to Define an Interface?
44

For a better understanding please have a look at the below example. Here,
we have created one interface with the name ITestInterface by using the
interface keyword.

Object Oriented Programming Fall-2023


How to Define Abstract Methods in an Interface?
45

 In a class (i.e. Abstract Class), we generally use the abstract keyword to

define abstract methods as follows.

public abstract void Add(int num1, int num2);

 If you want to write the above abstract method in an interface, then you

don’t require public and abstract keywords in the method signature as


follows:

void Add(int num1, int num2);

Object Oriented Programming Fall-2023


Interface Rules.
46

 Point1: The first point that you need to remember is the default scope for the members

of an interface is public whereas it is private in the case of a class.

 Point2: The second point that you need to remember is by default every member of an

interface is abstract, so we don’t require to use the abstract modifier on it again just like
we do in the case of an abstract class. For a better understanding, please have a look at
the below example. By default, the Add method is going to be public and abstract.

 Point3: The third point that you need to remember is we cannot declare fields/variables,

constructors, and destructors in an interface in C#.

Object Oriented Programming Fall-2023


Interface Rules.
47

 Point4: The fourth point that you need to remember is if require an interface can inherit

from another interface in C# just like a class inherits from another class.

 For a better understanding, please have a look at the below code. Here, we have two

interfaces i.e. Interface1 and Interface2. Interface2 is inherited from Interface1 and now
interface2 has two abstract methods i.e. Add (from interface 1) and Sub.

Object Oriented Programming Fall-2023


Interface Rules.
48

 Point5: The fifth point that you need to remember is every member of an interface

should be implemented under the child class without fail (mandatory), but while
implementing we don’t require to use the override modifier just like we have done in the
case of an abstract class.

 For a better understanding, please have a look at the following code. Here, we have two

interfaces and two implementation classes. Interface2 is inherited from Inteface1 and
hence it has two abstract methods. ImplementationClass1 inherits from Interface1 and
hence implements the Add method only. ImplementationClass2 inherits from Interface1
and Interface2 inherits from Interface1 and hence this class needs to implement both the
abstract methods. That is what you can see in the below code.

Object Oriented Programming Fall-2023


Interface Rules.
49

Object Oriented Programming Fall-2023


Interface Rules.
50

In the above example, you can see that while implementing the method we are using the
public modifier and this is required. If you don’t use public, then it will treat the method as
private and you will get a compiler error ‘ImplementationClass1’ does not implement
interface member ‘ITestInterface1.Add(int, int)’.
‘ImplementationClass1.Add(int, int)’ cannot implement an interface member
because it is not public. as shown in the below image.

Object Oriented Programming Fall-2023


Interface Rules.
51

 Point6: We cannot create an instance of an interface, but we can create a reference of an

interface. The interface reference is going to hold the child class instance. Using the
interface reference, we can only invoke the methods which are declared in the interface.

 For a better understanding, please have a look at the below example. In the below

example, ITestInterface1 declared one abstract method i.e. Add. This interface is then
implemented by the ImplementationClass and this class provides the implementation for
the Add interface method. Again, in this class, we defined a new method i.e.Sub. Next,
inside the Main method, we are creating a reference of the interface which is pointing to
the child class instance. And using this reference we can only invoke the Add method and
we cannot invoke the Sub method. This is because the Add method signature is there
inside the interface but the Sub method signature is not there in the interface.

Object Oriented Programming Fall-2023


Interface Rules.
52

Object Oriented Programming Fall-2023


Explicit Interface Implementation
53

 When each interface method is implemented separately under the child class by

providing the method name along with the interface name then it is called
Explicit Interface Implementation. But in this case, while calling the method we
should compulsorily use the interface reference that is created using the object
of a class or type cast the object to the appropriate interface type.

 You can also implement an interface in another way without using the public

access modifier. In this case, we need to specify the interface name before the
method name using the dot operator as shown in the below code. This is called
Explicit Implementation of Interface Methods.

Object Oriented Programming Fall-2023


Example -- Explicit Interface Implementation
54

Object Oriented Programming Fall-2023


Explicit Interface Implementation in C#
55

Now, if the method is implemented using the public access specifier, then you
can create the object and call them directly. But if the method is implemented
using the interface name, then while calling the method we need to typecast
the object to the interface type or you can create an interface reference and
call the method. So, in our case, we call Add method directly using obj1 but
while calling the Sub method we need to typecase the obj1 to Interface2 type
as this is an instance of ImplementationClass or you can call directly using
reference variable obj2 as shown in the below image.

Object Oriented Programming Fall-2023


Example -- Explicit Interface Implementation
56

Object Oriented Programming Fall-2023


Explicit Interface Implementation Example in C#
57

Object Oriented Programming Fall-2023


Interfaces cannot contain fields
58

Please have a look at the below code. Here, we are trying to declare a
variable and as soon as we declare we get one compile-time error
i.e. Interfaces cannot contain fields as shown in the below image.

Object Oriented Programming Fall-2023


What are the Members we can and can’t define in an
interface?
59

 An interface can contain


 Abstract methods

 Properties

 Indexes

 Events

 An interface cannot contain


 Non-abstract functions

 Data fields

 Constructors

 Destructors

Object Oriented Programming Fall-2023


Can I use public access specifiers for interface methods?
60

.NET interface methods are implicitly public by default, even if they


belong to nested interfaces. Non-public modifiers are not valid for
interface methods. So, the compiler will fail and warn you in this case.
Nested interfaces may be declared protected or private but not the
interface methods. So, if you try to declare the method will public access
specifier, you will get the following error.

Object Oriented Programming Fall-2023


Can an Interface Implement an Abstract Class?
61

No. In .NET an interface cannot implement an abstract class. An interface


may only extend a super interface. However, an abstract class can
implement an interface because an abstract class can contain both
abstract methods and concrete methods. If you try to implement an
interface you will get the following compile-time error.

Object Oriented Programming Fall-2023


Can an Interface be Declared as Sealed?
62

No, it is not permitted to declare an interface as sealed; it will cause a


compilation error. This is a .NET language design decision. Interface
types are intended to be implemented and can be extended without
restriction. If you try to declare the interface as sealed, you will get the
following error.

Object Oriented Programming Fall-2023


Is it Necessary to Implement all Interface Methods?
63

It is not necessary for a class that implements an interface to implement


all its methods, but in this case, the class must be declared as abstract. For
a better understanding, please have a look at the following code.

Object Oriented Programming Fall-2023


How Interface is Different from a Class?
64

An interface is different from a class in the following ways:


 We cannot instantiate an interface.

 An interface does not contain any constructor or data fields or destructor, etc.

 All of the methods of an interface are abstract and public by default.

 An interface is not extended by a class; it is implemented by a class.

 An interface can extend multiple interfaces.

Object Oriented Programming Fall-2023


What are the Similarities Between the Interface and Abstract Class?
65

An interface is similar to an abstract class in the following ways


 Both interface and the abstract class cannot be instantiated means we cannot create the
object.

 But we can create a reference variable for both the interface and abstract class.

 The subclass should implement all abstract methods.

 Both cannot be declared sealed.

Object Oriented Programming Fall-2023


Is more than one Interface allowed to Implement a Class?
66

Yes, a class can implement multiple interfaces; this is an effective way to achieve multiple
inheritances in C#. But a class can extend only one superclass. For a better understanding,
please have a look at the following example.

Object Oriented Programming Fall-2023


Encapsulation vs Abstraction
67

 The Encapsulation Principle is all about data hiding (or information hiding). On the other hand,

the Abstraction Principle is all about detailed hiding (implementation hiding).

 Using the Encapsulation principle, we can protect our data i.e. from outside the class, nobody

can access the data directly. We are exposing the data through publicly exposed methods and
properties. The advantage is that we can validate the data before storing and returning it. On
the other hand, using the Abstraction Principle, we are exposing only the services so that the
user can consume the services but how the services/methods are implemented is hidden from
the user. The user will never know, how the method is implemented.

 With the Encapsulation Principle, we group data members and member functions into a single

unit called class, interface, enum, etc. On the other hand, with the Abstraction Principle, we are
exposing the interface or abstract class to the user and hiding implementation details i.e. hiding
the child class information.

Object Oriented Programming Fall-2023


Encapsulation vs Abstraction
68

 We can implement Encapsulation by declaring the data members as private and exposing the

data members only through public exposed methods and properties with proper validation. On
the other hand, we can implement abstraction through abstract classes and interfaces.

 In abstraction, only the abstract view is presented to the user while complex and detailed data is

hidden from the user. On the other hand, in encapsulation, data member and member
functions are bundled as a single unit and can be protected or made accessible using access
modifiers and getter and setter methods.

 Abstraction in C# is used to hide unwanted data and shows only the required properties and

methods to the user. Encapsulation in C# is used to bind data members and member functions
into a single unit to prevent outsiders from accessing it directly.

Object Oriented Programming Fall-2023


What is the Difference Between Interface and Abstract Class ?
69
Abstract Class:

 It is a partially implemented class. It allows us to define both concrete and abstract methods.

 It should be declared as abstract by using the abstract keyword, abstract methods should also contain the abstract keyword.

 Its member’s default accessibility modifier is private and can be changed to any of the other accessibility modifiers.

 It is possible to declare data fields in an abstract class.

 An abstract class can contain a non-abstract function.

 An abstract class can inherit from another abstract class or from an interface.

 An abstract class cannot be used to implement multiple inheritances.

 Abstract class members can have Access Specifiers.

Interface:

 It is a fully un-implemented class. It allows us to define only abstract methods.

 It should be created by using the keyword interface. By default, all the members are abstract only. Explicitly using abstract keyword is not allowed.

 Its member’s default accessibility modifier is public and cannot be changed.

 It is not possible to declare any data fields in an interface.

 An interface cannot contain non-abstract functions.

 An interface can inherit from only other interfaces but cannot inherits from the abstract class.

 An interface can be used to implement multiple inheritances.

 Interface members cannot have Access Specifiers.

Object Oriented Programming Fall-2023


What are the Advantages of using Interface?
70

The following are the advantages of using Interface in the C# application.


 It is used to achieve loose coupling.

 It is used to achieve total abstraction.

 To achieve multiple inheritance and abstraction.

Object Oriented Programming Fall-2023


You have the quality of education, life give
you more opportunity for new area of
learning to achieve your dreams.

THANK YOU.

71

You might also like