
- 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
What will happen when we try to override final method of the superclass in Java?
Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of super class we will get an error in Java.
Rules for implementing Method Overriding
- The method declaration should be the same as that of the method that is to be overridden.
- The class (subclass) should extend another class (superclass), prior to even try overriding.
- The Sub Class can never override final methods of the Super Class.
Example
class Car { public void brake() { System.out.println("brake() method of Car"); } public final void accelerate() { System.out.println("accelerate() method of Car"); } } public class BenzCar extends Car { public static void main(String[] args) { BenzCar benz = new BenzCar(); benz.accelerate(); benz.brake(); } public void accelerate() { System.out.println("accelerate() method of Benz Car"); } }
In the above example, if we try to override the final method (accelerate() method) of the superclass. the compiler will throw an error. Hence we do not override the final method of the superclass in Java.
Output
overridden method is final
- Related Articles
- Can we override final methods in Java?
- What will happen if we directly call the run() method in Java?
- What happens if we try to extend a final class in java?
- Can we override the static method in Java?
- Can we override the equals() method in Java?
- Can we override the main method in java?
- Can we override a start() method in Java?
- Can we override a protected method in Java?
- Can we call methods of the superclass from a static method in java?
- Can we override a private or static method in Java
- Can we declare the method of an Interface final in java?
- Can we inherit a final method in Java?
- What happens when you declare a method/constructor final in Java?
- Can we declare the main () method as final in Java?
- Can we overload or override a static method in Java?\n

Advertisements