java - Why i am getting here method overriding error? -
this superclass:
class superclass { int a=89; final static void m( int p){ system.out.println("inside superclass"); } static void n(){ system.out.print("superclass"); } }
this subclass::
class subclass extends superclass { int a=90; static void m( int p){ system.out.println("inside subclass"); } static void n(){ system.out.print("subclass"); } }
main class:
class main { public static void main(string[] args) { subclass.m(89); new subclass().n(); } }
the problem cannot understand why javac giving me overriding error in static method..an p.s plzz elaborate rules overriding valid hiding. like
the new method definition must have same method signature (i.e., method name , parameters) , same return type. whether parameters in overriding method should final @ discretion of subclass method's signature not encompass final modifier of parameters, types , order.
the new
method definition cannot narrow accessibility of method, can widen new method definition can specify or none, or subset of exception classes (including subclasses) specified in throws clause of overridden method in superclass
my error is: run:
exception in thread "main" java.lang.runtimeexception: uncompilable source code - m(int) in javaapplication3.subclass cannot override m(int) in javaapplication3.superclass overridden method static,final @ javaapplication3.subclass.m(javaapplication3.java:18) @ javaapplication3.min.main(javaapplication3.java:25) java result: 1
also want ask if static members called classname resolve whicch version of method executed when method hidden subclass extending superclass if make anonymous object , call method how compiler determines version of method should called. in above code in main class type this: new subclass().n();
how compiler know subclass version of method should called if not providing type of reference variable
from jls 8.4.3.3:
a method can declared final prevent subclasses overriding or hiding it.
static methods same signature parent class hidden when called instance of subclass. however, can't override/hide final methods. keyword final
disable method being hidden. cannot hidden , attempt result in compiler error. there nice explanation on over javaranch.
Comments
Post a Comment