0%

JavaSE Class's Constructor & Extend

Why & What ‘extends’? The features?

As we all know, extend can make code resuable.

What is extend in dictionary? Cause to cover a wider area; make larger. Namely, it can help a thing have more features than before after doing extend. 因此,中文里说继承是不准确的,是扩展。实际extends的作用也是扩展,在父类的代码上扩展代码

In Java, what will happen after a class ‘extends’?

1
2
3
4
5
6
7
8
9
10
11
12
,----------.
|SuperClass|
|----------|
|----------|
`----------'
|
|
,--------.
|Subclass|
|--------|
|--------|
`--------'

Subclass can

  • have all variables (non-static or static variables) from Superclass; If Subclass and Superclass have the same name for a variable, there will be two copies held in their own hands.
  • have all methods from Superclass
  • have its own variables and methods
  • use keyword this to invoke its own constructors, variables and methods
  • use keyword super to invoke Superclass’s constructors , variables and methods
  • override Superclass’s methods

Subclass cannot

  • have Superclass’s constructor (because both of you are still different after all)

You can test above things by some code.

Be careful:

  • Please remember this and super cannot appear in the code block with static keyword
  • 中文里,类变量=静态变量

Some important principles here

  • Assume there is a subclass, when you use a variable ‘a’ in a method? 当你在一个方法中使用一个变量a

    • compiler will check if there is another local variable ‘a’
    • compiler will check if the current subclass has a variable ‘a’
    • compiler will check if all its direct or indirect superclass has a variable with the same name ‘a’
  • whenever subclass is initialized

    • invoke superclass’s constructor

      • implicitly invoke superclass’s zero-args constructor if superclass has such a constructor with zero args or has no defined constructors
      • explicitly invoke its own constructor by keyword this
      • explicitly invoke superclass’s constructor by keyword super
        一些结论
        • 总结1:构造函数不能继承,只是调用而已。
        • 总结2:创建有参构造函数后,系统就不再有默认无参构造函数。

      overall, anyway, each subclass will invoke superclass’s constructor once.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      Wolf.java

      class Creature{
      public Creature(){
      System.out.println("Creature无参数的构造器");
      }
      }

      class Animal extends Creature{
      public Animal(String name){
      System.out.println("Animal 带一个参数的构造器," + "该动物的name为 " + name);
      }

      public Animal(String name, int age){
      this(name);
      System.out.println("Animal 带两个参数的构造器," + "该动物的age为 " + age);
      }
      }

      public class Wolf extends Animal{
      public Wolf(){
      super("灰太狼", 3);
      System.out.println("Wolf无参数构造器");
      }

      public static void main(String[] args){
      new Wolf();
      }
      }

      $
      Creature无参数的构造器
      Animal 带一个参数的构造器,该动物的name为 灰太狼
      Animal 带两个参数的构造器,该动物的age为 3
      Wolf无参数构造器
  • a subclass’s intialization starts from the top superclass to the bottom subclass e.g. you will see it happens from Object to Class B when Class B is initialized

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
         ,------.     
    |Object|
    |------|
    |------|
    `------'
    |
    |
    ,------.
    |ClassA|
    |------|
    |------|
    `------'
    | |
    ,------. ,------.
    |ClassB| |ClassC|
    |------| |------|
    |------| |------|
    `------' `------'

Reference

疯狂Java讲义(第3版)
java中子类继承父类时是否继承构造函数