+ 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"); } }
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.
+ 3
thank u DAB
+ 1
can you help me DAB with another code?