Search code examples
javaobjectjakarta-eeinstantiationsuperclass

Difference between two types of a super class instantiation in Java


I have an abstract super class called Document and two child classes called Magazine and Book. What is the difference between :

Document book = new Book(); and Book book = new Book();

Thank you in advance.


Solution

  • The difference is for example if you have any information about document only in the document class, like, say, price. By doing

    Book book = new Book();
    

    you won't be able to get the price of the book, so doing

    Document book = new Book();
    

    will give you additional information about your object. Note that price is not the best example. I only wanted to show you how the super-/subclass work.