Interview Questions for class and object

  1. What is class in PHP?

    A class is a collection of objects with common properties, methods, relationship and variables. A class is a template for an object. A class can be created by class keyword.
    Syntax : class class-name { }
     
  2. What is object in PHP?

    Object is an instance of a class. Objects are created from Classes dynamically to access  properties, methods and variables. We can create object of class using new keyword.
     
  3. What is the difference between Classes and Objects?

    1. A class is a definition where an object is an instance of the class.
    2. A class is a blueprint while objects are actual objects existing in the real world.
    3. Class is a template for creating objects where Object are a implementation of class.
    4.Class is declared using "class" keyword where Object is created by 'new' keyword.
    5. Memory space is not allocated when class is created where Memory space is allocated when object is created.
    6. Class have many objects where Object belongs to only class.
     
  4. What are access modifiers?

    There are three type of access modifiers as below.
    public: Public method or variable of a class can be accessible from anywhere such as outside of the class or in a subclass.
    protected: A protected method or variable of a class can only be called in that class & it's subclass.
    private: A private method or variable of a class can only be called inside that class only in which it is declared.
     
  5. What is Encapsulation?

    Encapsulation is used to hide member variables and methods together inside class to prevent unauthorized parties' direct access to them. Visibility scope is the mechanism for encapsulation.
     
  6. What is Abstraction class?

    If  implementation details are hidden, then it is called Abstraction. Abstract method is declared but it not defined. Abstract method cannot contain the implementation.  Abstract class contains atleast one or more abstract method.

    Classes defined as abstract may not be instantiated  Classes that contains at least one abstract method must also be abstract.

     

  7. What is the difference between concrete class and abstract class?

    Difference between Abstract class and Interface
    Abstract class Interface
    A method must be declared as abstract in abstract class. Abstract methods doesn’t have any implementation. All the methods by default are abstract in interface.
    An Abstract methods can be declare with access modifiers like public, protected.Concrete methods can be declared public,private and protected. All methods declared in an interface must be public.
    Abstract class can also contain member variables and concrete methods. Interfaces cannot contain any member variables and concrete methods except constants.
    A class can Inherits only one Abstract class and Multiple inheritance is not possible for Abstract class. A class can implements many interfaces and Multiple interface inheritance is possible.
    Only complete member of abstract class can be static. Memebrs of Interface can not be static.

     

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

32602