简体   繁体   中英

why we can't implement methods from interface to abstract class, without modifying “public”?

interface:

interface MasterPlan{
    void getRate();
}

abstract class:

abstract class Plan implements MasterPlan{
    abstract void getRate();
} }

At this case, I can't implement MasterPlan interface without using public modifier in getRate() method.

public abstract void getRate();

this is working with normal behavior.

I know default modifier is default in interfaces. what is the reason for this error without public modifier in implemented abstract class?

You're facing the thing that every method in a Interface is public . So if you got an abstract class which implements the Interface the methods that are in there are public . You can't change the visibility of a defined method.

All abstract, default, and static methods in an interface are implicitly public.

interface have methods having implicit properties and once the method is declared after that when you define it or implement it you can't change the access modifier of it.

or else you can declare your class as private,protected etc.

oracle document defining interfaces

you can ask more eager to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM