*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes does static methods inherit ? what about private ? 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 "does static methods inherit ? what about private ?" Watch "does static methods inherit ? what about private ?" New topic
Author

does static methods inherit ? what about private ?

Enge Chall
Ranch Hand

Joined: Nov 22, 2005
Posts: 30
These are very trivial doubts.. hope it fits into this forum :roll: !!

1. Does a static method gets inherited to it's sub classes ?

2. Does the private method gets inherited to the sub classes ?

3. If the private method is not overridden in the sub class, can we
invoke the private method of the Base class thru sub class object ?
ie sub.supA(); // is it allowed ?
Thanks


Not everything that counts can be counted, and not everything that can be counted counts.
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
1. Does a static method gets inherited to it's sub classes ?

Yes

2. Does the private method gets inherited to the sub classes ?

No. Private methods are private to the classes in which they are defined. They are not accessible anywhere outside.


3. If the private method is not overridden in the sub class, can we
invoke the private method of the Base class thru sub class object ?
ie sub.supA(); // is it allowed ?


No. Private methods are not inherited by a sub class. Hence, they cannot be overridden neither can they can be accessed using an object of the subclass. Infact, they cannot be accessed outside the class in which they are defined even by using the object of the corresponding class



Arvind
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

1. Does a static method gets inherited to it's sub classes ?

Yes


No, static method doesn't get inherited to it's sub classes.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Most people learn best by doing, so if you have a question like this try writing some code that tests out your question.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Static method can be inherited
example below


public class Test1
{
public static void testMethod()
{
System.out.println("Testing");
}
}

public class Test2 extends Test1
{
public static void main(String[] args)
{
Test2 test2 = new Test2();
test2.testMethod();
}
}

>java Test2
Testing


SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
Originally posted by rathi ji:


No, static method doesn't get inherited to it's sub classes.



Static(Non private) methods do get inherited. You can always access the static method of the parent class using the sub class name.



Arvind
Enge Chall
Ranch Hand

Joined: Nov 22, 2005
Posts: 30
Originally posted by Arvind Sampath:
2. Does the private method gets inherited to the sub classes ?

No. Private methods are private to the classes in which they are defined. They are not accessible anywhere outside.


3. If the private method is not overridden in the sub class, can we
invoke the private method of the Base class thru sub class object ?
ie sub.supA(); // is it allowed ?


No. Private methods are not inherited by a sub class. Hence, they cannot be overridden neither can they can be accessed using an object of the subclass. Infact, they cannot be accessed outside the class in which they are defined even by using the object of the corresponding class



Arvind[/QB]


If the private methods are not inherited to the subclasses, why does the concept of overriding rules comes into picture like "we can broaden the method scope in the sub classes"

ie if "private" in super class, we can make it "public" in the sub classes ? In fact we are not overriding the method, right ?
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
If the private methods are not inherited to the subclasses, why does the concept of overriding rules comes into picture like "we can broaden the method scope in the sub classes"

ie if "private" in super class, we can make it "public" in the sub classes ? In fact we are not overriding the method, right ?


Yeah, You can always define a new method with the same signature as the method in the super class. But then if the method in the super class is private, you are not overriding it because private methods are not inherited by the subclass. Hence, you cannot access the subclass method using a super class reference(of course pointing to a subclass object). (Now pls dont tell me that you can still invoke the subclass method by explict casting )
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
All, I've asked this question before but got no reply.
Succinctly, the difference between static and non static methods seems to be that static methods do not participate in dynamic method dispatch. Also, static methods cannot be overridden to a lesser level of visibility.
I'm using KB and Java Complete Reference and couldn't find a good description of the differences between static and non static members.
Can anyone throw some light on the subject?
HIGH(Hope I Get Help),
Sashi
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
static methods cannot be overridden to a lesser level of visibility


For that matter, even non-static methods cannot be overridden to a lesser visibilty level
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225

A method that is declared static is called a class method. A class method is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compile-time error. It is a compile-time error for a static method to be declared abstract.

A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.



I think Jeff's example is not a good one :


Static method can be inherited
example below


public class Test1
{
public static void testMethod()
{
System.out.println("Testing");
}
}

public class Test2 extends Test1
{
public static void main(String[] args)
{
Test2 test2 = new Test2();
test2.testMethod();
}
}


because test2.testMethod() doesn't make sense.
It should be : Test2.testMethod()


Regards,<br />Seb<br /> <br />SCJP 1.4
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Well, given Aravind's assertion, it seems to me that the only difference between static and non-static methods (as related to inheritance) is that static methods cannot participate in polymorphism.
Am I right?
Sashi
Ravisekhar Kovuru
Greenhorn

Joined: Sep 23, 2003
Posts: 25
Originally posted by Sasikanth Malladi:
is that static methods cannot participate in polymorphism.
Am I right?
Sashi


Yes you are right. See the below code:


class Super {
public static void testMe() {
System.out.println("I am in Super");
}
}

class Sub extends Super {
public static void testMe() {
System.out.println("I am in Sub");
}
}

public class Test{
public static void main(String args[]) {
Super sup = new Sub();
sup.testMe();
}
}

The output is I am in Super
This shows that static methods do not participate in dynamic method dispatch.
If testMe() was an instance method, the output would have been I am in Sub.
In fact it is not a good practice to invoke static methods on the instance of a Class. They are meant to be invoked on the Class itself.
So the bottom line is:
Do not invoke static methods on instances, especially if you have same static method signature in the Sub class

Hope this helps....
-Ravi

[ December 13, 2005: Message edited by: Ravisekhar Kovuru ]
[ December 13, 2005: Message edited by: Ravisekhar Kovuru ]
Rujal Duale
Greenhorn

Joined: Dec 13, 2005
Posts: 5
Originally by Jeff:
--------------------------------------------------
public class Test1
{
public static void testMethod()
{
System.out.println("Testing");
}
}

public class Test2 extends Test1
{
public static void main(String[] args)
{
Test2 test2 = new Test2();
test2.testMethod();
}
}
------------------------------------------------
I think the concept of inheriting static method is well
demonstrated by Jeff above. However, since the static method
testMethod() is already inherited we didn't need to create object in order to access it (testMethod() -- it is already part of the sub-class), we could simply call testMethod() and it would work. The bottom line is that the static method testMethod() is inherited by the sub-class and it is available for use.
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
Static Methods can be inherited but can't be overwridden
private methods can't be inherited so, the question of overwritting is not comming.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Static methods cannot be inherited. They may look like they are inherited because they are available from the name of the class, but they are not inherited. Static methods cannot be overriden because they are not inherited, though they can be hidden, which at a trivial level looks like overriding but is not. If a class is a blueprint or design, then a static method is a method associated with the design plans not anything made from those designs. Whenever you make something from the design you can still refer to those static items on the design but they belong to the one and only original design, not any one of the items created from the design. Thus when you attempt to override a static method you are just hiding it and the original design will still reflect the original object.


Corey McClone comments on this in his tipline here

http://radio.javaranch.com/corey/2004/03/19/1079739453000.html

It is expressed in JLS3 at
8.4.8.2 Hiding (by Class Methods)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Marcus]: Static methods cannot be inherited. They may look like they are inherited because they are available from the name of the class, but they are not inherited.

I agree with the rest of your post, but not this part. I suppose this may depend on what you think "inherited" means. Unless someone wants to put forward al an alternate definition, I'll just use the two versions put forward in the JLS. From 6.4.3: "Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden". And from 8.4.8: "A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class." Nothing in that implies that a static method or field is not inherited. Unless of course it's overridden or hidden - but here again, as far as the concept of "inheritance" is concerned, overridden and hidden have the exact same effect.

In other words, static methods and fields are inherited. (Unless they're hidden.)

Another commentary on overridden vs. hidden (but ignoring the question of what "inherited" means) can be found here.


"I'm not back." - Bill Harding, Twister
tim bond
Greenhorn

Joined: Nov 21, 2005
Posts: 18
eb Mathe
ranch hand
Member # 107996
posted December 13, 2005 07:01 AM
--------------------------------------------------------------------------------
you need to pass the scjp again or improve yourself to know that the static method can be called by both the class name and instance name .



I think Jeff's example is not a good one :



Static method can be inherited
example below


public class Test1
{
public static void testMethod()
{
System.out.println("Testing");
}
}

public class Test2 extends Test1
{
public static void main(String[] args)
{
Test2 test2 = new Test2();
test2.testMethod();
}
}

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



because test2.testMethod() doesn't make sense.
It should be : Test2.testMethod()



so both Test2.testmethod(); class calling static method;
and test2.testmethod(); instance calling static method;


boss
do it and see.
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Here is one example please go through this.

base.java
Class InheritStaticBase{
public static print(){
System.out.println("I am in base class");
}
}

child.java
Class InheritStaticChild extends InheritStaticBase{

}

Class Main{
public static void main(String args[]){
InheritStaticChild.print();
}
}

Expected Result:
I am in a base class.

This shows static methods are inherited. But they should be dealt in a static way.


Raghusham Sankargal
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
raghu sham:
This shows static methods are inherited. But they should be dealt in a static way.

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

Seb Mathe:
because test2.testMethod() doesn't make sense.
It should be : Test2.testMethod()

--------------------------
What do you mean should be dealt in a static way?
Calling object.staticmethod() and class.staticmethod() are both legal and fine.
They both do the same thing.

I dont agree the usage test2.testMethod() is wrong.
Kicky San
Greenhorn

Joined: Aug 24, 2005
Posts: 18
From this thread and by the following example, I am jotting down my observations. Please correct me if i am wrong.

1. static methods can be inherited
2. static methods can be called both by the class name and the class instance
3. static methods cant be overridden. static means one in a class.
4. Even if there is a static method in the child class same as the one in the parent class, it is not the overriden method. It is a seperate method of the child class.

Hope this example will clear all these points.

I have modified the previous example. Here is the modified version

class InheritStaticBase
{
public static void print()
{
System.out.println("I am in base class");
}
}

class InheritStaticChild extends InheritStaticBase
{
public static void print()
{
System.out.println("I am in child class");
}
}

class Main
{
public static void main(String args[])
{
System.out.println("using parent class name");
InheritStaticBase.print();
System.out.println("using child class name");
InheritStaticChild.print();
System.out.println("befre instantiating child class");
InheritStaticChild a = new InheritStaticChild();
a.print();
System.out.println("after instantiating child class");
System.out.println("befre instantiating parent class");
InheritStaticBase b = new InheritStaticBase();
b.print();
System.out.println("befre instantiating parent class");
}
}


Cheers,<br />KicKy
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Kicky San:
1. static methods can be inherited
>> Right
2. static methods can be called both by the class name and the class instance
>> Right again
3. static methods cant be overridden. static means one in a class.
>> Right again ..
4. Even if there is a static method in the child class same as the one in the parent class, it is not the overriden method. It is a seperate method of the child class.
>> Static methods cant be overidden. They can be redeclared/redefined in subclass. Although it may appear to be overidden polymorphism will not apply.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Jeff Kumar:
Kicky San:
1. static methods can be inherited
>> Right


I am still not sure about this point.

Inheritance means, getting super class's method in sub class, adding specific behaviour in sub class if we need, calling super class's method from that with super keyword...

Very confuse...
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
Inheritance means, getting super class's method in sub class, adding specific behaviour in sub class if we need, calling super class's method from that with super keyword...


If we are able to invoke the parent class' method either by using the subclass object or by using the subclass' name, I guess the method qualifies for inheritance.



Arvind
Enge Chall
Ranch Hand

Joined: Nov 22, 2005
Posts: 30
Originally posted by Kicky San:
From this thread and by the following example, I am jotting down my observations. Please correct me if i am wrong.

1. static methods can be inherited
2. static methods can be called both by the class name and the class instance
3. static methods cant be overridden. static means one in a class.
4. Even if there is a static method in the child class same as the one in the parent class, it is not the overriden method. It is a seperate method of the child class.

Hope this example will clear all these points.

I have modified the previous example. Here is the modified version

class InheritStaticBase
{
public static void print()
{
System.out.println("I am in base class");
}
}

class InheritStaticChild extends InheritStaticBase
{
public static void print()
{
System.out.println("I am in child class");
}
}

class Main
{
public static void main(String args[])
{
System.out.println("using parent class name");
InheritStaticBase.print();
System.out.println("using child class name");
InheritStaticChild.print();
System.out.println("befre instantiating child class");
InheritStaticChild a = new InheritStaticChild();
a.print();
System.out.println("after instantiating child class");
System.out.println("befre instantiating parent class");
InheritStaticBase b = new InheritStaticBase();
b.print();
System.out.println("befre instantiating parent class");
}
}


Hi Kicky, thank you for the explanation. But according to your theory :

Does it mean that if I have a main() method( ie entry point for execution ) in the Base class,
don't I need to write one more main() method in the sub classes (say I need same logic of main() method for sub classes also) ??? ie

If I run the prog say: java Sub
It must invoke the Superclass main() method if I don't have a main() in the sub class... am I right ?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Enge Chall:


Hi Kicky, thank you for the explanation. But according to your theory :

Does it mean that if I have a main() method( ie entry point for execution ) in the Base class,
don't I need to write one more main() method in the sub classes (say I need same logic of main() method for sub classes also) ??? ie

If I run the prog say: java Sub
It must invoke the Superclass main() method if I don't have a main() in the sub class... am I right ?



But that doesn't work. You will get 'no main method' error.

I am still confused.
Krishnamoorthy Sethuraman
Greenhorn

Joined: Dec 12, 2005
Posts: 24
This link : http://java.sun.com/docs/books/tutorial/java/javaOO/override.html explains this issue with details.

For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called. For instance methods, the runtime system invokes the method defined in the runtime type of the reference on which the method is called.

I paste here the same for easy reference:
===================================================================
public class Animal {
public static void hide() {
System.out.println("The hide method in Animal.");
}
public void override() {
System.out.println("The override method in Animal.");
}
}

The second class, a subclass of Animal, is called Cat:

public class Cat extends Animal {
public static void hide() {
System.out.println("The hide method in Cat.");
}
public void override() {
System.out.println("The override method in Cat.");
}

public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = (Animal)myCat;
myAnimal.hide();
myAnimal.override();
}
}

The Cat class overrides the instance method in Animal called override and hides the class method in Animal called hide. The main method in this class creates an instance of Cat, casts it to a Animal reference, and then calls both the hide and the override methods on the instance. The output from this program is as follows:

The hide method in Animal.
The override method in Cat.
===================================================================
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
Please try it out

public class Test1
{
public static void main(String[] args)
{
System.out.println("Main method");
}
}


public class Test2 extends Test1
{
}


>java Test2
Main method

So main mathod is inherited as well.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Tim, here's what you said:

you need to pass the scjp again or improve yourself to know that the static method can be called by both the class name and instance name.

I agree but that's a compiler trick.
Look at this interesting example:

Will it compile? Will it run?
Sashi

[ December 15, 2005: Message edited by: Sasikanth Malladi ]
[ December 15, 2005: Message edited by: Sasikanth Malladi ]
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
Yeah i was able to compile it and run it as well



Arvind
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225

you need to pass the scjp again or improve yourself to know that the static method can be called by both the class name and instance name.


Well, I'm happy to learn sothething today...
However, I'm not sure to get my SCJD exam if I call all my static methods from instance names...
Enge Chall
Ranch Hand

Joined: Nov 22, 2005
Posts: 30
Thank you one and all for your nice explanations.

Eight of our team members are writing SCJP in the 2nd week of Jan-06.

best regards
Ravisekhar Kovuru
Greenhorn

Joined: Sep 23, 2003
Posts: 25
Originally posted by Sasikanth Malladi:
Tim, here's what you said:


Looks like this is interesting....
Eventhough doNothing() returns null, why don't we get NullPointerException??
Does it mean that even if we call static methods on instance, the JVM calls them on Class name???
Can someone clarify this please..
[ December 20, 2005: Message edited by: Ravisekhar Kovuru ]
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Originally posted by Ravisekhar Kovuru:


Does it mean that even if we call static methods on instance, the JVM calls them on Class name???
Can someone clarify this please..
[/QB]


Yes, but not the JVM, but the compiler does it. JVM does not know how to invoke static method on instance so the compiler compiles it as a static method call on class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: does static methods inherit ? what about private ?
 
Similar Threads
inheriting static members
Inheritance
Can static method be overridden?
WHy does main() work with private accessibility?
static method in Inheritance