Why overriding a static method does not result in polymorphism in Java

Many say that we cannot override a static methods.

But we can override a static method.

The question is, when we override a static method why it does not result in polymorphism ?

2

Interesting question, I tested it this way:

Superclass

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ClassA {
public static void printStatic(){
System.out.println("hi static from A");
}
public void printDynamic(){
System.out.println("hi dynamic from A");
}
}
</code>
<code>public class ClassA { public static void printStatic(){ System.out.println("hi static from A"); } public void printDynamic(){ System.out.println("hi dynamic from A"); } } </code>
public class ClassA {       
    public static void printStatic(){
        System.out.println("hi static from A");
    }       
    public void printDynamic(){
        System.out.println("hi dynamic from A");
    }       
}

Subclass

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ClassB extends ClassA {
public static void printStatic(){
System.out.println("hi static from B");
}
public void printDynamic(){
System.out.println("hi dynamic from B");
}
}
</code>
<code>public class ClassB extends ClassA { public static void printStatic(){ System.out.println("hi static from B"); } public void printDynamic(){ System.out.println("hi dynamic from B"); } } </code>
public class ClassB extends ClassA {
    public static void printStatic(){
        System.out.println("hi static from B");
    }
    public void printDynamic(){
        System.out.println("hi dynamic from B");
    }   

}

Test code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void main (String args[]){
ClassA x1 = new ClassB();
x1.printStatic();
x1.printDynamic();
ClassB x2 = new ClassB();
x2.printStatic();
x2.printDynamic();
}
</code>
<code>public static void main (String args[]){ ClassA x1 = new ClassB(); x1.printStatic(); x1.printDynamic(); ClassB x2 = new ClassB(); x2.printStatic(); x2.printDynamic(); } </code>
public static void main (String args[]){
    ClassA x1 = new ClassB();
    x1.printStatic();
    x1.printDynamic();
    ClassB x2 = new ClassB();
    x2.printStatic();
    x2.printDynamic();
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>hi static from A
hi dynamic from B
hi static from B
hi dynamic from B
</code>
<code>hi static from A hi dynamic from B hi static from B hi dynamic from B </code>
hi static from A
hi dynamic from B
hi static from B
hi dynamic from B

Analysis of test output

Calling a static method allways executes the method in the declaring type, not the overriden method in the instantiated type (no polymorphism), whereas calling a dynamic method always executes the overridden method in the instantiated type (polymorphism).

My explanation

I think that’s because static methods belong to classes, not to objects. That’s why eclipse suggest you to call the method in a static way using the name of the class and not the name of the object:

enter image description here

  • Static methods always refer to the declaration type.
  • It’s the same reason static fields/members/attributes are the same to every object, because they only exist once, in the class itself.
  • By calling a static method you are specifically telling Java you want the method in the declaring type executed.

I hope my answer doesn’t sound like Captain Obvious wrote it.

3

It is polymorphism, but not the same kind of polymorphism as when overriding instance methods.

It is really a question about terminology. A static method with the same name in a subclass is a form of ad-hoc polymorphism, somewhat similar to method overloading. The word overloading (rather than overriding) is usually used with ad-hoc polymorphism (like in “operator overloading” which is also a kind of ad-hoc polymorphism). But overriding instance methods is subtype polymorphism which is a different thing.

When talking about polymorphism in OO, usually subtype polymorphism is meant. With ad-hoc polymorphism, the method to be called is statically resolved at compile time. With subtype polymorphism the method implementation is selected at runtime. Subtype polymorphism is fundamental to object orientation while ad-hoc polymorphism is more of a convenience.

1

Polymorphism has two types:
static polymorphism : achieved using method overloading in java
dynamic polymorphism: achieved using method overriding

Your question deals with dynamic polymorphism.
Dynamic polymorphism is related to inheritance.
Let’s consider the example where child class ClassB override a method called methodA present in the parent class ClassA.

when writing this code :

ClassA c = new ClassB();
c.methodA();

Dynamic polymorphism in java dictates that a dynamic binding occurs here and the method methodA defined in class B will be called.

Static methods belong to the class and not to individual objects. When you define a static method in the child class, this method has no relationship with the parent method.
It is considered as a new method.
You can take a look at these tutorials for more informations about method overriding and polymorphism.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why overriding a static method does not result in polymorphism in Java

Many say that we cannot override a static methods.

But we can override a static method.

The question is, when we override a static method why it does not result in polymorphism ?

2

Interesting question, I tested it this way:

Superclass

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ClassA {
public static void printStatic(){
System.out.println("hi static from A");
}
public void printDynamic(){
System.out.println("hi dynamic from A");
}
}
</code>
<code>public class ClassA { public static void printStatic(){ System.out.println("hi static from A"); } public void printDynamic(){ System.out.println("hi dynamic from A"); } } </code>
public class ClassA {       
    public static void printStatic(){
        System.out.println("hi static from A");
    }       
    public void printDynamic(){
        System.out.println("hi dynamic from A");
    }       
}

Subclass

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ClassB extends ClassA {
public static void printStatic(){
System.out.println("hi static from B");
}
public void printDynamic(){
System.out.println("hi dynamic from B");
}
}
</code>
<code>public class ClassB extends ClassA { public static void printStatic(){ System.out.println("hi static from B"); } public void printDynamic(){ System.out.println("hi dynamic from B"); } } </code>
public class ClassB extends ClassA {
    public static void printStatic(){
        System.out.println("hi static from B");
    }
    public void printDynamic(){
        System.out.println("hi dynamic from B");
    }   

}

Test code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void main (String args[]){
ClassA x1 = new ClassB();
x1.printStatic();
x1.printDynamic();
ClassB x2 = new ClassB();
x2.printStatic();
x2.printDynamic();
}
</code>
<code>public static void main (String args[]){ ClassA x1 = new ClassB(); x1.printStatic(); x1.printDynamic(); ClassB x2 = new ClassB(); x2.printStatic(); x2.printDynamic(); } </code>
public static void main (String args[]){
    ClassA x1 = new ClassB();
    x1.printStatic();
    x1.printDynamic();
    ClassB x2 = new ClassB();
    x2.printStatic();
    x2.printDynamic();
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>hi static from A
hi dynamic from B
hi static from B
hi dynamic from B
</code>
<code>hi static from A hi dynamic from B hi static from B hi dynamic from B </code>
hi static from A
hi dynamic from B
hi static from B
hi dynamic from B

Analysis of test output

Calling a static method allways executes the method in the declaring type, not the overriden method in the instantiated type (no polymorphism), whereas calling a dynamic method always executes the overridden method in the instantiated type (polymorphism).

My explanation

I think that’s because static methods belong to classes, not to objects. That’s why eclipse suggest you to call the method in a static way using the name of the class and not the name of the object:

enter image description here

  • Static methods always refer to the declaration type.
  • It’s the same reason static fields/members/attributes are the same to every object, because they only exist once, in the class itself.
  • By calling a static method you are specifically telling Java you want the method in the declaring type executed.

I hope my answer doesn’t sound like Captain Obvious wrote it.

3

It is polymorphism, but not the same kind of polymorphism as when overriding instance methods.

It is really a question about terminology. A static method with the same name in a subclass is a form of ad-hoc polymorphism, somewhat similar to method overloading. The word overloading (rather than overriding) is usually used with ad-hoc polymorphism (like in “operator overloading” which is also a kind of ad-hoc polymorphism). But overriding instance methods is subtype polymorphism which is a different thing.

When talking about polymorphism in OO, usually subtype polymorphism is meant. With ad-hoc polymorphism, the method to be called is statically resolved at compile time. With subtype polymorphism the method implementation is selected at runtime. Subtype polymorphism is fundamental to object orientation while ad-hoc polymorphism is more of a convenience.

1

Polymorphism has two types:
static polymorphism : achieved using method overloading in java
dynamic polymorphism: achieved using method overriding

Your question deals with dynamic polymorphism.
Dynamic polymorphism is related to inheritance.
Let’s consider the example where child class ClassB override a method called methodA present in the parent class ClassA.

when writing this code :

ClassA c = new ClassB();
c.methodA();

Dynamic polymorphism in java dictates that a dynamic binding occurs here and the method methodA defined in class B will be called.

Static methods belong to the class and not to individual objects. When you define a static method in the child class, this method has no relationship with the parent method.
It is considered as a new method.
You can take a look at these tutorials for more informations about method overriding and polymorphism.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật