This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

little help required in "statics"  RSS feed

 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code "k" is a static variable of Super class.
It should not be visible in the Sub class.
Atleast not when I write "this.k" And even when I write "m.print()" , i should get a compiler error... But i do not get any error...
Why so?? Please give me a good example saying that it is not inherited.
Waiting for an early reply...

class Super
{
int i =1;
static int k = 2;
public static void print()
{
System.out.println(k);
}
}

class Sub extends Super
{
public void show()
{
System.out.println(" k : " + this.k);
}
public static void main(String []args)
{
Sub m =new Sub();
m.show();
m.print();
}
}
 
Krishnan Loganathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you don't want to access the variable k in the sub class means, declare the variable as private (as of now, it is default). Also, if you don't want to access the method means, mark the method as private. If you make those changes means, it will throw compile time error, as you expected.

Regards,
Loga
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!

As to my knowledge static method can be inherited but they cannot be overridden......so how can you expect a compiler error,you will get a compiler error if you try to override the static methods.......
 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Dhwani, thats not the case..
If they are inherited why cant they be overriden??
The main reason why static methods cannot be overridden are that they are not inherited in sub class...
And the reason why they are not inherited is that they are class's methods and not of object..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement "statics are not inherited," which some people are quite fond of saying to make themselves sound mysterious and important, depends on the definition of the word "inherited" that you use. According to the informal definition of "inherited" which most people use, they are inherited. If by "inherited" you mean they can be called from inside a subclass without qualification, or called referencing an instance of a subclass, or called using the name of the subclass, then (as you see in your code example) they are inherited.

Now, it is true that static methods are not polymorphic (they "can't be overridden.") This really has nothing to do with whether statics are inherited. They're not polymorphic because no object is used in looking up their implementation at runtime. The compiler figures out exactly what method to call while the program is being compiled, and there's no chance to choose at runtime, which is what polymorphism is.

Now can we please stop having this discussion?
 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr. Ernest Friedman-Hill.
I wasn' t getting a satisfactory answer immediately and so I put the same question on another forum.
Anyways I assure you that this won�t happen again from my end.


Regards,
Pitambari.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!