• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are delegates

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is delegates in core java
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the request on, you say that the object has forwarded responsibility for handling the request to the second object.

The following Stack class provides a simple example of composition and forwarding:

public class Stack {
private java.util.ArrayList list;

public Stack() {
list = new java.util.ArrayList();
}

public boolean empty() {
return list.isEmpty();
}

public Object peek() {
if( !empty() ) {
return list.get( 0 );
}
return null;
}

public Object pop() {
if( !empty() ) {
return list.remove( 0 );
}
return null;
}

public Object push( Object item ) {
list.add( 0, item );
return item;
}

}



Through composition, Stack holds on to an ArrayList instance. As you can see, Stack then forwards the requests to the ArrayList instance. Simple composition and request forwarding (such as that of the Stack class presented above) is often mistakenly referred to as delegation.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private java.util.ArrayList list;

is this statement right i mean i have usually seen while importing ArrayList??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's legal Java syntax, Vishal.

You can either import for example java.util.ArrayList and then use the simple name 'ArrayList', or not import it and use the fully qualified name 'java.util.ArrayList'.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic