B which is subclass of class A often carries more features besides those extends from A, in other words,A is subset of B. = is an assignment symbol which requires the right side at least owns the whole features of the class on the left side. Therefore,A a = new B() is correct whereas B b = new A() will fail to pass the compilation. More vivid example, let us suppose that you need a book, your friend offer you a sheet of paper.Obviously your requirement cannot be catered,although a sheet of paper is a part of a book.But if your friend offer you a book and a sheet of paper.S/he can cater you. Perhaps it's not a suitable example, but it can make you easy to understand this question.
B a=B(new A()) 可以这么写 ,这样只能调用父类有的方法 ,子类中自己的方法是不能被调用的。
= is an assignment symbol which requires the right side at least owns the whole features of the class on the left side.
Therefore,A a = new B() is correct whereas B b = new A() will fail to pass the compilation.
More vivid example, let us suppose that you need a book, your friend offer you a sheet of paper.Obviously your requirement cannot be catered,although a sheet of paper is a part of a book.But if your friend offer you a book and a sheet of paper.S/he can cater you. Perhaps it's not a suitable example, but it can make you easy to understand this question.
可以这么写 ,这样只能调用父类有的方法 ,子类中自己的方法是不能被调用的。