The following notes are based on video series: 手把手教你用Java基础教程 - 北京尚学堂 - 马士兵
I. Access control keywords
keyword | within class | within package | son class | anywhere |
---|---|---|---|---|
private |
✅ | |||
default |
✅ | ✅ | ||
protected |
✅ | ✅ | ✅ | |
public |
✅ | ✅ | ✅ | ✅ |
- For class, only
public
anddefault
are used. default
members can be accessed by a class that belongs to the same package.
II. Use extends
to inherit
- A son class inherits all members of its superclass (varibales and methods).
- Java only allows single inheritage. A son can have only one father(superclass).
1 | <modifier> class <name> [extends <superclass>]{...} |
III. Overwite (override)
A method of a son class can override the original method in its superclass.
To realize, the followings needs to be satisfied:
same name.
same return type.
same parameters list.
can not use stricter access control.
IV. Super
- In a son class, use
super.<name>
to refer superclass members.
V. Constructive method in inheritage
- Son constructive method must first use superclass constructive method.
- If no
super
used, the son’s constructive method first executes the superclass’s non-parametrized constructive method, namelysuper();
If there exists nosuper()
, compile error. - The alternate is to use
super(p1,p2)
in the first line to call specific superclass’s constructive method.