0

Hi guys,

I am new in OOP. I know the rules and use of abstract classes but, I just want to know why we should use abctract or what is the purpose of it, rather than using normal classes while we can accomplish same thing in both ways?

If i am wrong please let me know. I'll be happy.

Thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ShawnCplus
0

Abstract classes are nice in that they aren't full implementations of objects but prototypes of them. So take this bad example.... for example: You have want to have Objects and you want all Objects to have an Identify method but each Object will do something different.

abstract class Object
{
  protected $name = 'Object';
  abstract protected function identify();
  protected function __toString()
  {
    return $this->name;
  }
}
class Square extends Object
{
  // PHP Fatal, class Square doesn't implement required method identify
}
class Square extends Object
{
  protected $name = 'Square';
  protected function identify()
  {
    return 'I am a Square';
  }
}
$square = new Square();
echo $square->identify(); // implementation of abstract method
echo $square; // Inherited implementation of Object::__toString
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.