'Is this an example violation of Liskov Substitution Principle?

From stackify:

"The principle defines that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. That requires the objects of your subclasses to behave in the same way as the objects of your superclass."

I am trying to fully grasp this principle, and I have an example. I am wondering, i any or both of these examples are violations of LSP?

1:

class FolderBuffer {
    getFileByName(name: string) {
        // retrieve file from buffer
    }
}

class IndexHtmlFolderBuffer extends FolderBuffer {
    getFile() {
        try {
            return super.getFileByName('index.html');
        } catch {
            throw Error('index.html not found');
        }
    }
}

2:

class FolderBuffer {
    buffer: Buffer;

    constructor(buffer: Buffer) {
        super(buffer)
        if (!buffer.includes('index.html')) {
            throw Error('index.html not found');
        }
    }

    getFileByName(name: string) {
        // retrieve file from buffer
    }
}

class IndexHtmlFolderBuffer extends FolderBuffer {
    getFile() {
        return super.getFileByName('index.html');
    }
}

Thanks for the input!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source