This question already has answers here:
What is the difference between static and default methods in a Java interface?
(12 answers)
Closed 5 years ago.
interface TestInterface
{
public static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
class TestClass implements TestInterface
{
public void square(int a)
{
System.out.println(a*a);
}
public void show()
{
System.out.println("Overridden Method");
}
public static void main(String args[])
{
TestClass d=new TestClass();
d.square(4);
TestInterface.square(4);
TestInterface.show();
d.show();
}
}
I have a doubt in my code. I learnt that static methods cannot be overridden in JAVA, but it seems to be working fine here.
When i give both default and static keywords together, like this
interface TestInterface
{
default static void square(int a)
{
System.out.println("Square is "+a*a);
}
public static void show()
{
System.out.println("Default Method Executed");
}
}
An error crops up as follows:
illegal combination of modifiers: static and default
What is the reason for JAVA treating this as an error?
A static method is meant to be called without an instance of the class/interface concerned. Usually they are meant to be utility methods.
A default method is meant to be called on an instance of the interface concerned. All implementations of this interface will have this method definition, unless it is overridden.
The reason these two terms are not allowed together is simply because they contradict each other: default requires an object, static requires no object.
TestClass.show() and TestClass.square() are not static and therefore do not override the static methods in the interface. They are member methods and require an object to call them. On the other hand, the methods with the same name in the interface are static and so you can call them with the interface name or class name without an object.
Related
This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
If static methods can't be overridden, how its working here (For Java)?
(3 answers)
Why doesn't the compiler complain when I try to override a static method?
(9 answers)
Closed 4 years ago.
Trying to work on an example from a book on method overriding.
The code has two classes Veggies and Pumpkin.The only confusion i have is if the object of pumpkin is assigned to the myveggie object, why does it call the method of veggies class since the object is from derived class.
I am sure this has something to do with static method,but I could get a more clear idea.
class Veggies {
public static void staticMethod(){
System.out.println("Veggies static method");
}
public void instancemethod(){
System.out.println("Veggies instance method");
}
}
public class Pumpkin extends Veggies{
public static void staticMethod(){
System.out.println("Pumpkin static method");
}
#Override
public void instancemethod(){
System.out.println("Pumpkin instance method");
}
public static void main(String[] args) {
Pumpkin myPumpkin=new Pumpkin();
Veggies myveggie=myPumpkin;
myveggie.staticMethod();
myveggie.instancemethod();
}
}
As this question:
Why doesn't Java allow overriding of static methods?
explains, static methods cannot be overridden in Java.
What you have in your example, is code that is calling a static method via an instance. This is bad practice.
What actually happens is that the static type of the variable (not the reference!) is resolved at compile time to a class. In this case, the class is Veggies. Then the compiler inserts a call to the static method of that class; i.e. Veggies.staticMethod().
Which is confusing ... because it looks like you should be dispatching to a static method associated with the reference. But that is not how Java works.
That's why this is bad practice, and why a lot of IDEs will warn you about it.
This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Is it possible to override a static method in derived class?
(6 answers)
Closed 5 years ago.
class B extends A {
static public void printMe(){
System.out.println("in static B");
}
}
class A{
static public void printMe(){
System.out.println("In static A");
}
}
public static void main(String[] args) {
A a = new B();
a.printMe();
}
Why is the output "In static A" ?
Static members bind to type rather than implemented type. Hence you see the methods executing from class A.
And static members are not to be ovverriden and they share same copy regardless of instance state.
If you need methods to be ovveriden, do not use them as static members.
Static member or method belongs to class level instead of specific instance.
A static class will have only 1 instance even if you create multiple instance or do not create instance.
In your case, since you have created instance of class A, method inside class A is implemented.
Another way to get a clear concise for the problem scenario is try running the code mentioned below:
public static void main(String[] args) {
A.printMe();
}
You will get clear idea.
This question already has answers here:
Reducing the visibility of a static method
(4 answers)
Closed 7 years ago.
It's not permitted to override and reduce visibility of inherited methods, but the point is you can't override a static method anyway:
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
So why is the second hide ok while the first one is not?
public class Base {
public static void foo(){
System.out.println("Base");
}
}
hide1:
public class Sub extends Base {
static void foo(){ //cannot reduce visibility error!
System.out.println("Sub");
}
}
hide2:
public class Sub extends Base {
public static void foo(){ //OK
System.out.println("Sub");
}
}
The reason is, you might want to give an instance of your subclass as an type of the superclass to a consumer. This consumer tries to run the (public) method he is used to run, since it's public, but you hid it with making it protected. To prevent this, this rule is introduced. (the problem is visibility, not static!).
Although I have to admit, this reasoning doesn't completely apply to static methods, just when the consumer wants to directly have a subclass (as a subclass) like he was used to run the superclass.
It is ok to hide a static superclass method in your subclass:
public class Base {
public static void foo(){
System.out.println("Base");
}
}
public class Sub extends Base {
static void foo(){ //cannot reduce visibility error!
System.out.println("Sub");
}
}
public class Sub1 extends Base {
public static void foo(){ //OK
System.out.println("Sub1");
}
}
public class Main {
public static void main(String[] args)
{
Base.foo();
Sub1.foo();
}
}
Output:
Base
Sub1
In Sub you are trying to reduce visibility (going from public to no modifier). In Sub1 you are not reducing visibility (going from public to public). That is why it compiles in Sub1 but not Sub. It is about the visibility of the method. It has nothing to do with overriding/hiding. (Hiding in this case since it is a static method).
If you try to run Sub.foo() in the main method without changing anything you will get this exception:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from Base
Here is a similar question and answer.
This question already has answers here:
Are static methods inherited in Java?
(15 answers)
Closed 8 years ago.
I was able to run the following code:
class A
{
public static void display()
{
System.out.println("Inside static method of superclass");
}
}
class B extends A
{
public void show()
{
display();
}
}
public class staticMethodInheritance {
public static void main(String[] args) {
B b = new B();
b.display();
}
}
Now I was able to access the method display() from the instance of the class B then why is it said that static methods cannot be inherited. If I declare a method display in class B then it is said that the method in the superclass is hidden and the method in the child class is called then again isnt this the behavior that is desired when we override a method.
The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.
Static methods are inherited but cannot be overriden they can be re- defined.
This question already has answers here:
Static methods and their overriding
(5 answers)
Closed 8 years ago.
What should be printed when we execute this code?
I picked the question from here where the answer is provided but I kinda believe is wrong. First the call to a static function has to be done in static way, second when we override a static function the previous one is no longer accessible ( no new memory is assigned)
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public static void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
static method belongs to class itself and overriding works for instance methods only. If you define the same method again in sub-class then it's called re-defining hiding of the static method.
static method looks for class to invoke it and that can be verified by below same code:
Base b = null;
b.show(); // Base::show() called
It doesn't make any sense to call static method via object.