This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can static methods be inherited? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can static methods be inherited?" Watch "Can static methods be inherited?" New topic
Author

Can static methods be inherited?

Judy YU
Ranch Hand

Joined: Nov 19, 2000
Posts: 30
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
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
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.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
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.


Val SCJP <BR>going for SCJD
venkat_alladi
Greenhorn

Joined: Jan 31, 2001
Posts: 12
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

Joined: Jan 29, 2001
Posts: 213
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.
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
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

Hungson Le<BR>
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
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

Joined: Jan 25, 2001
Posts: 32
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).]
Howard Hu
Greenhorn

Joined: Jan 10, 2001
Posts: 17
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
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
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.
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
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

Joined: Jan 26, 2001
Posts: 439
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

Joined: Jan 26, 2001
Posts: 439
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
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
------------------
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
So everybody,
What Is The Conclusion For This Topic. Are We Settled with any Answer??
BYe,
Tualha Khan


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can static methods be inherited?
 
Similar Threads
Main() and Constroctor inherited?
Doubt in overrididng
private method...
Static clarifications required
fun with main() and abstract