What is the reason of error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the reason of error?

public class TestInheritanceError { public static void main(String[] args) { B b=new C(); b.x();// we are creating object of class C(child class) by using keyword 'new' and storing it in the reference of class B(parent class) C c=new B(); c.x(); } } class A{ public void x(){ System.out.println("x of A"); } } class B extends A{ public void x(){ System.out.println("x of B"); } } class C extends B{ public void x(){ System.out.println("x of C"); } }

10th Jul 2018, 3:10 AM
Tarika
Tarika - avatar
3 Answers
+ 8
That's because you can't invoke the base class constructor on a derived class object. Edit: Here's the explanation: All integers are numbers, but not all numbers are integers. So, Integer __int__ = new Number(); might result in storing a non-integer value in an integer reference, which is not allowed.
10th Jul 2018, 3:24 AM
DAB
DAB - avatar
+ 3
thank u DAB
10th Jul 2018, 12:24 PM
Tarika
Tarika - avatar
+ 1
can you help me DAB with another code?
11th Jul 2018, 4:21 PM
Tarika
Tarika - avatar