Skip to content

Commit 84adc2e

Browse files
committed
feat: Add ShapeVIMP abstract class with CircleVIMP and Rectangle implementations
WHAT the code does: Defines abstract class ShapeVIMP with two abstract methods: - peimeterIMP(): calculates perimeter. - areaIMP(): calculates area. Defines CircleVIMP extending ShapeVIMP: - Implements peimeterIMP() as 2πr. - Implements areaIMP() as πr². Defines Rectangle extending ShapeVIMP: - Implements peimeterIMP() as 2 × (length + breadth). - Implements areaIMP() as length × breadth. Defines AbstractIMP with main(): - Creates a Rectangle, sets length=10 and breadth=5. - Assigns Rectangle to a ShapeVIMP reference. - Calls areaIMP() via the abstract reference and prints the result (50.0). WHY this matters: Demonstrates **abstraction**: ShapeVIMP defines a contract for all shapes (area and perimeter). Shows **polymorphism**: a ShapeVIMP reference can point to different shape objects (Rectangle, CircleVIMP). Encourages code reuse and flexibility by separating "what a shape does" from "how each shape does it". HOW it works: ShapeVIMP s = r (where r is a Rectangle). - Calls areaIMP() → resolves to Rectangle.areaIMP() → computes 10 × 5 = 50. Same approach could be used for CircleVIMP by setting radiusIMP and calling its methods. Tips and gotchas: Method names should follow Java naming conventions (perimeter() instead of peimeterIMP()). Fields (radiusIMP, length, breadth) should be private with constructors or setters for encapsulation. Currently only Rectangle is demonstrated; adding a CircleVIMP demo in main would show polymorphism more clearly. Abstract class ShapeVIMP could be refactored into an interface if only abstract methods are required. Use-cases: Educational example of abstract classes and polymorphism in OOP. Foundation for building extensible geometry libraries. Practical demonstration of how base contracts ensure consistency across different subclasses. Short key: class-shapevimp circle rectangle abstract-polymorphism. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 6741c43 commit 84adc2e

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Section13AbstractClasses/src/AbstractIMP.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
import java.awt.*;
2-
abstract class ShapeVIMP
3-
{
2+
3+
abstract class ShapeVIMP {
44
abstract public double peimeterIMP();
55
abstract public double areaIMP();
66
}
77

8-
class CircleVIMP extends ShapeVIMP
9-
{
8+
class CircleVIMP extends ShapeVIMP {
109
double radiusIMP;
1110

12-
public double peimeterIMP()
13-
{
11+
public double peimeterIMP() {
1412
return 2 * Math.PI * radiusIMP;
1513
}
1614

17-
public double areaIMP()
18-
{
15+
public double areaIMP() {
1916
return Math.PI*radiusIMP*radiusIMP;
2017
}
2118
}
2219

23-
class Rectangle extends ShapeVIMP
24-
{
20+
class Rectangle extends ShapeVIMP {
2521
double length;
2622
double breadth;
2723

28-
public double peimeterIMP()
29-
{
24+
public double peimeterIMP() {
3025
return 2*(length+breadth);
3126
}
32-
public double areaIMP()
33-
{
27+
28+
public double areaIMP() {
3429
return length*breadth;
3530
}
3631
}
3732

38-
class AbstractIMP
39-
{
40-
public static void main(String[] args)
41-
{
33+
class AbstractIMP {
34+
public static void main(String[] args) {
4235
Rectangle r=new Rectangle();
4336
r.length=10;
4437
r.breadth=5;

0 commit comments

Comments
 (0)