What is difference between abstract, interfaces, namespaces?

Posted on 2014-01-19
Last Modified: 2014-01-23
What is difference between abstract, interfaces, namespaces?

I am learning oop. Please answer with words or a link that compares contrasts all 3. Not looking for individual pages for each.

Abstract child classes can use the same parent constructor

Why would a class use multiple interfaces

Objects can be employee, department from the same namespace file
Question by:rgb192
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39792551
You can't really compare namespaces with the other two.  Like the concept of variable scope in functions, the namespace is a way of encapsulating items.  They provide a way in which to group related classes, interfaces, functions and constants.  Unfortunately PHP made the namespace separator into the back-slash which is also the DIRECTORY_SEPARATOR if you're running on a Windows machine.  I guess PHP has a sense of humor and has decided to mess around with Windows users!

Interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.  A class that implements an interface must define all of the methods called for in the interface.

An interface is typically pretty short, since all it does is list the names and argument signatures of the methods it requires.  And in the argument signatures, there will be no code at all.  Instead you will see a semi-colon terminator after the arguments.  You do not instantiate an interface.
public function myMethod($name, $size, $color);

You do not instantiate abstract classes either, you only extend* them.  Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature (name, argument list) - they cannot define the implementation.  The implementation must be defined in the class that extends the abstract class.

The abstract class can have some methods that are not abstract.  So you can think of an abstract class as being a partially formed thing, unusable by itself, but providing structure and, optionally, some of the functionality you need.

* Sorry for the link to PHP4, but it's the best explanation on the site.
LVL 34

Accepted Solution

gr8gonzo earned 250 total points
ID: 39792664
To build on what Ray said, it can be hard sometimes to understand the reason WHY you would want to use any of those three items. Often times, you are just thinking about how YOU would build an application. However, those three concepts are vital when you're building applications that will be distributed and used by other people.

Imagine you are a car manufacturer, just like Toyota, Honda, Ford, etc... Each of you has built your own "Car" class. Toyota's "Car" might have different features and abilities than Ford's "Car". Now imagine that you are trying to enter your "Car" into a race against all the others, so you have to submit your "Car" code to a 3rd party. How can you make sure that your "Car" doesn't accidentally end up using the "Tire" class from Honda, or the "SteeringWheel" class from Toyota?

This is where namespaces come in. A namespace is simply a container for your class(es). This way, you can make sure that when your "Car" needs a "Tire", that it is getting the right one. In essence, a namespace is almost like adding a prefix to all your classes:


However, instead of trying to change all of your class names, you simply put them all into the same namespace and they won't get confused or have any conflicts with other classes that have the same name:

namespace RGB192;

class Car

class Tire

class SteeringWheel

... will define RGB192\Car, RGB192Tire, RGB192\SteeringWheel, etc... and you can use that code in an application that also has other classes with the same names.

Interfaces are pretty simple. Just think of interfaces as a list of requirements. Now let's say you are a computer manufacturer. If you built a computer ANY way you wanted, you might end up with something that doesn't turn on because maybe YOU decided that you wanted to make the power plug look like your initials.

In order to make your computer compatible so that other people can plug your computer into the electrical socket in their wall, you are REQUIRED to build your computer in a way that uses a normal power plug that everyone else uses, not one that you built from scratch.

An interface is simply something that says, "When you build an instance of a Foo class, it needs to have X, Y, and Z." You can add more to it, but it needs AT LEAST X, Y, and Z. So an interface is just there to define requirements for other people who want to build their own instances of your class.

An abstract class is like a build-your-own-something toolkit. For example, you can buy "barebones" computer kits out there that provide you with a case, a motherboard, memory, and some other basic parts, but not everything.

It's a starting point, but it's not functional by itself.

The toolkit is meant for you to buy and then fill in with your own hardware (you provide the hard drive, video card, etc...).

This is like an abstract class. The abstract class is there so you can provide some basic functionality to other developers who want to use your code as a starting point. You can build out some of the code so they aren't building everything from scratch, but they're extending your class every time.

Author Closing Comment

ID: 39805680
Latter was more about the comparison which is what I wanted to learn.
Thanks both.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 32
Preventing users manipulating URL strings 10 33
Making register page log you in after registration is complete. 2 13
Problem sending file attachments 8 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now