aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problems?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problems??" Watch "Problems??" New topic
Author

Problems??

vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
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...???


scjp 6
http://algorithmsea.blogspot.com
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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 ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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:


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Ryan Anderson
Greenhorn

Joined: May 18, 2009
Posts: 21
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

Joined: Apr 24, 2009
Posts: 317
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():
Samrat Kakustam
Ranch Hand

Joined: May 21, 2009
Posts: 56
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.


SCJP 5.0
vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
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

Joined: Apr 24, 2009
Posts: 317
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems??