
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we declare the main () method as final in Java?
Yes, we can declare the main () method as final in Java. The compiler does not throw any error.
- If we declare any method as final by placing the final keyword then that method becomes the final method.
- The main use of the final method in Java is they are not overridden.
- We can not override final methods in subclasses.
- If we are using inheritance and we need some methods not to overridden in subclasses then we need to make it final so that those methods can't be overridden by subclasses.
- We can access final methods in the subclass but we can not override final methods.
Example
class BaseClass { public final void show(Object o) { System.out.println("BaseClass method"); } } class DerivedClass extends BaseClass { public void show(Integer i) { System.out.println("DerivedClass method"); } } public class Test { public static final void main(String[] args) { // declaring main () method with final keyword. BaseClass b = new BaseClass(); DerivedClass d = new DerivedClass(); b.show(new Integer(0)); d.show(new Integer(0)); } }
Output
BaseClass method DerivedClass method
- Related Articles
- Can We declare main() method as Non-Static in java?
- Can we declare a main method as private in Java?\n
- Can we declare constructor as final in java?
- Can we declare an interface as final in java?
- Can we declare the method of an Interface final in java?
- Can we declare an abstract method final or static in java?
- Can we declare main() method as private or protected or with no access modifier in java?
- Can we declare final variables without initialization in java?
- Can we overload the main method in Java?
- Can we override the main method in java?
- Can we overload Java main method?
- Can we inherit a final method in Java?
- Can we declare a constructor as private in Java?
- Can we change return type of main() method in java?
- Can we declare a static variable within a method in java?

Advertisements