BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to restrict inheritance in java using Final Classes and Methods

Last Updated: December 14, 2024 by Chaitanya Singh | Filed Under: java

In Java, final classes and methods are used to restrict inheritance and prevent modifications in subclasses. This is useful when you want to ensure that class or its behaviour does not change.

Prerequisite:

  • Inheritance in java
  • Common errors in Inheritance

1. Final Classes

A class declared as final cannot be extended (cannot inherit it). As the name suggests, this is used for final classes (critical or sensitive classes) whose behaviour is final and remains unchanged.

Syntax

final class Parent {
void display() {
System.out.println("This is a final class");
}
}
class Child extends Parent { // Error: Cannot inherit from final 'Parent'
}

Where this needs to be used:

  • Security: classes that deals with the sensitive data such as user personal information.
  • Immutability: For immutable classes to ensure that no subclass can modify the behaviour (e.g., String class in Java).

2. Final Methods

A final method cannot be overridden in a subclass. This ensures that a specific implementation of the specific method remains same for all the derived classes. Let’s say you want to declare a method in class so that it can be used in child classes but you don’t want the child classes to alter the method definition.

Syntax

class Parent {
final void display() {
System.out.println("This is a final method");
}
}
class Child extends Parent {
// void display() { // Error: Cannot override final method
// System.out.println("Overriding not allowed");
// }
}

Use Cases

  • Preserve Behavior: Prevent subclasses from altering critical logic.
  • Enforce Consistency: To make the method work same in all sub classes.

3. Final Variables in Inheritance

Error: This would ensure that the value of final variable remains unchanged in sub classes. A subclass cannot alter the value of the final variable.

class Parent {
final int NUM = 10;
}
class Child extends Parent {
void update() {
// NUM = 20; // Error: Cannot assign a value to final variable 'NUM'
}
}

4. Rules for Final Classes and Methods

  1. A final method can be inherited but not overridden.
  2. If a class is declared as final, all its methods are implicitly final.
  3. Constructors cannot be declared as final because they are not inherited.

5. Examples of Final Classes in Java

  • Built-in Final Classes: Many Java standard library classes are final, such as:
    • String
    • Integer
    • Double
  • These classes are immutable, and their final status makes their behaviour consistent among sub classes.

6. Best Practices

  • Use final classes or methods for immutability and security.
  • Avoid overusing final, as it reduces flexibility in inheritance.
  • When designing an API, carefully decide which classes and methods should be final to balance extensibility and control.

Top Related Articles:

  1. Multithreading in java with examples
  2. Final Keyword In Java – Final variable, Method and Class
  3. Java – Static Class, Block, Methods and Variables
  4. Common Errors in Inheritance in Java: Examples and solutions
  5. StringJoiner toString() Method in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap