aspose file tools*
The moose likes Beginning Java and the fly likes some definitions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "some definitions" Watch "some definitions" New topic

some definitions

Muhammad Usman

Joined: Nov 18, 2001
Posts: 29
hello brothers,
can any one tell me the following definitions.
1. early binding
2. late binding
3. composition
i will be thankful

an student of Operation Badar ( a educational movement in Pakistan).
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Early binding = at compile time
Late binding = at Runtime
Composition = "what composes it" or what it is made of.
Of course the ramifications of those definitions are much trickier to catch on to.
Early binding allows the compiler to convert final variables into constants - which is more efficient.
Late binding allows for polymorphism, where the JVM does not identify which "version" of a method to use until runtime, so a sub-class can override the methods of its superclass.
If you "favor composition over inheritance" it means that, given a choice, you should chose to have your object have fields that reference objects of a second class, and then you use it rather than sub-classing the second class. The fields of an object compose the state of the object.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Early binding happens in non-polymorphic languages.
It also happens when you make static references, or when the compiler can tell at compile time that a method reference is not polymorphic.
Here are some examples:
class foo{
static int anInt = 5;
final String aMethod(){ return "aMethod()";}
public void anotherMethod() { return "anotherMethod();}
}//end class foo
Class foo is not final, so it can be subclassed. The child subclass may override anotherMethod(), but it cannot override aMethod() since it's final. The compiler can make use of this information when it compiles this code.
When you write in your code
foo aFoo = new foo();
These are all examples of early binding. The compiler creates bytecode that references the aMethod() and anInt field directly. At runtime, there is no additional checking required. The method dispatch is always the same.
Compare this with late binding. Continuing with our example, when whe write
The compiler sees that the type of aFoo is class foo, and that this is not a final class, so it is possible that it can be subclassed. Also, anotherMethod() is not final so it is possible that in a subclass of foo, this method has been overriden. Since these are all reasonable possibilities, the compiler cannot make a static reference to foo's anotherMethod(); it must allow for the possibility that at runtime, the variable aFoo really contains a subclass of foo. So the compiler inserts bytecodes at compile time to check the run-time type of variable aFoo. ONLY at runtime can it be determined exactly which anotherMethod() will be run.
Composition is just a design description whereby you create a class that is composed, or contains, other classes that help it do some meaningful work.
class foo{
String aString;
Random aRandom;
In this example, class foo is composed of multiple classes. It's like the sum of foo is equal to the composite of its member class instances aString and aRandom.
Also, there is a compositition design pattern whereby a superclass is designed to allow containment of classes that are derived as children from the superclass (See the relationship between javax.swing.JComponent and its relationship to java.awt.Container.)


SCJP 1.4
I agree. Here's the link:
subject: some definitions