Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.
Hannah Smalltree, Editorial Director
public class A
{
public String sayHello()
{
return "Hello";
}
}
public class B extends A
{
public String sayHello()
{
return super.sayHello() + " world!";
}
}In the above example, a call to the sayHello method of class B will return "Hello world!", since class B "inherits" the "Hello" from class A. Notice that the keyword "super" is used to call the method on the parent class.
Runtime inheritance refers to the ability to construct the parent/child hierarchy tree at runtime. While Java does not allow this natively, there are a number of projects and technologies available that will enable you to modify the bytecode of a class after compilation. While they really aren't intended to use for runtime inheritance, they could do the job.
An alternative to native runtime inheritance is a concept known as "delegation", which refers to constructing a hierarchy of object "instances" at runtime. This technique will allow you to simulate runtime inheritance. In Java, delegation is typically achieved in a manner similar to the following:
public class A
{
public String sayHello()
{
return "Hello";
}
}
public class B
{
public String sayHello()
{
return new A().sayHello();
}
} This was first published in August 2004