• 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

does static methods inherit ? what about private ?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most people learn best by doing, so if you have a question like this try writing some code that tests out your question.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 )
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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()
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2813
  • 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. 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)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Gyan Shankar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i was able to compile it and run it as well



Arvind
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic