Overridden private method is causing exception in accessing subclass public method in Java -


the below program giving compilation error in line "obj.method()" inside main method. error "the method method() type superclass not visible". understanding should able access public method of subclass. can explain concept behind it?

class superclass{  private void method(){     system.out.println("inside superclass method"); }  }  public class myclass extends superclass{       public void method(){         system.out.println("inside subclass method");     }      public static void main(string s[]){          superclass obj = new myclass();         obj.method();      } } 

from understanding should able access public method of subclass.

yes, when compile-time type of expression you're calling on subclass.

so if change code to:

myclass obj = new myclass(); 

then should fine. currently, compile-time type of obj superclass, doesn't have public method method.

also note myclass.method not override superclass.method. call method() within superclass call superclass.method() if actual type of object myclass.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -