Answer the question
In order to leave comments, you need to log in
Is it possible in Java to make an object of one class an object of another?
There is a class:
Class C1 {
public C1 () {
// Код конструктора
}
}
Class C2 extends C1{
}
public void myMethod (C1 c1object) {
// Код метода
}
myMethod(new C2());
Answer the question
In order to leave comments, you need to log in
There is no error, everything works.
https://repl.it/@maksimmosieichuk/PromotedMulticol...
No. You can get a reference to an object of type C1 from a reference to C2. But at the same time, the object will still behave like C2, which it is. This is called polymorphism.
can. there will be no error. if you have a bug, then it's something else, not inheritance.
The right question already contains the answer. What exception does the compiler throw?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question