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

  • SEARCH
  • Home
  • 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 8782739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:38:50+00:00 2026-06-13T20:38:50+00:00

Possible Duplicate: Prefer composition over inheritance? What is the difference between inheritance and delegation

  • 0

Possible Duplicate:
Prefer composition over inheritance?

What is the difference between inheritance and delegation in java?

How to use the following example in my project? Please can you guide me with delegation. I know about inheritance but do not have much knowledge about delegation. So, please give a proper reason. Why should I use this?

 package com.m;

 class RealPrinter { // the "delegate"
     void print() { 
      System.out.println("something"); 
    }
 }

 class Printer { // the "delegator"
     RealPrinter p = new RealPrinter(); // create the delegate 
     void print() { 
     p.print(); // delegation
     } 
 }

 public class Tester {

// to the outside world it looks like Printer actually prints.
     public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print();
     }

   }
  • 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-13T20:38:52+00:00Added an answer on June 13, 2026 at 8:38 pm

    When you delegate, you are simply calling up some class which knows what must be done. You do not really care how it does it, all you care about is that the class you are calling knows what needs doing.

    If I were you though I would make an interface and name it IPrinter (or something along those lines) which has one method named print. I would then make RealPrinter implement this interface. Finally, I would change this line: RealPrinter p = new RealPrinter(); to this: IPrinter p = new RealPrinter().

    Since RealPrinter implements IPrinter, then I know for sure that it has a print method. I can then use this method to change the printing behaviour of my application by delegating it to the appropriate class.

    This usually allows for more flexibility since you do not embed the behaviour in your specific class, but rather leave it to another class.

    In this case, to change the behaviour of your application with regards to printing, you just need to create another class which implements IPrinter and then change this line: IPrinter p = new RealPrinter(); to IPrinter p = new MyOtherPrinter();.

    public interface IPrinter {
        void print();
    }
    
    public class RealPrinter implements IPrinter {
    
        @Override
        public void print() {
            System.out.println("This is RealPrinter");
        }
    }
    
    public class RealPrinter2 implements IPrinter {
    
        @Override
        public void print() {
            System.out.println("This is RealPrinter2");
        }
    }
    
    public class Main {
    
        public static void main(String...arg){
            IPrinter printer  = new RealPrinter();
            printer.print();
    
            printer = new RealPrinter2();
            printer.print();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Prefer composition over inheritance? I wonder, why (or in which cases) should
Possible Duplicate: Prefer composition over inheritance? A general question which I stumbled upon several
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: C++ When should we prefer to use a two chained static_cast over
Possible Duplicate: objective c difference between id and void * For example, an UIView
Possible Duplicate: Should I prefer to use literal syntax or constructors for creating dictionaries
Possible Duplicate: When to prefer JSON over XML? i wonder if JSON is the
Possible Duplicate: B- trees, B+ trees difference What are the advantages/disadvantages of BTree+ over
Possible Duplicate: Difference between try-catch syntax for function What is the difference in the
Possible Duplicate: Should I always prefer MySQL InnoDB over MyISAM? Under what conditions should

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.