Using final with Inheritance in Java
Last Updated :
16 Mar, 2022
Prerequisite - Overriding in java, Inheritance
final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword.
Using final with inheritance
During inheritance, we must declare methods with the final keyword for which we are required to follow the same implementation throughout all the derived classes. Note that it is not necessary to declare final methods in the initial stage of inheritance(base class always). We can declare a final method in any subclass for which we want that if any other class extends this subclass, then it must follow the same implementation of the method as in that subclass.
Java
// Java program to illustrate
// use of final with inheritance
// base class
abstract class Shape
{
private double width;
private double height;
// Shape class parameterized constructor
public Shape(double width, double height)
{
this.width = width;
this.height = height;
}
// getWidth method is declared as final
// so any class extending
// Shape can't override it
public final double getWidth()
{
return width;
}
// getHeight method is declared as final
// so any class extending Shape
// can not override it
public final double getHeight()
{
return height;
}
// method getArea() declared abstract because
// it upon its subclasses to provide
// complete implementation
abstract double getArea();
}
// derived class one
class Rectangle extends Shape
{
// Rectangle class parameterized constructor
public Rectangle(double width, double height)
{
// calling Shape class constructor
super(width, height);
}
// getArea method is overridden and declared
// as final so any class extending
// Rectangle can't override it
@Override
final double getArea()
{
return this.getHeight() * this.getWidth();
}
}
//derived class two
class Square extends Shape
{
// Square class parameterized constructor
public Square(double side)
{
// calling Shape class constructor
super(side, side);
}
// getArea method is overridden and declared as
// final so any class extending
// Square can't override it
@Override
final double getArea()
{
return this.getHeight() * this.getWidth();
}
}
// Driver class
public class Test
{
public static void main(String[] args)
{
// creating Rectangle object
Shape s1 = new Rectangle(10, 20);
// creating Square object
Shape s2 = new Square(10);
// getting width and height of s1
System.out.println("width of s1 : "+ s1.getWidth());
System.out.println("height of s1 : "+ s1.getHeight());
// getting width and height of s2
System.out.println("width of s2 : "+ s2.getWidth());
System.out.println("height of s2 : "+ s2.getHeight());
//getting area of s1
System.out.println("area of s1 : "+ s1.getArea());
//getting area of s2
System.out.println("area of s2 : "+ s2.getArea());
}
}
Output:
width of s1 : 10.0
height of s1 : 20.0
width of s2 : 10.0
height of s2 : 10.0
area of s1 : 200.0
area of s2 : 100.0
Using final to Prevent Inheritance
When a class is declared as final then it cannot be subclassed i.e. no other class can extend it. This is particularly useful, for example, when creating an immutable class like the predefined String class. The following fragment illustrates the final keyword with a class:
final class A
{
// methods and fields
}
// The following class is illegal.
class B extends A
{
// ERROR! Can't subclass A
}
Note :
- Declaring a class as final implicitly declares all of its methods as final, too.
- It is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations. For more on abstract classes, refer abstract classes in java
Using final to Prevent Overriding
When a method is declared as final then it cannot be overridden by subclasses. The Object class does this—a number of its methods are final. The following fragment illustrates the final keyword with a method:
class A
{
final void m1()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void m1()
{
// ERROR! Can't override.
System.out.println("Illegal!");
}
}
Normally, Java resolves calls to methods dynamically, at run time. This is called late or dynamic binding. However, since final methods cannot be overridden, a call to one can be resolved at compile time. This is called early or static binding.
Similar Reads
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Object-Oriented Design (OOD) - System Design
A crucial method for system design is object-oriented design (OOD), which places an intense focus on scalability, modularity, and reusability. OOD resembles real-world systems by encapsulating data and behavior into objects, which facilitates understanding programs and maintenance. By utilizing conc
12 min read
Dynamic Method Dispatch or Runtime Polymorphism in Java
Prerequisite: Overriding in java, Inheritance Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called thro
5 min read
Method Overloading in Java
In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference can be in the number of parameters, the types of parameters, or the order of those parameters.Method overloading in Java is also known as Compile-time Polymorp
10 min read
Overriding in Java
Overriding in Java occurs when a subclass or child class implements a method that is already defined in the superclass or base class. When a subclass provides its own version of a method that is already defined in its superclass, we call it method overriding. The subclass method must match the paren
15 min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Object Creation of Inherited Class
In Java, being an object-oriented language, objects inside a class is created with help of constructors. When it comes down to inheritance in Java we are basically dealing with deriving a class from another class. Now let us understand inheritance a step deeper so when a particular class inherits a
3 min read
Java Multiple Inheritance
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass. On calling the method, the compiler cannot determine which c
4 min read
Super Keyword in Java
The super keyword in Java is a reference variable that is used to refer to the parent class when we are working with objects. You need to know the basics of Inheritance and Polymorphism to understand the Java super keyword. The Keyword "super" came into the picture with the concept of Inheritance. I
7 min read
Using final with Inheritance in Java
Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are
4 min read