Passing different type than parameter type

前端 未结 4 379
庸人自扰
庸人自扰 2021-01-07 08:50

If I would have a variable a declared by A a and a method m with void m(B b). Is there any way that calling m(a)

相关标签:
4条回答
  • 2021-01-07 09:09

    This can be possible when A is a subclass of B or we can say A extends B, this is known as inheritance in programming language, in this A will be a child of the class B and inherit all its properties

    you can check it from the link below

    https://www.tutorialspoint.com/java/java_inheritance.htm

    0 讨论(0)
  • 2021-01-07 09:11

    This works if A is a subclass of B

    0 讨论(0)
  • 2021-01-07 09:20

    If A extends B, it can be passed as argument to that function. If not and A contains values suitable for B you have to create an instance of B and fill it with the required values of the instance of A.

    0 讨论(0)
  • 2021-01-07 09:29

    This could work in two scenarios:

    • When A is a B, i.e. inheritance or interface implementation, or
    • When A and B are primitive data types, and an implicit conversion exists from A to `B.

    Here is an example:

    void m(long b) {
        ...
    }
    int a = 123;
    m(a); // This compiles and runs correctly
    
    0 讨论(0)
提交回复
热议问题