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)
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
This works if A
is a subclass of B
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
.
This could work in two scenarios:
A
is a B
, i.e. inheritance or interface implementation, orA
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