Why instance method cannot override the static method [duplicate] - java

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 5 years ago.
class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
Compiler gives error at line 3 saying that
This instance method cannot override the static method from Abc
Why can static methods not be overridden by a instance method?

Simple: because the language specification says so.
That is one of the downsides of static methods: there is no polymorphism for them! Conceptually, they are not meant to be overridden. That is all there is to this.
To be precise: the JLS says differentiates between static and non-static method and states:
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.

Related

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

can a method be declared with static type argument in java ? if no, why? [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 6 years ago.
can a method be declared with static type argument in java ? if no, why?
ex:
class A
{
void m(static int x)
{
System.out.println(x);
}
}
I don't believe this is possible and I cannot think of any valid use case for doing it.
It may make sense to make the method static in order to implement a singleton pattern.
class A
{
static void m(int x)
{
System.out.println(x);
}
}
Then it could be used without having to instantiate A as follows:
A.m(1);
Alternatively you might want to make x immutable to avoid unexpected behavior. This would be done using "final" as follows:
class A
{
void m(final int x)
{
System.out.println(x);
}
}
But making x static would serve no purpose.
No it won't be allowed (compiler error) and it makes sense as well. The static keyword means that a variable will have only one instance in its scope and that instance is invisible outside that scope. Neither of this makes sense for a function argument.
§6.7.5.3/2: "The only storage-class specifier that shall occur in a parameter declaration is register."
Static members are considered as class level members and gets loaded
in memory during class loading, which means it is desirable that it
shouldn't have dependency on the class instances.
So your class has member method m which need a parameter to be passed to execute the method body.
If you declare a static member for it that doesn't make any sense
because it doesn't have any existence outside the method, class has no
information about it load time which severely violates all the rules stated
above.

Is this method overloading, overriding or none? [duplicate]

This question already has answers here:
What is the difference between method overloading and overriding? [duplicate]
(2 answers)
Closed 7 years ago.
Difference between method overloading and overriding in java? does not give the correct answer. Below is java code.
Parent class
public class Parent {
void display() {
// some code
}
}
Child class
public class child extends Parent
void display(int a) {
// some code
}
}
Question: Is this Method overloading, overriding or none?
That's overloading (in child), because JLS 8.4.9:
If two methods of a class (whether both declared in the same class, or
both inherited by a class, or one declared and one inherited) have the
same name but signatures that are not override-equivalent, then the
method name is said to be overloaded.
This is Overloading
Method Overloading - method in Same Class or different class
Method Overriding - method both in Parent Child class
Here method has different signature in both Parent and Child class

Why main() is declared public and static in java [duplicate]

This question already has answers here:
why main method can't be of default scope? [duplicate]
(9 answers)
Why is the Java main method static?
(37 answers)
Closed 8 years ago.
Why is the main declared as public and static?
public static void main(String arg[])
{}
acording to ans in java
"The method is static because otherwise there would be ambiguity: which constructor should be called?"
public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.
static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.
void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.

Override vs Shadowing in Java [duplicate]

This question already has answers here:
Overriding vs Hiding Java - Confused
(17 answers)
Closed 9 years ago.
What is the difference between overriding and shadowing in particular to this statement
“A static method cannot be overriden in a subclass, only shadowed"
If you were truly overriding a method, you could call super() at some point in it to invoke the superclass implementation. But since static methods belong to a class, not an instance, you can only "shadow" them by providing a method with the same signature. Static members of any kind cannot be inherited, since they must be accessed via the class.
Overrideing... This term refer to polymorphic behavior for e.g
class A {
method(){...} // method in A
}
class B extends A {
#Override
method(){...} // method in B with diff impl.
}
When you try to invoke the method from class B you get overriden behvaior ..e.g
A myA = new B();
myB.method(); // this will print the content of from overriden method as its polymorphic behavior
but suppose you have declared the method with static modifier than by trying the same code
A myA = new B();
myA.method(); // this will print the content of the method from the class A
this is beacuse static methods cant be overriden ... the method in class B just shadow the method from class A...

Categories