Wednesday, July 3, 2024
HomeLanguagesJavaDelegation vs Inheritance in Java

Delegation vs Inheritance in Java

Inheritance in Java programming is the process by which one class takes the property of another other class. i.e. the new classes, known as derived or child class, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super or parent class.
Delegation is simply passing a duty off to someone/something else. 
 

  • Delegation can be an alternative to inheritance.
  • Delegation means that you use an object of another class as an instance variable, and forward messages to the instance.
  • It is better than inheritance for many cases because it makes you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense.
  • Delegation can be viewed as a relationship between objects where one object forwards certain method calls to another object, called its delegate.
  • The primary advantage of delegation is run-time flexibility – the delegate can easily be changed at run-time. But unlike inheritance, delegation is not directly supported by most popular object-oriented languages, and it doesn’t facilitate dynamic polymorphism.

 

Java




// Java program to illustrate
// delegation
class RealPrinter {
    // the "delegate"
    void print()
    {
        System.out.println("The Delegate");
    }
}
 
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();
    }
}


Output: 
 

The Delegate

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.
 

Same code using Inheritance

 

Java




// Java program to illustrate
// Inheritance
class RealPrinter {
    // base class implements method
    void print()
    {
        System.out.println("Printing Data");
    }
} 3 // Printer Inheriting functionality of real printer
    class Printer extends RealPrinter {
 
    void print()
    {
        super.print(); // inside calling method of parent
    }
}
 
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();
    }
}


Output: 
 

Printing Data

When to use what? 
Here are some examples when inheritance or delegation are being used: 
Assume your class is called B and the derived/delegated to class is called A then If 
 

  • You want to express relationship (is-a) then you want to use Inheritance.
  • You want to be able to pass your class to an existing API expecting A’s then you need to use inheritance.
  • You want to enhance A, but A is final and can no further be sub-classed then you need to use composition and delegation.
  • You want functionality of a method and do not want to override the method then you should go for delegation.

This article is contributed by Abhishek Gupta. If you like Lazyroar and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments