Skip to main content
spelling in title
Source Link
Cœur
  • 39k
  • 25
  • 207
  • 282

Why static method of parent class is called when subclass has already overiddenoverridden it?

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code  :

public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case  ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

Why static method of parent class is called when subclass has already overidden it?

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code  :

public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case  ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

Why static method of parent class is called when subclass has already overridden it?

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code:

public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

deleted 1 characters in body
Source Link
Nicolas78
  • 5.1k
  • 1
  • 26
  • 43

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code :

 public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code :

 public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code :

public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

Source Link
Kshitij Jain
  • 1.8k
  • 5
  • 21
  • 30

Why static method of parent class is called when subclass has already overidden it?

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code :

 public class SuperClass { public static void staticMethod() { System.out.println("SuperClass: inside staticMethod"); } } public class SubClass extends SuperClass { //overriding the static method public static void staticMethod() { System.out.println("SubClass: inside staticMethod"); } } public class CheckClass { public static void main(String[] args) { SuperClass superClassWithSuperCons = new SuperClass(); SuperClass superClassWithSubCons = new SubClass(); SubClass subClassWithSubCons = new SubClass(); superClassWithSuperCons.staticMethod(); superClassWithSubCons.staticMethod(); subClassWithSubCons.staticMethod(); } } Below is the output which we are getting : 1) SuperClass: inside staticMethod 2) SuperClass: inside staticMethod 3) SubClass: inside staticMethod 

Why static method of superclass gets called here in the second case ?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.