I'm trying to summarize the parts that are not well understood in the basics of Java for the acquisition of Java Silver.
I think I understand the concept of inheritance itself. Personally, I think it's easier to understand if you express it as extending the function rather than inheriting it. The image of a subclass is that the superclass has a higher degree of abstraction and fewer functions, and more specific functions have been added to make it a superclass + α (extension).
・ Super class
SuperA.java
class SuperA {}
-Subclass that inherits the superclass
SubA.java
class SubA extends SuperA {}
--Multiple superclasses cannot be specified after extends.
SubA.java
class SubA extends SuperA, SuperB {}
--Private members defined in the superclass cannot be used in the subclass.
Members defined in the superclass can be used in the subclass, but private members can only be used from the same class, so they cannot be used in the subclass.
--When overriding a method, the access modifier must be within the public range specified in the superclass.
For example, if you want to override a method that specifies public in a superclass, you must specify public. (The disclosure range cannot be narrowed.)
-Public range of access modifiers
wide | ─ | ─ | narrow |
---|---|---|---|
public | protected | Default | private |
Accessable from any class | Subclass or same package | Same package | Same class only |
--Members with the final qualifier cannot be overridden.
There are two types of type conversion in superclasses and subclasses:
--Implicit type conversion Subclass → Superclass Implementation class → Interface
--Type conversion by cast Subclass ← Superclass Implementation class ← Interface
In other words, is it an image that implicit type conversion can be done if the functions that can be used after conversion are not reduced?
--Members called when overriding
When a subclass object is assigned to a superclass type variable, the members of the superclass are called except for the instance method.
Recommended Posts