+6

Can you override static method in Java? if I create the same method in the subclass is it compile time error?

Please help me I am totally confused

21 Answers

New Answer

+3

Can someone please upvote the question . I want badge🤣🤣🤣🤣

+6

Mirielle👽 He is talking about override static method.

+3

We can't override static method in java. Check here for more details https://www.google.com/amp/s/www.geeksforgeeks.org/can-we-overload-or-override-static-methods-in-java/amp/

+3

+3

Yes,you can override a static methods but actually is called method hiding. You can take static methods both parent and child then parent method hide with child method it means both methods are available. For ex:- class P{ public static void m1(){ System.out.println("parent"); } } class C extends P{ public static void m1(){ System.out.println("child"); } } class Test{ public static void main(String[] args){ P p = new P(); p.m1(); //O/P:-parent C c = new C(); c.m1(); //O/P:-child P p1 = new C(); p1.m1(); //O/P:-parent } }

+2

+2

nilesh kanti Do you know what is static keyword and static method? Do you know what is overriding and overloading in Java? Do you know polymorphism?

+2

The static keyword in Java is used mainly for memory management. It is used with variables, methods, blocks and nested classes. It is a keyword that is used to share the same variable or method of a given class

+2

nilesh kanti And what about others?

+2

Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. Polymorphism in Java is a concept by which we can perform a single action in different ways .Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

+2

nilesh kanti If you understood about overriding and polymorphism can you tell us some real time example?

+2

+2

+1

No you cannot override static methods in Java. If you try to create the same method in the sub class than the method is redefined. This is also called method hiding.

+1

nilesh kanti You should also know the difference between static and non-static. static methods belongs to class, non-static methods belongs to instance of class. This question is already asked. Please use the search bar to avoid duplicates. You can search for "override static". https://www.sololearn.com/Discuss/1902084/?ref=app https://www.sololearn.com/Discuss/1481432/?ref=app https://www.sololearn.com/Discuss/1087912/?ref=app

+1

This is not right place for chat. We have messaging feature. Use that. nilesh kanti Aapko pahle bhi bola hai. QA section me chat allow nhi hai.

0

Why overriding is called runtime polymorphism? And why overloading is called compiletime polymorphism

0

You cannot override static method static and non- static use for memory management .read concepts of polymorphism u can understood better.

0

0