• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can static methods be inherited?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JQPlus standard test4, it is said that static methods are not inherited. Is that true? I understand that private methods are not inherited because it isn't directly accessible in subclass although it exists in subclass. But we can directly invoke statc methods of the supercalss in subclass, how can one say it is not inherited?
Confused.
Judy
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, the answer I understand from my studies is that a static method is not associated with an instance. It is associated with
the class.
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods cannot be inherited since they don't deal with an object but wiht a class. They belong to a class not to any objects. Therefore they cannot be inheritted.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables and methods are Class level. Instances of the class or the subclass share the variable or the method declared static.
The answer for your question is
static methods do not get inherited.
hope this helps
Venkat
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I found an example from "The Complet Java 2 Certification Study Guide" that will help.
"A static method my not be overriden to be non-static. The Code
below, for example, will not compile:
1. class Cattle {
2. static void foo() {}
3. }
4.
5. class Sheep extends Cattle {
6. void foo() {}
}
The compiler flags line 6 with the message, "Static methods can't be overridden." If line 6 were changed to "static void foo() { }", the compilation would succeed.
Hope that helps.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
static fields and methods are at class level. Inheritance is irrelevant. However, inheritance is just a concept, in regard to coding, you can do the following:

LHS
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and to be technically correct, private methods are inherited! whenever a subclass extends a class, it akes all the copies of the class' methods and variable. but since the private method is private, it is not visible to the subclass.
 
Hungson Le
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to prove what Anshuman said:
You can re-declare a private method, but it has nothing to do with inheritance.

A.method() is included in B (hidden) for certain operations.
When you create an instance of class B, line //// shows that this hidden method had been called.
LHS
[This message has been edited by Hungson Le (edited February 01, 2001).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the definition of inheritance? Let's put attribute aside,
my feeling is that if subclass does not have that method but superclass has it, you can call that method (if inheritated) from subclass then the method is inherited by the subclass. You can not call it from anywhere other than the super calss itself and subclasses, can you?
Inheritance should not have much/anything to do with polymorphism. It should be a concept of code reuse. Java API has inheritance hierachy; attributes and all the methods shown there are part of inheritrance hierachy. Yes, overriding a static method does not realize late-binding effect but it is concept of polymorphism not inheritance. This is just my personal opinion.
--Howard
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static method, just like static variables is inherited by the subclass. This can be demonstrated by subclass invoking superclass'es static method/variables.
Don't be confused with overriding static method. static method cannot be overriden by subclass. nonstatic method cannot be overriden to be static by subclass.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the below examples, I think private method are not inherited but static method do.
The below codes have compilation error saying no method found rather than method is not accessible.
<code>
public class m {
private void p() {
System.out.println("OK");
}
}
class n extends m {
public static void main(String args[]) {
n obj1 = new n();
obj1.p();
}
}
</code>
I can still call `java l` for the below code
<code>
public class k {
public static void main(String args[]) {
System.out.println("OK");
}
}
class l extends k {
}
</code>
Please clarify.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private methods to get inherited but you cannot access them. I really liked how it's been explained in Thinking in Java 2nd ed.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused on this now , please tell me if this is overriding a static method. In this B class i overwrite a method which is staticin class A and i am able to use it. There is no errors.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Val Dra,
Your code works but technically it's not overriding; it's hiding. There is a Sun Tech Tip that explains what happens; basically, as 'static' methods are not inherited, the compiler does not look for a matching method in the superclass.
Hope that helps.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
i read your all message but we can explain it very easily that
why should i need static and why should i need non-static method
in different class. Some answer i found that it is not related with the class. I think it is somewhat related with the class.
Look, Thread is a class and sleep() is a static method in that
class isn't it?. You might have a question. Why it is? Because
static void sleep() method is to be happened several times in
the class or subclass if it is being inherited by other class. Inherit means when you extend a class, you are able to use all features of that class in your subclass.

Thread.sleep(1000);
Static classes can be useful sometimes if you happen to have a very useful function in a class that you use a lot. Then it can be a good idea top make the function static and to be able to save some memory by not having to allocate memory for a new object. The memory that is required for a static method is allocated first when the Java VM encounters the class where the static method is. All the future objects created and based on that class will always use the same memory cell where the static method exist.
I think it should clear it out,
Golam Newaz
------------------
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So everybody,
What Is The Conclusion For This Topic. Are We Settled with any Answer??
BYe,
Tualha Khan
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I think, the static methods and variables are inherited from the super class by the subclass. See the code below.
************************************
class test45// super class
{
static int a=45;// a static variable
int b;// an instance variable
public static void doer()// a static method
{
System.out.println("STATIC In super class doer()");
}
void doers()// an instance method
{
System.out.println("NON - STATIC In super class doers()");
}

}
class test46 extends test45// sub class
{
static int a=111;// another static variable
protected static void doer()// overriding a static method
{// compiler error about accessibility
System.out.println("STATIC In sub class doer()");
}
void doers()//overriding instance method
{
System.out.println("NON - STATIC In sub class doers()");
}

public test46()
{
b=67;//another instance variable
System.out.println(a);//calling a of sub class
System.out.println(b);//calling ab of subclass
System.out.println(super.a);//calling a of super class
}
public static void main(String args[])
{
test46 ob=new test46();//object ob for class test46
test45 ob1=ob;//reference type test45 pointing at ob
ob1.doers();//calling static method doers() of //superclass
ob1.doer();//calling instance method doer() of //subclass
}
}
**********************************************
If the static members are not inherited by the sub class, then the statement in which the static method doers() is given a narrow accessibility, will compile correctly and the program will run. However the compiler flags an error, thereby specifying that the method in the super class is in someway related to the method in the sub class. It is being inherited by the sub class.
Correct me if I am wrong???
Bye,
Tualha Khan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic