This question already has answers here:
Is method hiding a form of Polymorphism?
(5 answers)
Closed 4 years ago.
I read somewhere that overriding an static methods can not be considered polymorphism. Just hiding the method. What does it mean "hide"?
public class TesterClass {
public static void main(String[] args) {
ClassLetters.staticM();
ClassA.staticM();
ClassLetters Lettersobj = new ClassA();
ClassA Aobj = new ClassA();
Lettersobj.staticM();
Aobj.staticM();
ClassA aa = (ClassA) Lettersobj;
aa.staticM();
}
}
Output:
Static Method Called in CLassLetters
Static Method Called in ClassA
Static Method Called in CLassLetters
Static Method Called in ClassA
Static Method Called in ClassA
classA inherits ClassLetters with same static void classM. Why it is not polymorphism?
You can't override static members, because they are not inherited. They belong to the class itself, period.
If you create a subclass, and you create a (new) static method with the same name/parameters/... you don't override the original one, you re-define a new one.
So, the static method in your original class is not overridden, but hidden.
Related
This question already has answers here:
Are static methods inherited in Java?
(15 answers)
Closed 4 years ago.
From what I understand, usually the static method should be called using class's reference or it can be called directly without reference if its in a static method or static block.
But does this apply when static method is called from child class static blocks?
Why it allows such thing, as static methods are not inherited, it should only be allowed using parent class name right?
public abstract class abs {
/**
* #param args
*/
abstract void m();
static void n(){
System.out.println("satic method");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class myclass extends abs{
#Override
void m() {
// TODO Auto-generated method stub
}
static{
n();
}
}
Why my child class static block can call parent class static method without reference or classname?
Static method n() is inherited by subclass myclass, so you can call it directly in the static block of myclass.
Usually the static method should be called using class's reference or
it can be called directly without reference if its in a static method
or static block.
Not really. For example an instance method can invoke a static method without prefixing the class.
More generally, static members (fields as methods) have to be invoked by prefixing their class only as the compiler cannot infer the class where they belong to.
As you invoke a static method defined in the parent class from a subclass (and static methods are inherited in the subclasses), you don't need to prefix the class of the method invocation as the compiler infer that.
Because you inherited the parent class, you have access to all non private members of that class directly as if it belonged to the child class.
Why it allows such thing
By inheritance.
as static methods are not inherited
You keep saying that. You're mistaken. From JLS #8.4.8:
A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true: ...
For continuation see here.
All the members of superclass are inherited by subclass, which includes static methods too.
class SuperClassA {
static void superclassmethod() {
System.out.println("superclassmethod in Superclass ");
}
}
public class SubClassA extends SuperClassA {
static {
superclassmethod();
}
public static void main(String[] args) {
}
}
But when a static method of superclass is override it hides the superclass static method not overrides 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:
“overriding” private methods with upcasting call in java
(1 answer)
Overriding private methods in Java
(10 answers)
Closed 5 years ago.
Can anybody explain why output to below question is "A.test" ?
class A {
private void test(){
System.out.println("A.test");
}
public void mytest(){
this.test();
}
}
class B extends A{
protected void test(){
System.out.println("B.test");
}
}
public class Test{
public static void main(String[] args) {
A a = new B();
a.mytest();
}
}
The test() method of class A cannot be overridden by class B, since it is private. Therefore this.test(); invokes A's test() method even though it is executed on an instance of class B.
Super class can invoke the methods of the sub class without using typecasting, without using reflection and without using a reference other than this only if they are overridden.
In your case A a = new B(); , object of B is created which has both the behaviours private void test() as inherited by super class A as well as protected void test() as added by B. The reason for both the methods being there and not just one is that due to the acess modifier being private the method is not visible in subclass scope. Hence it can not overriden and adding the method with same name just simply adds another method.
As there is no run time polymorphism in this case hence compile time target method is resolved and method defined in A is invoked.
If in case you would have overriden the mytest in B and from the overriden mytest you would have made a call to test then method in B would have been invoked. This is easy to understand as any method of B can not see any private method of its super class.
This question already has answers here:
Is it possible to override a static method in derived class?
(6 answers)
Closed 7 years ago.
lets assume that i have 2 classes.
ParentClass:
public class ParentClass {
public static void getInstance(){
System.out.println("Parent method");
}
}
ChildClass:
public class ChildClass extends ParentClass {
public static void getInstance(){
System.out.println("child method");
}
public static void main(String args[]){
ParentClass pc=new ChildClass();
pc.getInstance();
}
}
as you notice above both classes has a static method called getInstance() and in java and many other languages if there is an inherited method and you have the same method in the child class the method that get executed is one in the child class.
the question is: why pc.getInstance(); calls the method in the parent class? yeah there is no method overriding for static methods but could anyone please explain more the weird behavior of pc instance and why does it refer to the parent method even tho its pointing on the child class??
and why is it allowed to call a static method with a reference to an instance of the class ?
Thanks
There is no method overriding for static methods. The static type of the instance being used to call the method (ParentClass in your example) determines which method is called.
Besides that, it's bad practice to use an instance reference in order to call a static method. You should use ClassName.methodName() to execute a static method.
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.