SE OOP Lecture8
SE OOP Lecture8
BS – (SOFTWARE ENGINEERING)
LECTURE # 08
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?
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
necessary and compulsory and we need to hide the unnecessary things from the outside
world.
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
5
Real-Time Example of Abstraction
6
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.
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.
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.
Using Interface
12
What are Abstract Classes in C#?
13
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.
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#.
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);
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.
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.
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.
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.
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.
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.
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.
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.
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.
A method that does not have a body is called an abstract method and the
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.
37
What is an Interface?
38
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.
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).
What are abstract methods, why do we need abstract methods, and how do
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.
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.
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.
If you want to write the above abstract method in an interface, then you
Point1: The first point that you need to remember is the default scope for the members
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,
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.
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.
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.
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.
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.
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.
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.
Properties
Indexes
Events
Data fields
Constructors
Destructors
An interface does not contain any constructor or data fields or destructor, etc.
But we can create a reference variable for both the interface and abstract class.
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.
The Encapsulation Principle is all about data hiding (or information hiding). On the other hand,
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.
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.
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.
An abstract class can inherit from another abstract class or from an interface.
Interface:
It should be created by using the keyword interface. By default, all the members are abstract only. Explicitly using abstract keyword is not allowed.
An interface can inherit from only other interfaces but cannot inherits from the abstract class.
THANK YOU.
71