Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Doubt

 
maredu ramesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi All,

In above code how subclass Minor can use static variable y,variable y belongs to super class Uber.How subclass can inherit it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by maredu ramesh:
...how subclass Minor can use static variable y,variable y belongs to super class Uber.How subclass can inherit it.

Welcome to JavaRanch!

The keyword "static" does not prevent inheritance.
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI. Static Variable values are avialable to all the instances of its class and its subclass.So Y is available to subclass constructor. And finally Output is 9.
 
maredu ramesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi,In above code subclass Dog is able to inherit variable testInt, but it is not able to inherit method doStuff().When i try to compile above code i am getting error at doStuff() method call.Can you please explain what is difference between variable and method here.
 
Hari Krishna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

doStuff() method is inherited. You tried to invoke dostuff() method, not doStuff() method. Java is Case sensitive...

Regards,
Hari Krishna
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, remember that although static methods are inherited, they cannot be overridden. (See Overriding vs. Hiding.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic