Delegation Versus 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 super class, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or child class.

Inheritance is used to create a hierarchical-type code structure that tries to keep as much “common” code near the top of the hierarchy. In small, static systems, inheritance can be ok. But large inheritance chains can also lead to hard-to-maintain code. Read up on design patterns that favor composition over inheritance for more info when to use inheritance and when not to.

Delegation is simply passing a duty off to someone/something else.Delegation is 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 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. Delegation can also a powerful design/reuse technique. 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.

Here is a simple example:

class ABC {
	     void methodHello()
    {
    System.out.println("hello");

    }

    void methodBye()
    {
     System.out.println("bye"); }
    }

class XYZ {
	     ABC obj = new ABC();

	 void methodHello()
        {
         obj.methodHello();
        }

        void methodBye()
        {
         obj.methodBye();
        }

}

public class VinayMain {
	     public static void main(String[] args) {
	         XYZ obj = new XYZ();
	         obj.methodHello();
	         obj.methodBye();
	     }
}

Sometimes, the choice between delegation and inheritance is driven by external factors such as programming language support for multiple inheritance or design constraints requiring polymorphism. Consider threads in Java. You can associate a class with a thread in one of two ways: either by extending (inheriting) directly from class Thread, or by implementing the Runnable interface and then delegating to a Thread object. Often the approach taken is based on the restriction in Java that a class can only extend one class (i.e., Java does not support multiple inheritance). If the class you want to associate with a thread already extends some other class in the design, then you would have to use delegation; otherwise, extending class Thread would usually be the simpler approach

 

Pin it

Vinay

I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube