Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method doudt

 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody help me in explaining why it is invoking base class method .,
class BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am BaseClass");
}
}
class SubClass extends BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am SubClass");
}
public static void main(String [] arg){
BaseClass bc = new SubClass();
bc.sayHello();
}
}
Also try making method sayHello as instance method and see the output... it will call subclass method.
[This message has been edited by Suren Babu (edited November 03, 2000).]
 
Milind Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Actually static methods are not overridden in the true sense. They are 'hidden' by subclass method of same signature and return type. So, they do not participate in dynamic binding. Thus irrespective of which refernce a superclass is holding, it JVM invokes superclass method, if method in question is static.
Hope this helps
Milind
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya im agree with mlind
Folowing are some rules that will help u

Hidding and Overriding

What u hides?
Varibles(static,instance)(static can also hide instance and vice versa)
Static methods

What u can override is?
Instance Methods.
So when ur hiding it is Type of reference varible deter mine whic member to access.But when u overrides it is Type of Object Which determine Which method is to called i.e Dyanmic Lookup ocurr only for overidden method

A compile-time error occurs if an instance method overrides a static method. In this respect, overriding of methods differs from hiding of fields , for it is permissible for an instance variable to hide a static variable.
U cannot Hide Instance method and cannot override Static methods .But Instance varibles and static varibles can hide each other as stated above

Even if two distinct inherited fields have the same type, the same value, and are both final, any reference to either field by simple name is considered ambiguous and results in a compile-time error. In the example:

The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile-time error occurs.

. A compile-time error occurs if a method declaration that contains the keyword abstract also contains any one of the keywords private, static, final, native, or synchronized.

A method that overrides or hides another method , including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its superclasses must occur in the throws clause of m; otherwise, a compile-time error occurs.

Note that it is possible for a method to have a declared return type and yet contain no return statements. Here is one example:
class DizzyDean {
int pitch() { throw new RuntimeException("90 mph?!"); }
}
If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts with that of any method that it overrides or hides; otherwise, a compile-time error occurs. In these respects, overriding of methods differs from hiding of fields for it is permissible for a field to hide a field of another type(as int can hide float)

The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:

If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.

If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.
Note that a private method is never accessible to subclasses and so cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.in Short a private method in super class can redeclared in subclass without any above say restictions.

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in the superclasses and superinterfaces of the class.
If a field declaration hides the declaration of another field, the two fields need not have the same type.
A class inherits from its direct superclass and direct superinterfaces all the fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class
u can Override method and can make it Final.

Hope this will help
 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Milind,
Thanks for your response..
i have a small doudt in your explanation...
"They are 'hidden' by subclass method of same signature and return type. So, they do not participate in dynamic binding."
Even the instance methods, if it is overridden then they are hidden from the super class... so what makes static methods not to participate in dynamic binding...
Actually in my code..
BaseClass bc = new SubClass();
bc.sayHello();
SubClass object is created and then with that object reference only i invoked sayHello..
Could you explain with more tech. inputs .
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suren,
What you're seeing is the result of the way Java resolves references at runtime. Static members are resolved using the declared type. Non-static, or instance members, are resolved using the actual type.
In your example, the object 'bc' is declared as type 'BaseClass' and created using 'SubClass'. Because <code>sayHello()</code> is static, the JVM resolves the reference to <code>sayHello()</code> using the declared type BaseClass.
Below I've modified you're code and added a new non-static method, <code>sayGoodbye()</code> in both the BaseClass and SubClass.

The output is:

Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic