• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion in static

 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its given that static members cannot be inherited but the static methods can be redefined in the sub class.
for the above program the output is :1
in the above program the getVal() is not redefined in the Flidder class ... then how come the f.getVal() is getting executed

and i have doubt with the static variable too


the output is:9
how are we able to access the y in sub class when its not inherited(Minor() { super(y); y = y + 3; })

 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Static members are inherited by subclasses. Only thing is static methods cant be overridden but they can be redefined. (I guess redefining is like overridding only just the wording is different. THis is my assumption.)

Refer to JLS for more information.

Hope this helps!
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Static members are inheritable.
so you can access the static methods and static variables of the Super class until and unless they are private.


Regards,
Kumara Swamy.
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks srilatha and swamy for the information
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are inheritable???
had always thought static means no inheritance......class related
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods are inherited but they cannot be overridden.

Thanks,
Geeta Vemula
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the access level of the superclass members both static & instance, all of them are inherited to the subclasses. But only, instance methods can be overridden if they are not final.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to add, you cannot hide a final static method from a super class...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember everyone - please cite the author of whatever mock exam questions you discuss!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic