1.What is Polymorphism ?
Ans: Polymorphism means the ability to have many forms.The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same wayThere are two types of Polymorphism; they are:
- Compile time (function overloading) or static polymorphism
- Run time (function overriding) or dynamic polymorphism
- Property Overloading
- Method Overloading
- _set(): triggered while initializing overloaded properties.
- __get(): triggered while using overloaded properties with PHP print statements.
- __isset(): This magic method is invoked when we check overloaded properties with isset() function
- __unset(): Similarly, this function will be invoked on using PHP unset() for overloaded properties.
<?php
Setting 'a' to '1'
Getting 'a: 1 Is 'a' set? bool(true) Unsetting 'a' Is 'a' set? bool(false) 1 Private property are visible inside the class 2 Private property are not visible outside of class Getting 'hidden:
- __call() – triggered while invoking overloaded methods in the object context.
- __callStatic() – triggered while invoking overloaded methods in static context.
Calling object method 'runTest' in object context Calling static method 'runTest' in static context
3. Can you call parent class constructor using clild class object ?
An
|
Parent constructors are not called implicitly if the child class defines a constructor.
In order to run a parent constructor, a call to parent::__construct() within
the child constructor is required.
ex :
abstract class view
|
3.Interface Vs abstract class ?
Abstrsct Class Properties
1. Contains an abstract method
2. Cannot be directly initialized
3. Cannot create an object of abstract class
4. Only used for inheritance purposes
5.Abstract Class is not possible to implement multiple inheritance.
1. Cannot contain a body
2. Cannot be defined as private
3. Child classes must define the methods declared in abstract class
Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.
interface MyInterface{
public function foo();
public function bar();
}
abstract class MyAbstract1{
abstract public function baz();
}
abstract class MyAbstract2 extends MyAbstract1 implements MyInterface{
public function foo(){ echo "foo"; }
public function bar(){ echo "bar"; }
public function baz(){ echo "baz"; }
}
class MyClass extends MyAbstract2{
}
$obj=new MyClass;
$obj->foo();
$obj->bar();
$obj->baz();
//output: foobarbaz
abstract class AbstractClass
{
abstract protected function getValue();
abstract protected function prefixValue($prefix);
public function printOut() {
echo "Hello how are you?";
}
}
$obj=new AbstractClass();
$obj->printOut();
//Fatal error: Cannot instantiate abstract class AbstractClass
4. What is Data Abstraction ?
5.What is Encapsulation ?
ANS: Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.
More Details ; http://www.csharpstar.com/top-30-oops-interview-questions/
