• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

In continuation to static method overriden

 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2023
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic