Tuesday, 23 July 2019

Abstract Classes, Static Keyword, Final Keyword and Calling parent constructors

Abstract Classes
An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same visibility.

An abstract class is a class that contains at least one abstract method. The abstract method is function declaration without anybody and it has the only name of the method and its parameters. There can be any number of methods in the class and we have to declare the class as abstract only when there is an abstract method.

Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.
Eg:- <?php
abstract class Cars 
{
    public abstract function getCompanyName();
    public abstract function getPrice();
}
class Baleno  extends Cars 
{
    public function getCompanyName() 
{
        return "Maruthi Suzuki" . '<br/>';
    }
    public  function getPrice()
 {
      return 720000 . '<br/>';
    }
}
class Santro extends Cars 
{
    public function getCompanyName()
 {
        return "Hyundai" . '<br/>';
    }
    public function getPrice() 
{
        return 300000 . '<br/>';
    }
}
$car = new Baleno();
$car1 = new Santro();
echo $car->getCompanyName();
echo $car->getPrice();
echo $car1->getCompanyName();
echo $car1->getPrice();
?>
Output for the above code is:
Maruthi Suzuki
720000
Hyundai
300000

Static  Keyword
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static cannot be accessed with an instantiated class object (though a static method can). Try out following example −
Eg:-<?php
   class Foo {
      public static $my_static = 'foo';
      
      public function staticValue() {
         return self::$my_static;
      }
   }
   print Foo::$my_static . "\n";
   $foo = new Foo();
   print $foo->staticValue() . "\n";
?>

Final Keyword
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()
eg:-<?php
   class BaseClass {
      public function test() {
         echo "BaseClass::test() called<br>";
      }
      final public function moreTesting() {
         echo "BaseClass::moreTesting() called<br>";
      }
   }
   class ChildClass extends BaseClass {
      public function moreTesting() {
         echo "ChildClass::moreTesting() called<br>";
      }
   }
?>

Calling  parent  constructors
Instead of writing an entirely new constructor for the subclass, let's write it by calling the parent's constructor explicitly and then doing whatever is necessary in addition for instantiation of the subclass. Here's a simple example −
Eg:-<?php
class Name {
   var $_firstName;
   var $_lastName; 
   function Name($first_name, $last_name) {
      $this->_firstName = $first_name;
      $this->_lastName = $last_name;
   }
   function toString() {
      return($this->_lastName .", " .$this->_firstName);
   }
}
class NameSub1 extends Name {
   var $_middleInitial;
      function NameSub1($first_name, $middle_initial, $last_name) {
      Name::Name($first_name, $last_name);
      $this->_middleInitial = $middle_initial;
   }
   function toString() {
      return(Name::toString() . " " . $this->_middleInitial);
   }
}
?>

In this example, we have a parent class (Name), which has a two-argument constructor, and a subclass (NameSub1), which has a three-argument constructor. The constructor of NameSub1 functions by calling its parent constructor explicitly using the:: syntax (passing two of its arguments along) and then setting an additional field. Similarly, NameSub1 defines its non constructor toString() function in terms of the parent function that it overrides.
NOTE − A constructor can be defined with the same name as the name of a class. It is defined in above example.

0 comments

Post a Comment