Loading…
oops interview questions

OOPS interview questions and answers

Looking for a new job? Do not miss to read our OOPS interview questions and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.

OOPS stands for Object Oriented Programming system. It is a programming technique which is used to design your application. In this programs are considered as a collection of objects and each object is an instance of a class.

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction etc

Class is a user-defined datatype that contains variables, properties, and methods. It defines properties of an Object

Example

                                                    
class Person{

    public $name;

    function __construct($name){

        $this->name = $name;

    }

    function getUserDetails(){

        return "My name is ".$this->name;

    }

}

Object is an instance of a class. It has it's own behavior, and identity.

Example

                                                    
//Create an object of MyClass 

$object = new MyCustomClass();

OR

$object = new MyCustomClass;

It is an attribute of an object, and it contains all data which is hidden. That hidden data is restricted to the members of that class.

It takes more than one form. It is nothing but assigning behavior or value in a subclass to something that was already declared in the main class.

It is a technique in which one class acquires the property of another class. With the help of inheritance, we can reuse the fields and methods of the existing class.

Inheritance has three type, are given below.

  • Single inheritance
  • Multiple inheritance
  • Multi level inheritance

But PHP supports only single inheritance, where only one class can be derived from single parent class. We can also use multiple inheritance by using interfaces.

Constructor and Destructor both are special functions which are automatically called when an object is created and destroyed.

Example

                                                    

Constructor

class Animal

{

     public $name = "No-name animal";

     public function __construct() {

     echo "I'm alive!";

}

}

Destructor

class Animal {

public $name = "No-name animal";

public function __construct($name) {

echo "I'm alive!";

$this->name = $name;

}

public function __destruct() {

echo "I'm dead now :(";

}

}

$animal = new Animal("Bob");

echo "Name of the animal: " . $animal->name;

 

PHP have three access modifiers such as public, private and protected.

  • public scope of this variable or function is available from anywhere, other classes and instances of the object.
  • private scope of this variable or function is available in its own class only.
  • protected scope of this variable or function is available in all classes that extend current class including the parent class.

It's properties can not be declared final, only classes and methods may be declared as final. If the class or method defined as final then it cannot be extended.

class childClassname extends parentClassname { 
    protected $numPages;

    public function __construct($autor, $pages) {
        $this->_autor = $autor;
        $this->numPages = $pages;
    }

    final public function PageCount() { 
        return $this->numPages; 
    }
}
When a variable or function declared as static then it cannot be accessed with an instantiated class object. It is treats as public if no visibility is defined. It can also be used to define static variables and for late static bindings.

Overloading : It occurs when two or more methods in one class have the same method name but different parameters.

Overriding : It means having two methods with the same method name and parameters. One of the methods is in the parent class and the other is in the child class.

It refers to the current object of a class.

Thera are many differences between abstract class and interface in php.

1. Abstract methods can declare with public, private and protected. But in case of Interface methods declared with public.

2. Abstract classes can have constants, members, method stubs and defined methods, but interfaces can only have constants and methods stubs.

3. Abstract classes doest not support multiple inheritance but interface support this.

4. Abstract classes can contain constructors but interface doest not support constructors.

It allows us to use the same function or class name in different parts of the same program without causing a name collision.

namespace MyAPP;
function output() {
echo 'IOS!';
}
namespace MyNeWAPP;
function output(){
echo 'RSS!';
}

 

Traits is a group of methods that reuse in single inheritance. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods.

Example

                                                    


trait HelloWorld

{

use Hello, World;

}

class MyWorld {

use HelloWorld;

}

$world = new MyWorld();

echo $world->sayHello() . " " . $world->sayWorld(); //Hello World

Magic methods are special names, starts with two underscores, which denote methods which will be triggered in response to particular PHP events.

The magic methods available in PHP are given below:-

  • __construct()
  • __destruct()
  • __call()
  • __get()
  • __set()
  • __isset()
  • __unset()
  • __sleep()
  • __clone()
  • __autoload() etc
  • Code Resusability
  • Flexibility
  • Maintainability
  • Security
  • Testability

Member variables defined inside a class. Data of member variables will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

Member function defined inside a class and are used to access object data.

Ask Question