Re: Static methods overridden !!
- From: "Daniel Pitts" <googlegroupie@xxxxxxxxxxxxx>
- Date: 25 Mar 2007 10:36:37 -0700
On Mar 25, 9:55 am, "Ravi" <v.r.san...@xxxxxxxxx> wrote:
Hi,
Consider the following piece of code
class AnimalTest {
public static void saySomething() {
System.out.println(" AnimalTest!");
}
}
class CowTest extends AnimalTest {
public static void saySomething1() {
System.out.println(" CowTest!");
}
public static void main(String[] args) {
AnimalTest[] animals = { new AnimalTest(), new CowTest() };
for (AnimalTest a : animals) {
a.saySomething();
}
new CowTest().saySomething();
}
}
Interestingly it prints out AnimalTest! thrice. I am wondering how did
it compile as CowTest doesn't have the method saySomething. Does the
static method got carried over to the subclass??
Regards,
Ravi
Static methods are Class level, so it doesn't matter WHAT object type
you have.
As a matter of fact, the following WILL work.
CowTest ct = null;
ct.saySomething();
Static calls are really replaced by CowTest.saySomething() (which it
inherits from AnimalTest)
Hope this helps,
Daniel.
.
- Follow-Ups:
- Re: Static methods overridden !!
- From: Ravi
- Re: Static methods overridden !!
- References:
- Static methods overridden !!
- From: Ravi
- Static methods overridden !!
- Prev by Date: Re: SyncronizingProxyFactory: Pattern or antipattern?
- Next by Date: Re: Choices - moving to Java environment
- Previous by thread: Static methods overridden !!
- Next by thread: Re: Static methods overridden !!
- Index(es):
Relevant Pages
|