Why do I need another constructor in an extended abstract class?

Miro Kropacek

I've come across this issue and I'm wondering what is the difference here:

abstract class Abstract {
    Abstract() {
        System.out.println("Abstract.Abstract()");
    }

    Abstract(String s) {
        System.out.println("Abstract.Abstract(String)");
    }

    void test() {
        System.out.println("Abstract.test()");
    }

    void test(String s) {
        System.out.println("Abstract.test(s)");
    }
}

abstract class Base extends Abstract {
}

class Sub extends Base {
    Sub(String s) {
        super(s);   // undefined constructor
    }

    void subTest(String s) {
        super.test(s);  // why is this all right then?
    }
}

Why do I have to define Base(String s) constructor to make it compilable but the super.test(s) call is fine without defining anything?

Idos

Java provides you with default constructors in any class (even if you don't define one yourself), meaning you have Base() default constructor in class Base, but not any other constructor (one that takes arguments, such as Base(String s)) because constructors are not inherited .

In addition, extending a class gives you its methods via inheritance, so calling super.test(s) is legal here because Base has the method test(String s) inherited from Abstract.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Why do we need a user-provided constructor for a const object?

来自分类Dev

Why do I need to update uvs?

来自分类Dev

Why do I need depthBuffer to use RenderTexture?

来自分类Dev

Abstract class inheriting a generic type requiring a constructor

来自分类Dev

Why do i need the gil for PyMem_Malloc()?

来自分类Dev

How do I use an extension function in another class? C#

来自分类Dev

Do I need STUN?

来自分类Dev

Why do I need to use the unit type in F# if it supports the void type?

来自分类Dev

Can I make virtual abstract class throw an exception

来自分类Dev

Why isn't my class constructor initializing its member variables?

来自分类Dev

Do I need to free popup menus in GTK?

来自分类Dev

Which keys do I need for CNContactFormatter?

来自分类Dev

Android abstract parcelable class

来自分类Dev

Abstract class extends concrete class

来自分类Dev

How can I test a final class with private constructor?

来自分类Dev

What do I need to do to use tgmath on iOS?

来自分类Dev

Why do Polymer's computed properties need explicit property arguments?

来自分类Dev

Meteor - Why do we need packages for single-file libraries?

来自分类Dev

Abstract data type constructor can be accidentally bypassed?

来自分类Dev

Why I can instantiate a static inner class?

来自分类Dev

casting to an abstract generic base class

来自分类Dev

Do I need to cleanup my event handlers when DOM changes?

来自分类Dev

What regex do I need for Java String.split for this case

来自分类Dev

Do I need to commit a code if doing an insert before an update?

来自分类Dev

Do I need rel="nofollow" in external scripts and stylesheets?

来自分类Dev

Do I need to sudo when running pip/easy_install?

来自分类Dev

Atomic Operation Thread Safety - Do I Need a "Mirror" Atomic Read?

来自分类Dev

Do I need to call delete before reassigning a pointer?

来自分类Dev

OpenSSL for making iOS certs on a windows machine - do I need a server?

Related 相关文章

  1. 1

    Why do we need a user-provided constructor for a const object?

  2. 2

    Why do I need to update uvs?

  3. 3

    Why do I need depthBuffer to use RenderTexture?

  4. 4

    Abstract class inheriting a generic type requiring a constructor

  5. 5

    Why do i need the gil for PyMem_Malloc()?

  6. 6

    How do I use an extension function in another class? C#

  7. 7

    Do I need STUN?

  8. 8

    Why do I need to use the unit type in F# if it supports the void type?

  9. 9

    Can I make virtual abstract class throw an exception

  10. 10

    Why isn't my class constructor initializing its member variables?

  11. 11

    Do I need to free popup menus in GTK?

  12. 12

    Which keys do I need for CNContactFormatter?

  13. 13

    Android abstract parcelable class

  14. 14

    Abstract class extends concrete class

  15. 15

    How can I test a final class with private constructor?

  16. 16

    What do I need to do to use tgmath on iOS?

  17. 17

    Why do Polymer's computed properties need explicit property arguments?

  18. 18

    Meteor - Why do we need packages for single-file libraries?

  19. 19

    Abstract data type constructor can be accidentally bypassed?

  20. 20

    Why I can instantiate a static inner class?

  21. 21

    casting to an abstract generic base class

  22. 22

    Do I need to cleanup my event handlers when DOM changes?

  23. 23

    What regex do I need for Java String.split for this case

  24. 24

    Do I need to commit a code if doing an insert before an update?

  25. 25

    Do I need rel="nofollow" in external scripts and stylesheets?

  26. 26

    Do I need to sudo when running pip/easy_install?

  27. 27

    Atomic Operation Thread Safety - Do I Need a "Mirror" Atomic Read?

  28. 28

    Do I need to call delete before reassigning a pointer?

  29. 29

    OpenSSL for making iOS certs on a windows machine - do I need a server?

热门标签

归档