{"id":171,"date":"2026-09-03T08:50:13","date_gmt":"2026-09-03T08:50:13","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=171"},"modified":"2026-09-03T08:50:13","modified_gmt":"2026-09-03T08:50:13","slug":"create-base-class-shape-with-method-perimeter","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/create-base-class-shape-with-method-perimeter\/","title":{"rendered":"Create base class shape with method perimeter()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Create base class shape with method perimeter(). Create subclass square and rectangle that override perimeter() method . Print perimeter.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Base class\nclass Shape:\n    def perimeter(self):\n        # Generic perimeter method, to be overridden by subclasses\n        print(\"This method should be overridden by the specific shape.\")\n        return 0\n\n# Subclass for Square\nclass Square(Shape):\n    def __init__(self, side):\n        self.side = side\n\n    # Overriding the perimeter method\n    def perimeter(self):\n        return 4 * self.side\n\n# Subclass for Rectangle\nclass Rectangle(Shape):\n    def __init__(self, length, breadth):\n        self.length = length\n        self.breadth = breadth\n\n    # Overriding the perimeter method\n    def perimeter(self):\n        return 2 * (self.length + self.breadth)\n\n# --- Creating objects and printing perimeter ---\n\n# Example 1: A square with side 5 meters (like a small garden)\nprint(\"--- Square ---\")\nsquare = Square(5)\nperim = square.perimeter()\nprint(f\"The perimeter of the square is: {perim} meters\")\n\n# Example 2: A rectangular plot (e.g., in a farm)\nprint(\"\\n--- Rectangle ---\")\nrectangle = Rectangle(10, 15)\nperim = rectangle.perimeter()\nprint(f\"The perimeter of the rectangle is: {perim} meters\")<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create base class shape with method perimeter(). Create subclass square and rectangle that override perimeter() method . Print perimeter. # Base class class Shape: def perimeter(self): # Generic perimeter method, to be overridden by subclasses print(&#8220;This method should be overridden by the specific shape.&#8221;) return 0 # Subclass for Square class Square(Shape): def __init__(self, side): [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[52,25,26,45],"class_list":["post-171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aiopd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}