File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables and methods..

 
geet kaur
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

my question is would x and method in class A be inherited by B?
 
ankit kumar
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thing id that your code will not compile because of no return type.

and the second thing is that static method and variable will not be inherited but that will be overhide.
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you compiled this example ?
Methods should have return type.

I think static methods can be inherited. Please check the link given below .

http://www.coderanch.com/t/411599/java/java/static-constructor
[ September 25, 2008: Message edited by: bittoo garg ]
 
ankit kumar
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sun page
 
Paul Somnath
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can static variables be inherited?
Think of it - Static variables are class variables. They belong to class and they have exactly one copy. Each instance created shares that copy. So if it was inherited then this rule would be violated isn't it?
As far as inheriting static methods are concerned, yes they are inherited, but they wont perform the way you think when you try polymorphism. Actually they are hidden.
To learn more, goto SCJP FAQ, and there is a very nice article on this.
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above link .. nothing has been written about inheritence with regard to static methods . This link just explain the difference between overriding and overhiding.



output is "Hi I am static method in parent class"
So I think static methods can be inherited.
 
V. Potluri
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

static members can partcipate in inheritance. But static method cannot be ovverriden. But the rules for overriding a method and hiding a method is same.

Please let me know if my understanding is wrong?

Have a nice day ahead!

Regards,
Gopal
 
Paul Somnath
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Please loot at SCJP FAQ.
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

The link you gave again describe the difference between overriding and overhiding.

The example which I posted in my last post is am example of inheritence.
where subclass object is invoking display function of parent class which is static.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)

But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:

 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)


I am not talking about overriding.
overriding involves two classes parent and child class .
In my example in my post display method is a static method in parent class only. There is no display method in child class .So no question arises of overriding or overhiding.



But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:


code:
--------------------------------------------------------------------------------

class parent{public static void display(){System.out.println("Hi I am static method in parent class");}}public class subclass extends parent{public void display (){System.out.println ("In subclass");}public static void main(String args[]){subclass s = new subclass();s.display();}}

--------------------------------------------------------------------------------



Yes, You are very true. But this is not my point.

I am just opposing the point that

the second thing is that static method and variable will not be inherited but that will be overhide


I am again posting my example which is not talking about overriding.

Question here is .. can static method be inherited?
I am saying yes. Avobe example clearly shows this.
 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a devil of a time with this one. I have not found Chapter and Verse quoted anywhere in Java documentation that flat out says that static methods are not inherited. What gives me grief proving it is that there are two ways to call a static method a) by class name:

( ClassName.statMethod() ),

or b) using a reference to an object

( objReference.statMethod() ).

Both are legal but the second makes it look like the subclass object inherited the method and it is being called via inheritance.

I'm still looking....
[ September 25, 2008: Message edited by: Bob Ruth ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Bob has been very clear in putting out this point...
"....but the second makes it look like the subclass object inherited the method and it is being called via inheritance"

So, in Bittoo's original code, the function display() is called using the subclass object, which is very similar to calling the function as parent.display () -- which means its NOT inherited. Its just calling the static method, this time, using a reference which IS-A parent (as opposed to calling the static method using its class)
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

See this code also.

 
Rudolf Meerkotter
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My contribution:


[ September 25, 2008: Message edited by: Rudolf Meerkotter ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic