

{"id":125782,"date":"2023-11-14T16:09:20","date_gmt":"2023-11-14T10:39:20","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=125782"},"modified":"2024-02-29T10:12:58","modified_gmt":"2024-02-29T04:42:58","slug":"python-program-on-interface","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/","title":{"rendered":"Python Program on Interface"},"content":{"rendered":"<p>Interfaces in Python serve as a crucial element in achieving abstraction and defining contracts for classes. Unlike abstract classes, interfaces solely consist of method signatures without any implementation details. They provide a blueprint for classes to adhere to, ensuring that specific methods are implemented.<\/p>\n<p>In this article, we delve into the practical application of interfaces in Python, elucidating how they facilitate code organization, promote adherence to design principles, and enable the creation of interchangeable components within a software system.<\/p>\n<h2>Topic Explanation:<\/h2>\n<p>Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design. This minimalist approach allows developers to concentrate on defining essential behaviors that must be adhered to, promoting a modular and loosely coupled code structure. Interfaces serve as a clear delineation of responsibilities, guiding the implementation of methods across various classes and ensuring a consistent and predictable codebase.<\/p>\n<p>Practically, interfaces in Python contribute to code organization and readability by providing a standardized framework for implementing shared functionalities. Through the adherence to interface contracts, classes gain the capability to participate in polymorphism, where objects of different classes can be treated interchangeably based on shared interface implementations. This versatility enhances the maintainability of code, allowing for seamless updates and expansions.<\/p>\n<p>The article explores how interfaces serve as a powerful tool in Python programming, enabling developers to create modular, extensible, and easily maintainable software systems through the adoption of well-defined interfaces and the adherence to established contracts.<\/p>\n<h3>Prerequisite:<\/h3>\n<p><strong>Object-Oriented Programming (OOP) Understanding:<\/strong><\/p>\n<ul>\n<li>Familiarity with fundamental OOP concepts, including classes, objects, and inheritance.<\/li>\n<\/ul>\n<p><strong>Python Class and Method Concepts:<\/strong><\/p>\n<ul>\n<li>Knowledge of defining classes and methods in Python.<\/li>\n<\/ul>\n<p><strong>Understanding of Abstraction:<\/strong><\/p>\n<ul>\n<li>Awareness of abstraction principles and its role in software design.<\/li>\n<\/ul>\n<p><strong>Basic Python Syntax:<\/strong><\/p>\n<ul>\n<li>Proficiency in basic Python syntax, including function definitions, variable assignments, and class instantiation.<\/li>\n<\/ul>\n<p><strong>Knowledge of Inheritance:<\/strong><\/p>\n<ul>\n<li>Understanding of how inheritance works in Python and its role in code organization.<\/li>\n<\/ul>\n<p><strong>Implementation of Methods:<\/strong><\/p>\n<ul>\n<li>Familiarity with the implementation of methods within classes and their impact on the overall functionality of a program.<\/li>\n<\/ul>\n<h3>Code With Comments:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Importing Abstract Base Class (ABC) module\r\nfrom abc import *\r\n\r\n# Abstract Class Definition for MyDatabase\r\nclass MyDatabase(ABC):\r\n    \r\n    # Abstract Method Declaration for Connection\r\n    @abstractmethod\r\n    def connection(self):\r\n        pass\r\n    \r\n    # Abstract Method Declaration for Disconnection\r\n    @abstractmethod\r\n    def disconnection(self):\r\n        pass\r\n\r\n# Concrete Class MySql implementing MyDatabase\r\nclass MySql(MyDatabase):\r\n    \r\n    # Implementation of Connection Method\r\n    def connection(self):\r\n        print(\"This is MYSQL Connection......\")\r\n    \r\n    # Implementation of Disconnection Method\r\n    def disconnection(self):\r\n        print(\"MYSQL Connection is Disconnect......\")\r\n\r\n# Concrete Class Oracle implementing MyDatabase\r\nclass Oracle(MyDatabase):\r\n    \r\n    # Implementation of Connection Method\r\n    def connection(self):\r\n        print(\"This is Oracle Connection Class.....\")\r\n    \r\n    # Implementation of Disconnection Method\r\n    def disconnection(self):\r\n        print(\"ORACLE Connection is Disconnect......\")\r\n\r\n# User Input for Database Class Selection\r\ndname = input(\"Enter class Name to Connect\")\r\n\r\n# Dynamically Obtaining Class Reference\r\nclassname = globals()[dname]\r\n\r\n# Creating an Instance of the Selected Database Class\r\nd1 = classname()\r\n\r\n# Calling the Connection Method on the Database Instance\r\nd1.connection()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Enter class Name to Connect MySql<br \/>\nThis is MYSQL Connection&#8230;&#8230;<\/p>\n<h3>Code Explanation:<\/h3>\n<p><strong>Importing Libraries:<\/strong><\/p>\n<ul>\n<li>from abc import * imports the Abstract Base Class (ABC) module.<\/li>\n<\/ul>\n<p><strong>Abstract Class Definition:<\/strong><\/p>\n<ul>\n<li><strong>class MyDatabase(ABC):<\/strong> defines an abstract class named MyDatabase with abstract methods for connection and disconnection.<\/li>\n<\/ul>\n<p><strong>Concrete Class MySql:<\/strong><\/p>\n<ul>\n<li><strong>class MySql(MyDatabase):<\/strong> defines a concrete class MySql that inherits from MyDatabase and implements the abstract methods.<\/li>\n<\/ul>\n<p><strong>Concrete Class Oracle:<\/strong><\/p>\n<ul>\n<li>class Oracle(MyDatabase): defines another concrete class Oracle that also inherits from MyDatabase and implements the abstract methods.<\/li>\n<\/ul>\n<p><strong>User Input for Database Class:<\/strong><\/p>\n<ul>\n<li>dname = input(&#8220;Enter class Name to Connect&#8221;) prompts the user to enter the desired database class name.<br \/>\nDynamic Class Reference:<\/li>\n<li>classname = globals()[dname] dynamically obtains the class reference based on the user input.<\/li>\n<\/ul>\n<p><strong>Instance Creation:<\/strong><\/p>\n<ul>\n<li>d1 = classname() creates an instance d1 of the dynamically selected database class.<\/li>\n<\/ul>\n<p><strong>Connection Method Call:<\/strong><\/p>\n<ul>\n<li>d1.connection() calls the connection method on the created database instance, demonstrating polymorphism based on the selected database class.<\/li>\n<\/ul>\n<h3>Conclusion:<\/h3>\n<p>In conclusion, this Python code successfully applies the concept of interfaces in a practical scenario, illustrating a straightforward database connection process. The abstract class, MyDatabase, serves as an interface, outlining the mandatory connection and disconnection methods. Concrete classes, MySql and Oracle, implement this interface, delivering distinct functionalities tailored for database connections. The user&#8217;s dynamic input allows the selection of the desired database class, highlighting the adaptability and interchangeability facilitated by interface-driven design. This approach fosters the development of modular and scalable systems, where classes adhere to a shared interface while providing unique implementations. The provided code serves as a testament to the effectiveness of interfaces in enhancing code organization and encapsulation, ultimately contributing to a more maintainable and extensible software architecture.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interfaces in Python serve as a crucial element in achieving abstraction and defining contracts for classes. Unlike abstract classes, interfaces solely consist of method signatures without any implementation details. They provide a blueprint for&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28885,10333,28886,28626,28887],"class_list":["post-125782","post","type-post","status-publish","format-standard","hentry","category-python","tag-interface-in-python","tag-python","tag-python-interface","tag-python-practical","tag-python-program-on-interface"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Interface - DataFlair<\/title>\n<meta name=\"description\" content=\"Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Interface - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-14T10:39:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-29T04:42:58+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Interface - DataFlair","description":"Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Interface - DataFlair","og_description":"Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-14T10:39:20+00:00","article_modified_time":"2024-02-29T04:42:58+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Interface","datePublished":"2023-11-14T10:39:20+00:00","dateModified":"2024-02-29T04:42:58+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/"},"wordCount":628,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["interface in python","Python","python interface","python practical","python program on interface"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-interface\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/","name":"Python Program on Interface - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-14T10:39:20+00:00","dateModified":"2024-02-29T04:42:58+00:00","description":"Interfaces in Python play a pivotal role in achieving a higher level of abstraction and enforcing well-defined contracts within software design.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-interface\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-interface\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Interface"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=125782"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125782\/revisions"}],"predecessor-version":[{"id":134249,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125782\/revisions\/134249"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=125782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=125782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=125782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}