Related and unrelated classes

  • Note

    1. Related classes

    A related class shares a logical connection or hierarchical connection with another class through inheritance or interface or composition

    1. Inheritance Example
    
    
    class Animal {
        public function makeSound() {
            echo "Some sound";
        }
    }
    
    class Dog extends Animal {
        public function makeSound() {
            echo "Bark";
        }
    }
    
    
    2. Interface example
    
    
    interface Logger {
        public function log($message);
    }
    
    class FileLogger implements Logger {
        public function log($message) {
            echo "Logging to file: $message";
        }
    }
    
    
    3. Composition exmple
    
    
    class Engine {
        public function start() {
            echo "Engine started";
        }
    }
    
    class Car {
        private $engine;
    
        public function __construct() {
            $this->engine = new Engine();
        }
    
        public function startCar() {
            $this->engine->start();
        }
    }
    
    

    2. Unrelated Classes

    An unrelated class has no direct relationship — it does not extend from same class or implement from the same interface.

    
    
    class Car {
        public function startEngine() {
            echo "Engine started";
        }
    }
    
    class Fruit {
        public function peel() {
            echo "Fruit peeled";
        }
    }