Can we write main method in subclass?
William Jenkins
Updated on March 04, 2026
Well, it depends, But usually main method put in sub class. There is quite big difference between in inheritance and shadowing. But you can put it subclass or super class. Remember main method is static.
Can a subclass use methods of superclass?
Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).Can we have main method in super class?
1) No. we cannot override main method, in fact we cannot override any static method. We can have static method( here main) with same name in both super class and sub class but they don't participate in polymorphism( that is they are not overriden).Can a subclass have new methods?
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass.Can we call Main method from child class?
Solution: Though Java doesn't prefer main() method called from somewhere else in the program, it does not prohibit one from doing it as well. So, in fact, we can call the main() method whenever and wherever we need to.Java Main Method Tutorial - Everything You Need to Know
Can subclass have main method in Java?
We have to put the main method inside the main class only. Then whether you make it a parent class or sub class doesn't matter, It will work fine, But the main method needs to be inside the main class else compiler will throw error.Can we execute program without main?
Yes, we can execute a java program without a main method by using a static block. Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block.Can subclasses have their own methods?
A subclass can do more than that; it can define a method that has exactly the same method signature (name and argument types) as a method in its superclass.Can subclass have the same method name?
If your subclass defines a method with the same name and signature as a method in its superclass, the method in the subclass overrides the one in the superclass. Thus, the subclass does not inherit the method from its superclass.Can the main method be overridden?
Overriding main methodYou cannot override static methods and since the public static void main() method is static we cannot override it.