OOPS interview questions and Answers
OOPS, i.e. Object-Oriented Programming Language is one of the most important concepts which is widely used in the programming world. Every interview that you are attending today requires the knowledge of OOPS. The below article compiles the most frequently asked OOPS Interview Questions and Answers which help you to ace your interviews. If you don’t know object-oriented programming then you can’t think of developing mobile software. Its unique features like inheritance, data hiding which is also known as encapsulation, data binding, polymorphism make it superior to other programming languages.
Popular languages like Java, C++, and Python, etc. are using the concept of the oops model and making it more significant in the programming world. Through OOPs a user can break his program into small-sized problems and that can be solved easily like one object at one time. The system in which the concept of OOP is used can be easily upgraded and hence provides better quality of software, enhances programmer productivity, and lower maintenance cost.
Object Oriented Programming Interview Questions
Constructor and Destructor both are special functions which are automatically called when an object is created and destroyed.
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.
Final Class- A class that can’t be extended and inherited further is known as Final Class. This class is declared with the keyword final and should be declared.
Final Method- Methods in the final class are implicitly final and if a user uses the final keyword that means methods can’t be overridden by subclasses.
class childClassname extends parentClassname {
protected $numPages;
public function __construct($author, $pages) {
$this->_author = $author;
$this->numPages = $pages;
}
final public function PageCount() {
return $this->numPages;
}
}
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.
trait HelloWorld
{
use Hello, World;
}
class MyWorld {
use HelloWorld;
}
$world = new MyWorld();
echo $world->sayHello() . " " . $world->sayWorld(); //Hello World
- Code Resusability
- Flexibility
- Maintainability
- Security
- Testability
Conclusion
Object-Oriented Programming is a very vast concept and takes some time to master. Going only through these object-oriented programming interview questions won't help you. You have to brush up your mind by practicing more and more programming questions to keep you updated and to get a better understanding of OOPS concepts.
Let me give you a short and smart trick to crack these types of interviews. Whenever you look at any real entities try to relate them with the OOPS concept and try to find their methods, attributes. In this way, programming is more enjoyable and easier.