Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

static and inheritance

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i am preparing for SCJP 6 and am following Kathy&Bates study guide. Till now i have covered first two chapters.

got some doubt when am trying below code.

class A
{
static void fun1()
{
System.out.println("inside class A");
}
}

class B extends A
{

public static void main(String args[])
{
fun1();
}
}

i saw in topic of "static" that static methods can never be overridden. so there is no possibility of inheriting static members of superclass right?

How the fun1() call from subclass is happening then? can anyone please explain.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinod yenduri wrote:
i saw in topic of "static" that static methods can never be overridden.



Correct ,you cant override a static method. Static is related to a class(template) . so it is common for an instances of the class.

is it make a sense?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to javaranch . we are happy to have you here
 
vinod yenduri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but when i run the above code i am not getting any compilation error. instead i am seeing the output as : "Inside class A"

My question is How come the static method fun1() of superclass A is available to subclass B?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinod yenduri wrote:but when i run the above code i am not getting any compilation error. instead i am seeing the output as : "Inside class A"

My question is How come the static method fun1() of superclass A is available to subclass B?



whether both the classes in a same package? if not you will get compilation error.otherwise you will get your output.

by the way what error you are getting? please post the error first

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static method doesn't need an instance. It runs directly.

Normally, when you create an instance is like:

A a = new A();
a.fun1();

But if the method is static, it means the method and its variables are only ONE for ALL the instances.

By the way, in the example you provided, there is no override... you are just using the STATIC method in class A.

I hope it helps...
Dave
 
vinod yenduri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Davide Crudo wrote:

By the way, in the example you provided, there is no override... you are just using the STATIC method in class A.

Dave



so you mean to say that a subclass can inherit the static methods of superclass??
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
member in super class is always available to all the sub classes unless the member in the super class is private.

But you can override only the instance method where the polymorphism comes into play but this is not for the static method,all you can do is you can redeclare the static method inthe sub class but that does not means overriding.Call to static method always depends on reference type where as call to overriden instance method always depends on object type at run time.

Think of like this if you have some members declared in super class and a class extend that super class,then it is almost like you declared those member in sub class and you can use them in the sub class as such you declared inthe sub class unless they are private.But if you redeclare those members in the sub class itself then polymorphism comes into play in the form of overriden whch depends on the member type(static or instance)
 
vinod yenduri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siva Masilamani wrote:member in super class is always available to all the sub classes unless the member in the super class is private.



Thanks siva. you are very clear in what you said.

am aware of overriding and redeclaring concepts for static methods though thanks for your stuff which helps me to recollect those concepts.

pretty confused in area of Inheritance of static methods. i was thinking in this way "as we cannot override static methods there is no way of inheriting them". As Overriding comes into picture only if inheritance is possible.

thanks for making me clear
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I have the same doubt regarding the inheritance of the static methods.I know how hiding, overriding works. I also known that a static method can't be overridden, only redefined in subclases.
What is not very clear for me is if the static methods can or can't be inherited.

I tried to write many pieces of code and I came to conclusion that static methods are inherited. Regarding the inheritance concept, they only differ than instance methods when we talk about polymorphism.

Is my assumption correct?

Thanks!
Dragos
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from constructor and private members everything from super class is inherited by all its Sub classes.

But the concept of polymorphism comes into play only for instance method or when assigning super class reference variable to sub class object.

When we extend a class there is always a possiblity for polymorphism which can be in either of the form as expained int he previous line.
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic