SlideShare a Scribd company logo
ABSTRACT
CLASSES AND
INTERFACES
GROUP # 8
1. Adnan Haider
2. M Ismail
3. Hussain ahmed
4. Rimsha Ali
5. Sofia Bashir
2
OVERVIEW
♦️Introduction
♦️Abstract Classes
♦️Interfaces
♦️Comparison between Abstract Classes and
Interfaces
♦️Real World Examples and use cases
3
1. INTRODUCTION:
Abstract Methods:
• The Functions that are declared in a class
but not implemented in that class are
called abstract methods .
• They Serves as placeholder for methods
that must be implemented by subclasses.
• These methods are typically declared in
abstract classes or interfaces.(Abstract
methods are achieved by declaring
virtual functions without providing any
implementation) .
• They are useful for defining a common
interface that implemented in their own
way.
CONCRETE CLASS:
 A concrete class is a class that provides
implementations for all its member functions,
including inherited abstract methods.
 It can be instantiated to create objects. Concrete
classes can also have their own non-abstract
methods and member variables.
 They serve as building blocks for creating
objects with specific behavior and attributes
PURE VIRTUAL
FUNCTION:
 “Pure virtual function is a virtual function with no body
.”
 A pure virtual function is a special type of virtual
function in C++ that is declared in a base class but has
no implementation. It is marked with = 0 at the end of its
declaration.
 Classes containing pure virtual functions are abstract
classes, meaning they cannot be instantiated directly.
Instead, they serve as interfaces or blueprints for
derived classes to provide implementations for the pure
virtual functions.
 Subclasses must override all pure virtual functions to
become concrete classes and be instantiated. Pure
virtual functions are a key feature of achieving
polymorphism and defining abstract interfaces in C++. 6
ABSTRACT
CLASSES:
• “The classes that contain pure virtual
functions are called abstract classes”
• These classes can not be instantiated
directly ; they are meant to be subclassed.
• Abstracts methods are declared but not
implemented in the abstract class itself.
• It serves as blueprints for other classes and
can contain one or more abstract methods.
7
​SYNTAX
EXAMPLE
: #include <iostream>
// Abstract class
class Shape
{ public:
// Pure virtual function (abstract method)
virtual void draw() = 0;
};
int main() {
// Attempting to create an object of the abstract
class will result in a compilation error // Shape
shape;
return 0;}
.
8
EXAMPLE:
#include <iostream>// Abstract class
using namespace std;
class Shape {
public: // Abstract method
virtual void draw() = 0; };
// Concrete subclass implementing the abstract method
class Circle :
public Shape {public:
void draw() override {
cout << "Drawing Circlen"; }}
;// Concrete subclass implementing the abstract method
class Rectangle :
public Shape {public:
void draw() override {
cout << "Drawing Rectanglen"; }}; 9
EXAMPLE:
int main()
{ // Create objects of concrete subclasses
Circle circle;
Rectangle rectangle; // Call methods
circle.draw();
rectangle.draw();
return 0;
}
10
output:
Drawing Circle
Drawing Rectangle
INTERFACES:
• “An interface is a blueprint that defines a set of
methods and properties that a class must implement
,without providing the actual implementation”.
• interfaces are typically defined using abstract classes
with pure virtual functions.
• An abstract class is a class that cannot be instantiated
on its own and contains one or more pure virtual
functions, which are declared using the virtual keyword
followed by = 0; syntax.
11
EXAMPLE
:
#include <iostream>
Using namespace std;
class Interface {
public:
virtual void method1() = 0;
virtual void method2() = 0;};
class MyClass :
public Interface {public:
void method1() {
cout << "Inside method1n"; }
void method2() {
cout << "Inside method2n"; }};
int main() {
MyClass obj;
obj.method1();
obj.method2(); return 0;
}
1. .
12
OUTPUT:
Inside method1
Inside method2
RULE OF INTERFACES:
13
Interfaces in OOP define what classes can do
without providing any implementation details,
while abstract classes can provide both
method declarations and some
implementations but cannot be instantiated
directly.
ABSTRACT CLASS
a)Abstract class contain both
virtual methods and concrete
methods(with
implementations)
b)These cannot be instantiated
directly.
c) Abstract Classes Support
Single inheritance.
d)Abstract classes are used
when a base class needs to
provide some default
behavior along with abstract
methods that subclasses
must implement.
a) Interfaces contain only pure
virtual methods (method without
implementations).
b) These also cannot be
instantiated at all.
c) Interfaces support multiple
inheritance
d) Interfaces are used when
defining a common behavior of
multiple functions or methods.
INTERFACES
REAL WORLD EXAMPLE:
• .
Virtual void getName()=0;
Void getName()
{
}
Void getName()
{
}
Class Manager Class Accountant Class Costumer
Virtual void
getName()=0;
Class Database
ADVANTAGES IN OOP:
1. Defining a Common Interface: Abstract classes set a
blueprint for subclasses, ensuring they share common
methods.
2. Organizing Code: They help group related classes
under a common umbrella, improving code structure.
3. Extending Functionality: New features can be easily
added by creating new subclasses of abstract classes.
4. Enhancing Maintainability: Abstract classes make
code easier to understand and maintain by
encapsulating related behavior. 16

More Related Content

Similar to Abstract Classes and Interfaces in oop.pptx

Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
Ganesh Samarthyam
 
What are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdfWhat are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdf
arihantkitchenmart
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
Linh Lê
 

Similar to Abstract Classes and Interfaces in oop.pptx (20)

ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Java basics
Java basicsJava basics
Java basics
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
 
Abstract Class and Interface.pdf
Abstract Class and Interface.pdfAbstract Class and Interface.pdf
Abstract Class and Interface.pdf
 
abstract class and interface.Net
abstract class and interface.Netabstract class and interface.Net
abstract class and interface.Net
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
What are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdfWhat are abstract classes Dont interfaces also allow for polymo.pdf
What are abstract classes Dont interfaces also allow for polymo.pdf
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
javainterface
javainterfacejavainterface
javainterface
 

Recently uploaded

527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf
rajpreetkaur75080
 

Recently uploaded (14)

Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
 
05232024 Joint Meeting - Community Networking
05232024 Joint Meeting - Community Networking05232024 Joint Meeting - Community Networking
05232024 Joint Meeting - Community Networking
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
 
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
 
Hi-Tech Industry 2024-25 Prospective.pptx
Hi-Tech Industry 2024-25 Prospective.pptxHi-Tech Industry 2024-25 Prospective.pptx
Hi-Tech Industry 2024-25 Prospective.pptx
 
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
 
527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf
 
Eureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 PresentationEureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 Presentation
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
 
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
 
123445566544333222333444dxcvbcvcvharsh.pptx
123445566544333222333444dxcvbcvcvharsh.pptx123445566544333222333444dxcvbcvcvharsh.pptx
123445566544333222333444dxcvbcvcvharsh.pptx
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
 
The Canoga Gardens Development Project. PDF
The Canoga Gardens Development Project. PDFThe Canoga Gardens Development Project. PDF
The Canoga Gardens Development Project. PDF
 

Abstract Classes and Interfaces in oop.pptx

  • 2. GROUP # 8 1. Adnan Haider 2. M Ismail 3. Hussain ahmed 4. Rimsha Ali 5. Sofia Bashir 2
  • 3. OVERVIEW ♦️Introduction ♦️Abstract Classes ♦️Interfaces ♦️Comparison between Abstract Classes and Interfaces ♦️Real World Examples and use cases 3
  • 4. 1. INTRODUCTION: Abstract Methods: • The Functions that are declared in a class but not implemented in that class are called abstract methods . • They Serves as placeholder for methods that must be implemented by subclasses. • These methods are typically declared in abstract classes or interfaces.(Abstract methods are achieved by declaring virtual functions without providing any implementation) . • They are useful for defining a common interface that implemented in their own way.
  • 5. CONCRETE CLASS:  A concrete class is a class that provides implementations for all its member functions, including inherited abstract methods.  It can be instantiated to create objects. Concrete classes can also have their own non-abstract methods and member variables.  They serve as building blocks for creating objects with specific behavior and attributes
  • 6. PURE VIRTUAL FUNCTION:  “Pure virtual function is a virtual function with no body .”  A pure virtual function is a special type of virtual function in C++ that is declared in a base class but has no implementation. It is marked with = 0 at the end of its declaration.  Classes containing pure virtual functions are abstract classes, meaning they cannot be instantiated directly. Instead, they serve as interfaces or blueprints for derived classes to provide implementations for the pure virtual functions.  Subclasses must override all pure virtual functions to become concrete classes and be instantiated. Pure virtual functions are a key feature of achieving polymorphism and defining abstract interfaces in C++. 6
  • 7. ABSTRACT CLASSES: • “The classes that contain pure virtual functions are called abstract classes” • These classes can not be instantiated directly ; they are meant to be subclassed. • Abstracts methods are declared but not implemented in the abstract class itself. • It serves as blueprints for other classes and can contain one or more abstract methods. 7
  • 8. ​SYNTAX EXAMPLE : #include <iostream> // Abstract class class Shape { public: // Pure virtual function (abstract method) virtual void draw() = 0; }; int main() { // Attempting to create an object of the abstract class will result in a compilation error // Shape shape; return 0;} . 8
  • 9. EXAMPLE: #include <iostream>// Abstract class using namespace std; class Shape { public: // Abstract method virtual void draw() = 0; }; // Concrete subclass implementing the abstract method class Circle : public Shape {public: void draw() override { cout << "Drawing Circlen"; }} ;// Concrete subclass implementing the abstract method class Rectangle : public Shape {public: void draw() override { cout << "Drawing Rectanglen"; }}; 9
  • 10. EXAMPLE: int main() { // Create objects of concrete subclasses Circle circle; Rectangle rectangle; // Call methods circle.draw(); rectangle.draw(); return 0; } 10 output: Drawing Circle Drawing Rectangle
  • 11. INTERFACES: • “An interface is a blueprint that defines a set of methods and properties that a class must implement ,without providing the actual implementation”. • interfaces are typically defined using abstract classes with pure virtual functions. • An abstract class is a class that cannot be instantiated on its own and contains one or more pure virtual functions, which are declared using the virtual keyword followed by = 0; syntax. 11
  • 12. EXAMPLE : #include <iostream> Using namespace std; class Interface { public: virtual void method1() = 0; virtual void method2() = 0;}; class MyClass : public Interface {public: void method1() { cout << "Inside method1n"; } void method2() { cout << "Inside method2n"; }}; int main() { MyClass obj; obj.method1(); obj.method2(); return 0; } 1. . 12 OUTPUT: Inside method1 Inside method2
  • 13. RULE OF INTERFACES: 13 Interfaces in OOP define what classes can do without providing any implementation details, while abstract classes can provide both method declarations and some implementations but cannot be instantiated directly.
  • 14. ABSTRACT CLASS a)Abstract class contain both virtual methods and concrete methods(with implementations) b)These cannot be instantiated directly. c) Abstract Classes Support Single inheritance. d)Abstract classes are used when a base class needs to provide some default behavior along with abstract methods that subclasses must implement. a) Interfaces contain only pure virtual methods (method without implementations). b) These also cannot be instantiated at all. c) Interfaces support multiple inheritance d) Interfaces are used when defining a common behavior of multiple functions or methods. INTERFACES
  • 15. REAL WORLD EXAMPLE: • . Virtual void getName()=0; Void getName() { } Void getName() { } Class Manager Class Accountant Class Costumer Virtual void getName()=0; Class Database
  • 16. ADVANTAGES IN OOP: 1. Defining a Common Interface: Abstract classes set a blueprint for subclasses, ensuring they share common methods. 2. Organizing Code: They help group related classes under a common umbrella, improving code structure. 3. Extending Functionality: New features can be easily added by creating new subclasses of abstract classes. 4. Enhancing Maintainability: Abstract classes make code easier to understand and maintain by encapsulating related behavior. 16