• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Static and Inheritance

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are static methods and Variables,if not private, inherited? If yes, Can they be overridden?

What do they mean conceptually ,if they are inherited?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are inherited in the sense that they are in the scope of the subclass -- i.e., a static method Parent.method() can also be called as Child.method(), or just as method() withing the Child class.

Variables can not be "overridden," so your second question applies only to methods. Static methods can't be overridden for the simple reason that they're not called polymorphically -- i.e., the compiler decides when compiling the code precisely which static method should be invoked, whereas for non-static methods, of course, that decision isn't made until the program is executed.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an interesting distinction between hiding and overriding.

When a child class has an instance method with the same name and signature as a method in its parent class, calling that method on a reference of the parent class's type that happens to point to an object of the child class's type will call the child class's method. This is called overriding and is an example of polymorphism.

When a child class has an instance variable with the same name as a variable in its parent class, accessing that variable using a reference of the parent class's type that happens to point to an object of the child class's type will access the parent class's variable. Accessing the same variable using a reference of the child class's type will access the child class's variable. This is called hiding.

If static variables and methods of a child class match variables and methods of the parent class, the member selected will be based on the class name used to access that variable or method. Java can't use the actual type of the object because static class members are not part of individual objects. (OK, they are part of the class object, but that's another story.) So all static class members use hiding instead of overriding.
[ October 05, 2004: Message edited by: Mike Gershman ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dhandapani Venkataraman:
Are static methods and Variables,if not private, inherited?...



The private-ness is not an issue here. Inheriting and overriding (or hiding) static methods or variables is the same whether they are public, package private, protected, or private.

Layne
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic