• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Interface -vs- Abstract Class

You do not have to include the properties and methods of an abstract class or redefine them in a child class. An interface is an interface, it's just an outline saying these methods MUST be included in any class that uses the interface. Abstract classes cannot be instantiated, usually you put static methods and properties in an abstract class.
 
Interfaces are sort of contracts that anything that implements them has to implement all methods they have
Abstract classes are somewhat of an extension of interfaces, they have all the functionality of Interfaces, but you can have variables and implemented methods in them

A simple example(Java):

Code:
interface IAnimal{
   public int getLegCount();
   public String getName();
}

class Dog implements IAnimal{  //Here you see that the Dog class has implemented all the methods that the IAnimal interface has
   public int getLegCount(){ return 4; }
   public String getName(){ return "Dog"; }
}

abstract class Human implements IAnimal{ //Here you see that the Human class has implemented some methods, while leaving others for child classes to implement
   public int getLegCount(){ return 2; }
   public abstract String getName();
}

class MaleHuman extends Human{  //Here you see that the MaleHuman class implements the remaining methods that weren't implemented in Human
   public String getName(){ return "Male Human"; }
}
 
Well, Scarlet's example perfectly fits in the PHP concept. Also Black Reaper correctly described the difference.
Interfaces are best to use if you want to create a possibility for others to plug into your software so that you REQUIRE from them specific functions. I use interfaces to mark some functions too, eg. you can implement interface and then check a class/object if it implements it or not. It allows you to check for multiple interfaces in one class because it can implement more than one interface but can extend only one class.

Plus, you are wrong about the scrictness of anything when extending (abstract) class. It does not force anything, you can, but you are not required to overwrite any function/property.
 
Back
Top