• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems??

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
import java.io.*;
class Base{
public static void amethod()throws FileNotFoundException{}
}



public class ExcepDemo extends Base{
public static void main(String argv[]){
ExcepDemo e = new ExcepDemo();
}

public static void amethod( )throws IOException{}



private ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readChar();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}



}


static methods are not overridden then what is the problem with this code...???
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You used the code tags but forgot to put the code inside it

Are you trying to compile a class file which has a public and private class in the same java file ? A private class that is not inside another class or method ?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't override, but it hides the method Base.amethode() (just outcomment the method in the sub class). And it looks like the rules are the same for hiding as for overriding. So it isn't allowed to use wider exceptions in the subclass.
So if you swap the exceptions all is fine, like in the following code:
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why the static method is not inherited. If the same rules apply to hiding and inheriting then why not just say that the static method is inherited.

I know that it says in K&B that "a static method cannot be overridden" but if these same rules apply then aren't we overriding the method in the subclass when following the rules?
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Anderson wrote:I don't understand why the static method is not inherited. If the same rules apply to hiding and inheriting then why not just say that the static method is inherited.
I know that it says in K&B that "a static method cannot be overridden" but if these same rules apply then aren't we overriding the method in the subclass when following the rules?


Why are static methods not inherited? There is no polymorphisus, right. But AFAIK they are inherited. Remember, there is no hiding without inheritance. What should be hidden, if there is nothing to hide?
From sun (sun.com):

You can use the inherited members as is, replace them, hide them, or supplement them with new members



Try this code and you see it can access the static Base.amethod():
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small change to code sent by Bob wheeler :-


So it is clear that super class static methods are inherited. Also one more point is that if we override a static method then compiler allows us if and only if we mark it also as static. If we override a super class protected method by a sub class default method then it says 'attempting to assign weaker access privileges'. We are decreasing the ability of the method. So if we override a static method with non-static one then we are decreasing the ability of the method to be able to be called without the need of an instance. Make the sub class static method private in the code and compiler says the same thing which it says for a non-static method override. So rules are same for legal overrides related to access modifiers and exceptions but just one more thing is non-acess modifier, that is static.
But check the following :-



I tried to analyse on my own. Correct me if I am wrong.
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can use the inherited members as is, replace them, hide them, or supplement them with new members



As static methods are inherited that`s true ...but why the rules of overriding are applied ??
why is it said that that method can`t throw wider exceptions??



 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vikky.ag agrawal wrote:

You can use the inherited members as is, replace them, hide them, or supplement them with new members


As static methods are inherited that`s true ...but why the rules of overriding are applied ??
why is it said that that method can`t throw wider exceptions??


Check 8.4.8.2 and 8.4.8.3 out: Java Language Specification
Especially this:

A method declaration must not have a throws clause that conflicts (ยง8.4.6) with that of any method that it overrides or hides; otherwise, a compile-time error occurs.



cheers Bob
reply
    Bookmark Topic Watch Topic
  • New Topic