why can't we assign weaker privilege in subclass

后端 未结 8 1235
挽巷
挽巷 2020-12-09 10:53

I have a class which has a method whose access specifier by default is public. Now, I would like to extend this class in a subclass and I want to override this method to hav

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 11:28

    Because you alter the order of access privileges , so you got error

    correct order is:==> private to default to protected to public

    *in your case you goes default ==> private whenever you goes in wrong order ,it result is error

    Right order of access privilege is--

    `class SuperClass{
              void foo(){
        System.out.print("SuperClass");
         }
       class SubClass extends{
    
      //--default to default--//
             void foo(){
      System.out.print("SubClass");
       }
     //--default to protected--//
    
      protected void foo(){
      System.out.print("SubClass");
    
     //--default to public //
    
     public void foo(){
     System.out.print("SubClass");
     }` 
    
        **you make sure to preserve correct order while overriding **
    

提交回复
热议问题