ABSTRACT CLASSES


computer apg

Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract.

An abstract class is defined with the abstract keyword.


 ABSTRACT METHODS 


Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

An abstract method is a method that is declared, but not implemented in the code.

An abstract method is defined with the abstract keyword.



 ABSTRACT CLASSES & METHODS 


When inheriting from an abstract class, the child class method must be defined with the same name, and the same or a less restricted access modifier.

So, if the abstract method is defined as protected, the child class method must be defined as either protected or public, but not private.

Also, the type and number of required arguments must be the same. However, the child classes may have optional arguments in addition.

So, when a child class is inherited from an abstract class, we have the following rules:

1. The child class method must be defined with the same name and it redeclares the parent abstract method.

2. The child class method must be defined with the same or a less restricted access modifier.

3. The number of required arguments must be the same. However, the child class may has optional arguments in addition.


<?php

abstract class ParentClass {
    
  abstract public function 
someMethod1();
  abstract public function 
someMethod2($name$behavior);
  abstract public function 
someMethod3() : string;
  
}

?>

  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   Parent class
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

abstract class Dwarfs {
  public 
$name;
  public function 
__construct($name) {
    
$this->name $name;
  }
  abstract public function 
intro() : string;
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   Child classes
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   
class Doc extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Leader of the group, not too good at speaking.";
  }
}

class 
Bashful extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>The shy one.";
  }
}

class 
Grumpy extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Always annoyed and irritated, doesn't 
    like Snow White."
;
  }
}

class 
Happy extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Fat, jolly, friendly, spreading joy to everyone.";
  }
}

class 
Sleepy extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Always tired, most observant.";
  }
}

class 
Dopey extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Annoying, silly, the comic relief.";
  }
}

class 
Sneezy extends Dwarfs {
  public function 
intro() : string {
    return 
"$this->name<br>Likes to have fun, has never 
    ending case of hay fever that makes him sneeze."
;
  }
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   Create objects from the child classes
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   
$Doc = new Doc("Doc");
echo 
$Doc->intro();
echo 
"<br><br>";

$Bashful = new Bashful("Bashful");
echo 
$Bashful->intro();
echo 
"<br><br>";

$Grumpy = new Grumpy("Grumpy");
echo 
$Grumpy->intro();
echo 
"<br><br>";

$Happy = new Happy("Happy");
echo 
$Happy->intro();
echo 
"<br><br>";

$Sleepy = new Sleepy("Sleepy");
echo 
$Sleepy->intro();
echo 
"<br><br>";

$Dopey = new Dopey("Dopey");
echo 
$Dopey->intro();
echo 
"<br><br>";

$Sneezy = new Sneezy("Sneezy");
echo 
$Sneezy->intro();

?>

 RESULT   

Doc
Leader of the group, not too good at speaking.

Bashful
The shy one.

Grumpy
Always annoyed and irritated, doesn't like Snow White.

Happy
Fat, jolly, friendly, spreading joy to everyone.

Sleepy
Always tired, most observant.

Dopey
Annoying, silly, the comic relief.

Sneezy
Likes to have fun, has never ending case of hay fever that makes him sneeze.


  2 EXERCISE   

<?php

abstract class Dwarfs {

abstract protected function 
addName($name);
}

class 
ChildDwarfsClass extends Dwarfs {
    
  public function 
addName($name) {
    if (
$name == "Peter Diklage") {
      
$add "Mr.";
    } elseif (
$name == "Erica Schmidt") {
      
$add " married with Mrs.";
    } else {
      
$add "";
    }
    return 
"{$add} {$name}";
  }
}

$class = new ChildDwarfsClass;
echo 
$class->addName("Peter Diklage");
echo 
$class->addName("Erica Schmidt");
echo 
'.';

?>

 RESULT   

Mr. Peter Diklage married with Mrs. Erica Schmidt.