Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I faced the below interview questions in OOP under PHP language. Kindly clear my clarifications regarding this. I am very confused. As i am a beginner to OOP i got too confused. Could anyone clarify these things clearly?

  1. Difference between Abstract class and interface.

    Interviewer : Let us consider abstract class contains three abstract methods such as a,b,c and interface contains three methods a,b,c. In this case these do the same functionality. Then why are going for abstract and why are we going for interface.

    Me : ?

  2. static keyword.

    Interviewer: We call static method without creating object by using scope resolution operator in PHP. As well as we can able to call concrete methods also. Then what is need of static keyword there?

    Me : ....

  3. final keyword.

    Interviewer: Give me any scenario of using final keyword.

    Me : For db connection related method

    Interviewer: Other than that?

    Me: ...

  4. Constructor.

    Interviewer: What is the use of constructor?

    Me : There is no need for object to access this. It will call automatically when the class calls.

    Interviewer: Other than that?

    Me : ....

Thanks in advance...

share

migration rejected from stackoverflow.com Jan 2 at 10:22

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as too broad by MichaelT, gnat, Bart van Ingen Schenau, GlenH7, Simon Jan 2 at 10:22

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers 5

up vote 15 down vote accepted
  1. Difference between Abstract class and interface.

    Abstract classes are extended, whilst an interface is implemented. A class should be really only be extended if it first the "is a" principle. e.g. Dog is a Pet, therefore it makes sense that Dog should extend Pet. Interfaces should be used to provide a common interface to use in situations, in PHP you can implement ArrayAccess, which requires you to implement certain functions.

    Use abstract classes when other classes should be created that are a specialization of another class. In the case mentioned an abstract class is no different to an interface, however an abstract class can create non-abstract functions that provide default behaviour.

    Use interfaces allow for multiple inheritance, most useful for defining "mixins", such as ArrayAccess, Serializable or Sortable, where the class isn't a specialization but are more attributes of a class.

  2. static keyword.

    This is fairly basic, a static variable or function are part of a Class and can be accessed using ClassName::$property or ClassName::functionName(), without the need for creating an instance/object of that class.

  3. final keyword.

    By declaring a class or function final, it cannot be overridden. A class declared final will have no child classes, and function in a class declared final cannot be overridden in a child class.

    The final keyword on functions is useful when the function is critical to the consistent state of the object. If you have a delete function you might wish to declare it final, so classes that extend it cannot prevent deletion.

    A class is useful to be declared final when you do not want its implementation to be changed, the primary example is the String class in Java which is immutable.

  4. Constructor.

    Called on object creation. When you use the new keyword to create an object $object = new Object(). Constructors should be used to initialise object properties, the main reason being you want to keep your objects in a complete state.

    Here's a basic example. If you have a Dog class with a property name, and a function speak that introduces itself using its name. The name should be defined in the constructor, otherwise a Dog could be created and the speak function called and failing because it doesn't know its name.

share

I'm answering this from C# perspective, which may or may not apply to PHP.

Constructor is basically used to initialize member variables. A class can only inherit from 1 abstract class, but a class can inherit multiple interfaces

as for the other questions, not sure.

share
    
found some article on difference between abstract and interface –  Fun Mun Pieng Mar 8 '11 at 5:32

why are going for abstract and why are we going for interface.

Whenever we need to implement multiple inheritance we should use interface instead of abstract because a class can inherit only one abstract class so multiple inheritance is not possible via abstract class. Also If you want to declare non-public members, then use abstract method (Because all method declare in interface must be public). In case of abstract class, you are free to add new public methods in the future, but in reverse interface are used for stable development.

share

What is the use of constructor?

Constructor are automatically invoked when the object is created, so it's used for initialization of member variables. And the second use is to execute some code in object initialization time, if we want some code to be executed automatically whenever the class object is created. The code that we want to execute must be put in the constructor.

share

Interesting, you're being asked the "why" not the "what" for these...

Difference between Abstract class and interface.

An abstract class lets you define default behavior, while an interface does not. If the deriving types should share some common behavior then an abstract class is preferred. If they share common interface then...yeah.

More subtly, an interface is a contract on a type that states what it is able to respond to, but nothing about what it is. An abstract base class says something about what derived types actually are, and what they are not because they can only inherit from the one base type.

EDIT: As a little addendum to this post: the two are not mutually exclusive. I have used an interface to define a contract I'd like some objects to use, then implemented that interface on a base class that my several objects will derive from. The abstract base provides some helper methods that would be common to that family of types, but not part of the public behaviors of those objects. Also the interface could be reused by any other types that wanted to expose that behavior, but didn't want the baggage of my base class.

static keyword

In a language that allows "free standing" global methods and static methods on objects, the reason to prefer the latter is that it provides 1) better scoping and 2) context.

Say you wanted a method CreateNew() to act as a factory for some type. Making it static on that type makes it obvious what it does: Dogs::CreateNew(). The alternative, CreateNew(Type t) tells you nothing about what limitations the method may have. Worse, it would be a nightmare to maintain.

final keyword

Honestly, I very rarely come across methods or classes I would definitely never want someone to extend, and frequently come across other people's code I wish I could extend but can't due to final.

That said, good candidates include: Any method (or class) with dangerous, complex, or slow side effects. Any method that other classes or methods rely on operating in a specific way (which is bad design, but it is a use case). Singletons. Code that no one should ever need to extend, like simple utility methods. But again, if there's no harm in allowing it, why bother locking it?

Constructor

A constructor is for initializing an object's private state prior to it being used. Any variables that need setting, objects that need making, or anything that needs to be done before an object is usable should be done in the constructor if possible.

share

Not the answer you're looking for? Browse other questions tagged or ask your own question.