Which methods can a subclass inherit in Java?

后端 未结 6 1541
说谎
说谎 2021-02-09 19:12

Sorry I am a newbie to Java. I am trying to get my head around inheritance and subclass/superclass relationships in Java.

If classA is a subclass of classB, will classA\

6条回答
  •  野的像风
    2021-02-09 19:43

    Public methods, labeled by public are available to every class. Protected methods, labeled by protected are available to subclasses and friendly classes, which are classes in the same package. Friendly methods, labeled by nothing (i.e. default) are available to friendly classes. Private methods are available only to the class itself.

    Static methods, labeled by static are available to without an object to access them. These are called by ClassName.foo(), SuperClassName.foo() (unnecessary if not overriden, foo() is acceptable), or ClassName.foo() (unnecessary, foo() is acceptable) Dynamic (check vocab???) labeled by nothing (i.e. default) are available only if one has an object of the class. ex: bar.foo(), this.foo() (unnecessary, foo() is acceptable), or super.foo() (unnecessary if not overridden, foo() is acceptable) Overriden methods, annotated by @Override are only available through super.foo

    So in an instance of ClassB, all protected and public members and methods of ClassA will be accessible. In a static call, only static methods are accessible.

提交回复
热议问题