Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:24:25+00:00 2026-06-13T02:24:25+00:00

Consider the following class hierarchy: Abstract class Printer{ public print(){ //code to handle printing

  • 0

Consider the following class hierarchy:

Abstract class Printer{
    public print(){
        //code to handle printing
    }
}

class LaserPrinter extends Printer{
    private $file;
    public setFile($file){
        $this->file = $file; 
    }
}

class InkJetPrinter extends Printer{
    private $document;
    public setDocument($document){
        $this->document= $document; 
    }
}

class ClientClass{
    private $filesToPrint=array();
    public __construct(InkJetPrinter $inkJetPrinter, LaserPrinter $laserPrinter){   //I was hoping to apply Dependency Inversion here by defining both inputs as type Printer instead
         //constructor stuff    
    }

    public function startPrinting(){
         //some logic to extract the $files and $documents from $this->filesToPrint
         //...
         $this->inkJetPrinter->setDocument($document);//<---Things got messy here
         $this->laserPrinter->setFile($file);//<---and here too
         //...
    }
}

Now the class LaserPrinter can’t be replaced by its parent Printer because Printer doesn’t have setFile method.
Does that mean that his hierarchy breaks the Liskov substitution principle? Are subclasses not allowed to have their own public methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:24:27+00:00Added an answer on June 13, 2026 at 2:24 am

    First of all, I assume that you have inheritance in that code, although I can’t see anything that hints that LaserPrinter or InkJetPrinter inherit from Printer.

    No, it doesn’t break the Liskov principle, because the subclass needs to behave properly when it’s used as a Printer.

    The bit that might be triggering your question is that the classes also should use Depedency Inversion, so the fully constructed LaserPrinter should be passed to anything that uses a Printer. In the example you have there, can the file change once LaserPrinter is created?

    If the file doesn’t need to change, then you probably want to pass it in the constructor of LaserPrinter. If the file can change, then you want to: create the file name inside LaserPrinter or create a new class which creates the file names and then inject the class in LaserPrinter.

    You might want to read about the Open/Closed and Dependency Inversion principles, as I think they are very closely related to your question.


    edit

    After looking at the code again, there’s something that I find odd. Printer.print() should receive a parameter describing what to print. Having the LaserPrinter or InkJetPrinter reference what they need to print from an instance variable looks wrong.

    For example you don’t say: Printer, here you have a document… now print it.

    It’s more like Printer, now that you’re fully configured and running, print this document

    It looks like you already have an object that represents what you want to print (that encapsulates a document and file). You can pass that object and have something like print(jobDetail).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Consider the following class hierarchy: public abstract class Entity { public virtual int Id
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following class definitions: import com.codahale.logula.Logging abstract class A extends Logging { log.info(from
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following code: class A { public: A& operator=( const A& ); const
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following class in a file MyClass.cs using System; public class MyClass :
Consider the following Java exception classes: public class BarException extends RuntimeException { // [...]
Consider the following class: public class Score { private static readonly Guid _relationId =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.