Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance question  RSS feed

 
Simon Shi
Greenhorn
Posts: 11
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the Oracle website of 8 practice questions:
3. Given:

What is the result?
A) Writing...
B) Writing book
C) Writing code
D) Compilation fails.

I chose C. Writing code but the correct answer is A. I am not sure why. I thought when w is instantiated, a new programmer is created. I know that w is of class Writer but if methods are called, the Programmer class should be called. Can you clear up the confusion.

-Simon
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you will notice then you will find write() is static and the static methods have doesn't to do with polymorphism(they are not polymorphic,known at run time).
Why so? because compiler just needs a class for it to invoke which it infers through the type of the reference variable during the compile time.when the compiler infers the type it checks for the method in the same type class and binds it with the code just like as it does with the compile time constants.
in fact static methods cannot be overriden and if you are declaring the same method  with the same signature in a subclass then it will hide the inherited one,this is called hiding(not overriding).

Writer w = new Programmer();
w.write()here the compiler will infer the class from the type of w(here it is not like let me wait till run time so that i will know what w is actually during running,indeed compiler will place a code accordingly whatever the class of w is.)

try this,
Writer w = null;
w.write();
think about the outcome
 
Simon Shi
Greenhorn
Posts: 11
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:
try this,
Writer w = null;
w.write();
think about the outcome


Ok! Thanks a lot. I understand. With this example, the output should be Writing...
because w is from the Writer class which has that method outputting Writing...
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon wrote:the output should be Writing...

correct,nicely caught!
 
B Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it ok to create an object of an abstract class ? Here, Writer is an abstract class. so Writer w = null; is it correct ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7190
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, when you do Writer w = null; you don't actually create an instance of Writer, because null is not an object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!