Inheritance

It is a concept of accessing the features of one class from another class. If we inherit the class features into another class, we can access both class properties. We can extends the features of a class by using 'extends' keyword.

  • It supports the concept of hierarchical classification.
  • Inheritance has three types, single, multiple and multilevel Inheritance.
  • PHP supports only single inheritance, where only one class can be derived from single parent class.
  • We can simulate multiple inheritance by using interfaces.

Example 1

snippet
<?php
class a
	{
		function fun1()
		{
			echo "rookienerd";
		}
	}
	class b extends a
	{
		function fun2()
		{
			echo "SSSIT";
		}
	}
	$obj= new b();
	$obj->fun1();
?>

Output:

INHERITANCE

Example 2

snippet
<?php
	class demo
	{
		public function display()
		{
			echo "example of inheritance  ";
		}	
	}
	class demo1 extends demo
	{
		public function view()
		{
			echo "in php";
		}	
	}
	$obj= new demo1();
	$obj->display();
	$obj->view();
?>

Output:

INHERITANCE
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +