This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes In continuation to static method overriden Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "In continuation to static method overriden" Watch "In continuation to static method overriden" New topic
Author

In continuation to static method overriden

Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Wat will happen, If I declare static final method in super class and redefine same method in sub class...
Sub-class can define same prototype which will be a different method all together and it will also hide the super class static method.





In above code, If I compile, It will give compilation error....
Method declare at line 1 is final.. At line 2, its a totally different method, its not overriden becoz of static declaration at line 1.

Wats the problem...

Regards

Naseem.K


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Krishna Bulusu
Ranch Hand

Joined: Jan 27, 2006
Posts: 32
hi,
final methods can not be visible to the childs classes.
In this case..the method f() is not visible to the child class...
if u remove the finla keyword from the parent class....it is nth but 'hiding' not 'overriding' b'coz f() is static...for this to happen..the method in the parent class should be visble to the child class....unless it is visible how can the compiler recognise it whether it is overriding or hiding??

plz correct me if i am wrong

krishna.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
1)final methods can not be visible to the childs classes-->WRONG.

final methods are visible in the subclass as below code shows.

class Parent {

static final void f() {System.out.println("parent");} // line (1)
}

class Child19of46 extends Parent {

//static final void f(){System.out.println("Child");} // line (2)

public static void main(String args[]){

Parent p = new Child19of46();
p.f();
}
}

2)declaring method as final(instance/static) in superclass implies

a)instance method in superclass can not be overriden in subclass.
b)static method in superclass can not be hidden in subclass.

3)Method declare at line 1 is final.. At line 2, its a totally different method, its not overriden becoz of static declaration at line 1.

a)This is the case for private methods.
b)Method at line (2) is not a totally different method.

4)You are getting compile time error because you are trying to hide static final method in superclass.

[ May 15, 2006: Message edited by: Girish Nagaraj ]
[ May 15, 2006: Message edited by: Girish Nagaraj ]
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 171
For static methods there is a concept of hidding not method overridding.For hidding also it is necessary to follow all conditions that we follow for method overriding like return type,type and number of parameters,exceptions in throws clause.

in your example you declared method as final,for static or non static, final methods should not be overridden.


so compilation error came.


Kiran Kumar.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
8.4.8 Inheritance, Overriding, and Hiding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In continuation to static method overriden
 
Similar Threads
Method Overriding
Static methods cannot be overridden.
fun with main() and abstract
the way objects get constructed
true or false