2. 2
Abstract Class
• An abstract class is a class that is designed to be specifically used
as a base class.
• An abstract class contains at least one pure virtual function. You
declare a pure virtual function by using a pure specifier ( = 0 ) in the
declaration of a virtual member function in the class declaration.
• An abstract function has no implementation and must be in an
abstract class. It can only be declared.
3. Rules for Abstract Class
● We cannot create objects of an abstract class.
● We can derive classes from them, and use their data members and
member functions (except pure virtual functions).
● 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
- Interface
● Abstract class and interface both can have abstract methods which are
necessary for abstraction.
3
4. ● Abstract class: is a restricted class that cannot be used to create
objects (to access it, it must be inherited from another class).
● Abstract method: It can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass (inherited from).
● An abstract member is implicitly virtual. The abstract can be called as pure
virtual in some of the languages.
● Abstraction: Abstraction is not a real implementation, it means showing
only necessary things that is no actual implementation how is about.
Abstract class is one opposite concept it is used for different purpose.
4
5. 5
Interface
● An interface describes the behavior or capabilities of a C++ class
without committing to a particular implementation of that class.
● C++ has no built-in concepts of interfaces. You can implement it using
abstract classes which contains only pure virtual functions.
● Since it allows multiple inheritance, you can inherit this class to create
another class which will then contain this interface in it.
● Any class derived from the pure abstract class (Interface) must
implement all of the methods of the base class i.e. Interface. Interface
pointers can be passed to functions and classes thereby we can call the
functions of the derived class from there itself.
6. Abstract Class vs. Interface
● Abstract Class and Interface are used in Java. The Abstract class and
Interface both are used to have abstraction. An abstract class contains an
abstract keyword on the declaration whereas an Interface is a sketch that is
used to implement a class.
● An abstract class allows you to create functionality that subclasses can
implement or override. An interface only allows you to define functionality,
not implement it. And whereas a class can extend only one abstract class, it
can take advantage of multiple interfaces.
6
7. Abstract Class vs. Interface
● The C++ interfaces are implemented using abstract classes and these
abstract classes should not be confused with data abstraction which is a
concept of keeping implementation details separate from associated data.
● A class is made abstract by declaring at least one of its functions as pure
virtual function. A pure virtual function is specified by placing "= 0" in its
declaration
7