109
  • When should implement or extend be used?
  • What are some real-world examples?

Is this correct?

Implementing appears to be a way to enforce that certain methods exists in a class, and that these methods function calls are properly formatted. Implementing is not a way of passing variables or "settings" to the class though?

Expected real-life scenario: I have an e-commerce platform featuring multiple payment classes that all follow the same design. When a new payment class should be added, it's really easy to follow the defined design of the interface to ensure that all bits and pieces are there, from the beginning.

Extending classes makes the extended (child?) class inherit everything from its parent class except methods & variables declared as private?

Expected real-life scenario: I have one class called sessions with two child classes named sessioncookies and databasesessions. sessioncookies and databasesessions, together inherit a number of mutual config options from their parent sessions, making it easy to change a config option to affect all sorts of eventual visitor data storage.

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
Industrial
  • 41,400
  • 69
  • 194
  • 289

2 Answers2

76

Inheritance is useful to reduce the amount of code you rewrite. If you have several classes with a few common methods or fields, instead of defining these methods and fields over and over you can factor them into a base class and have each of the child classes extend that base class.

Interfaces (and implements) are useful when you'd like to define a common protocol for how a group of objects should behave. For example, you might want to mandate that objects that are comparable can be compared for equality and hashed, etc.

Using inheritance is ultimately a design choice. Be on the lookout for cases where you define the same methods across several classes; those are excellent cases where you can factor those methods out into a base class. The same goes for classes that observe some of the same characteristics: you can guarantee consistency by putting those characteristics in an interface to be implemented by those related classes.

Inheritance is a big concept in OOP that goes way beyond just PHP. I recommend you read the wikipedia article on inheritance and perhaps Design Patterns by the Gang of Four.

I believe your understanding of inheritance is mainly correct. The next step would be to use it in production.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 5
    Many people now regard The Gang of Four book to be too heavy in terms of material as an introduction to design patterns. A popular alternative is Head First: Design Patterns which makes for lighter reading. http://www.amazon.co.uk/Head-First-Design-Patterns-Freeman/dp/0596007124 – Bendihossan Nov 07 '12 at 09:19
  • 1
    "Be on the lookout for cases where you define the same methods across several classes; those are excellent cases where you can factor those methods out into a base class." Would extending a Class have a benefit over using a Trait to define those methods? – Daniel Weiner May 19 '15 at 17:04
  • 2
    "Inheritance is useful to reduce the amount of code you rewrite" - The purpose of inheritance is to share behaviour, not code. Where possible, traits are the way to share code rather than extending an abstract class with concrete classes that otherwise have nothing in common. You can usually model your classes as has-a rather than is-a relationships, too, which will further facilitate both code reusability and polymorphism. – Duncan Feb 28 '19 at 01:32
36

Implement: Interfaces are abstract classes without implementation details, so you can only declare things(Contracts). A class implements an interface to make sure that it follows the interface's rules and contracts. A class can implement multiple interfaces.

Extend: You extend classes when you want a more specific version of a class. And also you don't want to repeat writing other methods that exist in the parent class.

Example:

// Contract: a pet should play
public interface Pet {
    public void play(); 
}

// An animal eats and sleeps
class Animal {
    public void eat(){ //details };
    public void sleep(){ //details };
}


public class Camel extends Animal {
    // no need to implement eat() and sleep() but
    // either of them can be implemented if needed. i.e. 
    // if Camel eats or sleeps diffrently from other animals!
}

public class Dog extends Animal implements Pet {    
    public void play() {
       // MUST implemt play() details           
    }
}

Both Camel and Dog are animals, so they extend Animal class. But only the Dog is a specific kind of Animal that also can be a Pet.

As you see this thread is closed because it's opinion-base there is no exact correct answer. It's a design choice and depends on the situation and project needs. you have both options available, now you can decide what is the best choice. for example dog class does not have to always be like in the above example, if the project is all about the different kind of dogs (and not other animals) and even their sleeping and eating difference is important! it can be like this:

// the main contract
public interface Dog {
   public void eat();
   public void sleep();
} 

// Contract
public interface Pet {
    public void play(); 
} 

// Contract
public interface Hunter {
    public void hunt();
}

public class FamilyDogs implements Dog, Pet {
    // must implement all details
} 

public class GuardDogs implements Dog, Hunter {    
    // must implement all details
}
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69