1

I have major issues in understanding the major topic of OOP. Though I been through Dietel book. I have an abstract class shape.

public abstract class Shape
{
public abstract area();
public absract volume;
}
public class Circle extends Shape
{
//definition of area method
}
public class Sphere extends Shape
{
//define volume method
// define area method
}

Now as I have Shape class as Parent class and Circle class as child class, at same time Shape class is an abstract class, Now I want to define area method in Circle class but not volume method, I want to define volume method in Sphere class. But when i do this, it shows me error but if i define both method in Circle class then it works fine. But then Circle class have extra code of volume which is not use of Circle class.

AHF
  • 1,070
  • 2
  • 15
  • 47
  • 1
    Introduce `3DShape` for `volume()` - remove `Shape#volume` and declare `3DShape#volume`, where `3DShape extends Shape` – Vince Apr 28 '19 at 20:07
  • So 2 more abstract classes then? class 2dShape with abstract area method and class 3dShapte with volume abstract method? – AHF Apr 28 '19 at 20:10
  • What is defined concept of abstract? If we have 3 abstract methods in one abstract class then we must have to define them all in concrete class even if we need one or two ? – AHF Apr 28 '19 at 20:12
  • Spheres and other 3d shapes have area ([surface area](https://en.m.wikipedia.org/wiki/Surface_area)), but if your requirements do not specify area for 3d shapes, then yes, you'd separate it between different classes. In the future, if all shapes including 3d shapes require area, you could move that property up to `Shape`. For your 2nd question: yes, an abstract class requires all abstract methods to be implemented by concrete subtypes. [Read here for more on abstract classes](https://stackoverflow.com/questions/1320745/abstract-class-in-java). – Vince Apr 28 '19 at 20:15

1 Answers1

3

An abstract member is not an optional member. It is a member that MUST be implemented for implementing a concrete class. If one of the abstract member is not defined in a derived class, this class is also abstract and cannot be instantiated.

This is an excellent example for the Interface Segregation Principle: your interface for Shape combines the interfaces of different potentially unrelated concepts: area() which is only relevant for 2D shapes, and volume() is only relevant for 3D shapes.

If you want a clean SOLID design, you'd need to consider a Shape2D with area() and perimeter() and a Shape3D with a surface() and volume().

If you think that the area() is in fact the same thing than a surface(), you may consider an abstract parent Shape that provides an abstract surface(), a child Shape2D that provides for the perimeter() and another child Shape3D that provides the volume().

More pragmatically, you could consider that the volume() of of a Circle or any other 2D shape is simply 0 and stick to your original design.

Christophe
  • 68,716
  • 7
  • 72
  • 138