What is OOPs and explain
OOP stands for Object-Oriented Programming. It’s a programming paradigm based on the concept of “objects,” which can contain data and code to manipulate that data.
<p style="-webkit-text-stroke-color: rgb(0, 0, 0); -webkit-text-stroke-width: 0px; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-size-adjust: none; font-size: 14px; font-stretch: normal; font-style: normal; font-variant-alternates: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-ligatures: normal; font-variant-numeric: normal; font-variant-position: normal; font-variation-settings: normal; line-height: normal; margin: 0px;"><span style="font-kerning: none;"><span style="font-family: times;">For example, let's say we're building a program to model different shapes. We could create a class called Shape that represents common properties and behaviors of shapes, such as area() and perimeter(). Then, we can create subclasses like Rectangle, Circle, and Triangle that inherit from Shape and provide specific implementations for those shapes' properties and behaviors.</span></span></p><h4 style="text-align: left;"><span style="font-kerning: none;"><span style="font-family: times;">A simplified example in Python:</span></span></h4><p style="-webkit-text-stroke-color: rgb(0, 0, 0); -webkit-text-stroke-width: 0px; font-family: "Times New Roman"; font-feature-settings: normal; font-kerning: auto; font-optical-sizing: auto; font-size-adjust: none; font-size: 14.7px; font-stretch: normal; font-style: normal; font-variant-alternates: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-emoji: normal; font-variant-ligatures: normal; font-variant-numeric: normal; font-variant-position: normal; font-variation-settings: normal; line-height: normal; margin: 0px;"><span style="font-kerning: none; font-size: 14.67px;"> </span></p><pre style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); -webkit-text-size-adjust: 100%; border-radius: 0.375rem; border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(13, 13, 13); color: #0d0d0d; font-family: "Söhne Mono", Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-feature-settings: normal; font-size: 0.875em; font-variation-settings: normal; line-height: 1.714286; margin-bottom: 0px; margin-top: 0px; overflow-x: auto; padding: 0px;"><div style="--border-heavy: hsla(0,0%,100%,0.2); --border-light: hsla(0,0%,100%,0.1); --border-medium: hsla(0,0%,100%,0.15); --border-xheavy: hsla(0,0%,100%,0.25); --link-hover: #5e83b3; --link: #7ab7ff; --main-surface-primary: var(--gray-800); --main-surface-secondary: var(--gray-750); --main-surface-tertiary: var(--gray-700); --sidebar-surface-primary: var(--gray-900); --sidebar-surface-secondary: var(--gray-800); --sidebar-surface-tertiary: var(--gray-750); --text-error: var(--red-500); --text-primary: var(--gray-100); --text-quaternary: var(--gray-500); --text-secondary: var(--gray-300); --text-tertiary: var(--gray-400); --tw-bg-opacity: 1; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; background-color: rgba(13,13,13,var(--tw-bg-opacity)); border-color: var(--border-medium); border-image: none; border-radius: 0.375rem; border-style: solid; border-width: 0.5px; box-sizing: border-box;"><div style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; align-items: center; border-top-left-radius: 0.375rem; border-top-right-radius: 0.375rem; border: 0px solid rgb(227, 227, 227); box-sizing: border-box; display: flex; font-family: Söhne, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif, "Helvetica Neue", Arial, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 0.75rem; justify-content: space-between; line-height: 1rem; padding: 0.5rem 1rem; position: relative;"><span style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(227, 227, 227); box-sizing: border-box;">python</span><div style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; align-items: center; border: 0px solid rgb(227, 227, 227); box-sizing: border-box; display: flex;"><span data-state="closed" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(227, 227, 227); box-sizing: border-box;"><button style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; align-items: center; appearance: button; background-color: #0d0d0d; background-image: none; border-color: rgb(227, 227, 227); border-width: 0px; column-gap: 0.25rem; cursor: pointer; display: flex; font-family: inherit; font-feature-settings: inherit; font-size: 12px; font-variation-settings: inherit; gap: 0.25rem; letter-spacing: inherit; line-height: inherit; margin: 0px; padding: 0px; row-gap: 0.25rem;"><svg fill="none" height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M7 5a3 3 0 0 1 3-3h9a3 3 0 0 1 3 3v9a3 3 0 0 1-3 3h-2v2a3 3 0 0 1-3 3H5a3 3 0 0 1-3-3v-9a3 3 0 0 1 3-3h2zm2 2h5a3 3 0 0 1 3 3v5h2a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1h-9a1 1 0 0 0-1 1zM5 9a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1v-9a1 1 0 0 0-1-1z" fill-rule="evenodd" fill="currentColor"></path></svg>Copy code</button></span></div></div><div dir="ltr" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(227, 227, 227); box-sizing: border-box; overflow-y: auto; padding: 1rem;"><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3; font-size: 0.875em;">class</span><span style="caret-color: rgb(255, 255, 255); color: white; font-size: 0.875em;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d; font-size: 0.875em;">Shape</span><span style="caret-color: rgb(255, 255, 255); color: white; font-size: 0.875em;">:</span></div><div dir="ltr" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,0.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; border: 0px solid rgb(227, 227, 227); box-sizing: border-box; overflow-y: auto; padding: 1rem;"><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">area</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">pass</span><span style="caret-color: rgb(255, 255, 255); color: white;">
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">perimeter</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">pass</span><span style="caret-color: rgb(255, 255, 255); color: white;">
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">area</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">return</span><span style="caret-color: rgb(255, 255, 255); color: white;"> self.width * self.height
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">perimeter</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">return</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #df3079;">2</span><span style="caret-color: rgb(255, 255, 255); color: white;"> * (self.width + self.height)
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">area</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">return</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #df3079;">3.14</span><span style="caret-color: rgb(255, 255, 255); color: white;"> * self.radius ** </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #df3079;">2</span><span style="caret-color: rgb(255, 255, 255); color: white;">
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">def</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #f22c3d;">perimeter</span><span style="caret-color: rgb(255, 255, 255); color: white;">(</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; caret-color: rgb(255, 255, 255); color: white;">self</span><span style="caret-color: rgb(255, 255, 255); color: white;">):
</span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #2e95d3;">return</span><span style="caret-color: rgb(255, 255, 255); color: white;"> </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #df3079;">2</span><span style="caret-color: rgb(255, 255, 255); color: white;"> * </span><span style="border: 0px solid rgb(227, 227, 227); box-sizing: border-box; color: #df3079;">3.14</span><span style="caret-color: rgb(255, 255, 255); color: white;"> * self.radius
# Example usage:
rect = Rectangle(5, 4)
print(“Rectangle Area:”, rect.area())
print(“Rectangle Perimeter:”, rect.perimeter())
circle = Circle(3)
print(“Circle Area:”, circle.area())
print(“Circle Perimeter:”, circle.perimeter())

Leave a Reply