Abstract class hide the implementation and group of similar type data.
Group of data is understandable but implementation hiding is not.
According to the following code:
abstract class area
{
protected $a;
protected $b;
abstract public function CalculateArea($ax, $ay);
}
class rectangle extends area
{
public function CalculateArea($ax, $ay)
{
$this->a = $ax;
$this->b = $ay;
return $this->a * $this->b;
}
}
class ellipse extends area
{
private $c = 3.1416;
public function CalculateArea($ax, $ay)
{
$this->a = $ax;
$this->b = $ay;
return $this->a * $this->b * $this->c;
}
}
$RectangleArea = new rectangle();
echo $RectangleArea->CalculateArea(2, 3);
echo"<br>n";
$EllipseArea = new ellipse();
echo $EllipseArea->CalculateArea(2, 3);
Here a
,b
properties in abstract class area
that is used to next inherited class rectangle
and ellipse
. Through this it is understandable that grouping of data satisfied but how the implementation hiding is satisfied? Can anyone clarify it?
Thanks in advance.
2
Answers
In your abstract class $a and $b are protected.
This is how visibility works:
public to make property/method available from anywhere,
other classes and instances of the object.
private when you want your property/method to be visible in its
own class only.
protected when you want to make your property/method visible in
all classes that extend the current class including the parent class.
If you don’t use any visibility modifier, the property/method will be public.
see this for reference https://stackoverflow.com/a/4361582/9354303
What’s presented is an interface, because the actual logic is pushed down into the implementing class, which doesn’t, in fact, create a hidden implementation.
Here’s a modified example demonstrating something closer to what you’re describing, because you access through
area()
but the implementation is private to the abstract class.At this point, we have an abstract class that utilizes a trait and determines, based on checking it’s final implementation, which area method to use.
Now we just extend it and each uses a common
->area()
invocation to call into the abstract class:https://3v4l.org/l3RTl#v8.2.0
Because the trait has the calculate methods as private, they are "hidden" (private method) from the implementing class (protected method), as well as any external code (public method).