This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

The game of statics

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read on all the forums so many times that Static methods cannot be inherited and cannot be overridden. But I came across the code which is allowing me to override and inherit static methods. Please go through the code below for overriding the static method.


package foo;

class A{

A(){
System.out.println("Here is A's constructor");
}

public static void print()
{
System.out.println("Printing A");
}

}

public class B extends A{

B(){
System.out.println("Here is B's constructor");
}

public static void print()
{
System.out.println("Printing B");
}


public static void main(String[] args){

B b = new B();

B.print();

A.print();

}
}

The output I got for this was allowing me to override this method print. It worked fine. Please let me know if it is really possible to override and inherit static method.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harshad,

Originally posted by Harshad:I read on all the forums so many times that Static methods cannot be inherited and cannot be overridden. But I came across the code which is allowing me to override and inherit static methods. Please go through the code below for overriding the static method.



Whar do you mean by inheritance ?

try this :
A a = new B();
a.print();
you will know it by yourself !
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That example doesn't demonstrate overriding.

Static methods can be inherited by subclasses.

However, you cannot override them.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

I just want Harsad to know the fact that "overriding will not take place by that code."

You got me wrong. So sad.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Srinivasan. My post was a comment to the OP, not a criticism of your post.
 
Marshal
Posts: 70305
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshaut Raut, welcome to the Ranch.

Posted by Srinivasan thoyyeti

try this :
A a = new B();
a.print();

Now try thisYou have two separate methods in different classes, which just happen to have the same name.

Try this:-Result: won't work. You are not overriding at all.
 
This tiny ad is guaranteed to be gluten free.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic