TL;DR
If you see a an error with Interceptor.php
be sure to empty /var/generation/
before you look further. It will save you lots of time.
Explanation
After some testing I found the underlying cause. When executing a custom controller for the first time Magento automatically generates files. So far so good. In my case just one. It's living in var\generation\
The full path in this example would be var\generation\Demo\Hello\Controller\Adminhtml\Order\MassPrint\Interceptor.php
.
This class is extending your custom controller. So naturally it is calling the parent constuctor in its own constuctor. So if you make changes to your controller constructor this file must be regenerated to mach your new code. If you don't it will yield unwanted results. So be sure to at least delete the Interceptor class for that controller. Easier would be to empty the whole director so Magento will regenerate all files matching your new code.
rm -rf var/generation/*
Actually magento gives a very good clue where to look in the last part of the error message.
called in /var/generation/Demo/Hello/Controller/Adminhtml/Order/MassPrint/Interceptor.php on line 15
By Default create() and get() method is responsible for generate new object and retrieve obejct in magento 2.
Both function are defined in vendor/magento/framework/ObjectManager/ObjectManager.php
When you open above file, You can check create and get function defination.
with __construct()
defined in above file,
public function __construct(FactoryInterface $factory, ConfigInterface $config, &$sharedInstances = [])
{
$this->_config = $config;
$this->_factory = $factory;
$this->_sharedInstances = &$sharedInstances;
$this->_sharedInstances['Magento\Framework\ObjectManagerInterface'] = $this;
}
Here, $this->_factory is responsible for calling below files,
Magento\Framework\ObjectManager\FactoryInterface
is used to create dynamic object in magento 2.
Where FactoryInterface is linked to Magento/framework/ObjectManager/Factory/Dynamic/Developer class.
So all logic are resides in below files of create and get factory object,
All Dynamic Object are created from below path,
For Developer mode Object Creation is processed from this file,
vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php
For Production mode Object Creation is processed from this file,
vendor/magento/framework/ObjectManager/Factory/Dynamic/Production.php
Above both file extends Magento\Framework\ObjectManager\Factory\AbstractFactory.php
file.
create() function is responsible for create dynamic object of perticular factory.
Best Answer
Pagefactory is used to initialize the layout.
Its working in magento2 same like as in magento1 was doing with below code