The Artima Developer Community
Sponsored Link

Java Answers Forum
inheritance: what about static methods?

2 replies on 1 page. Most recent reply: Feb 24, 2003 5:24 AM by Ptite Ce

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Ptite Ce

Posts: 4
Nickname: ptitece
Registered: Feb, 2003

inheritance: what about static methods? Posted: Feb 13, 2003 2:50 AM
Reply to this message Reply
Advertisement
Hello,

I'm a beginner in Java programming language, and I'm reading the sun tutorial.
About inheritance:
I understand subclasses can override methods and hide member variables of the superclass.
But in the exercises, they talk also about hiding methods. It seems related to static method of the superclass.
So, what's about the inheritance of static methods?

Thanks,
PtiteCe.


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: inheritance: what about static methods? Posted: Feb 13, 2003 2:23 PM
Reply to this message Reply
Overriding and dynamic binding are closely realted. All non-static methods are dynamically bound. That is, at runtime it is determined which method to call. If a class overrides a non-static method then its method is called , otherwise its ancestor method would be called. Static method calls are bound at compile time and that is why overriding doesn't apply in case of static methods. When a subclass reimplements a static method then it is called method hiding and not method overriding. Let us take an example:
class A {
  static void s() { } 
  void d() { } 
}
 
class B extends A {
  static void s() { } 
  void d() { } 
 
}
 
When you write:
A a = new B() ;
a.d() ;
 
then class to a.d() is resolved at run time. Since at runtime a will refer to an object of class B, d() method in class B will be called here.
If you write:
A a = new A() ;
a.d() ;
then d() method in A will be called.
 
However, if you write:
A a = new B() ;
a.s() ;
then s() method in A will be called. It is so because s() is a static method and at cmpile time a.s() was replaced by A.s() becauase a was a reference variable of A type.
If you write:
A a = new A() ;
a.s() ;
 then A.s() will be called again.
 

Ptite Ce

Posts: 4
Nickname: ptitece
Registered: Feb, 2003

Re: inheritance: what about static methods? Posted: Feb 24, 2003 5:24 AM
Reply to this message Reply
Thanks,
It helps me to understand :-)
PtiteCe.

Flat View: This topic has 2 replies on 1 page
Topic: interface question Previous Topic   Next Topic Topic: anyone can help?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use