Questions tagged [final-class]

A Final Class is simply a class that can't be extended. (This does not mean that all references to objects of the class would act as if they were declared as final.)

A Final Class is simply a class that can't be extended.

(This does not mean that all references to objects of the class would act as if they were declared as final.)

TO ADDRESS THE FINAL CLASS PROBLEM:

There are two ways to make a class final. The first is to use the keyword final in the class declaration:

public final class SomeClass {
  //  . . . Class contents
}

The second way to make a class final is to declare all of its constructors as private:

public class SomeClass {
  public final static SOME_INSTANCE = new SomeClass(5);
  private SomeClass(final int value) {
  }

Marking it final saves you the trouble if finding out that it is actual a final, to demonstrate look at this Test class. looks public at first glance.

public class Test{
  private Test(Class beanClass, Class stopClass, int flags)
    throws Exception{
    //  . . . snip . . . 
  }
}

Unfortunately, since the only constructor of the class is private, it is impossible to extend this class. In the case of the Test class, there is no reason that the class should be final. The Test class is a good example of how implicit final classes can cause problems.

So you should mark it final when you implicitly make a class final by making it's constructor private.

11 questions
38
votes
4 answers

"Cannot subclass the final class" error, but the class is not final

Here is my code: package basic; public abstract class Entity {} package characters; import basic.Entity; public abstract class Character extends Entity {} package player; public class Player extends Character {} I am getting the The type…
Fletcher
  • 621
  • 6
  • 16
14
votes
2 answers

What is the difference between a final Class and a Record?

In simple words what is the difference between a final class and a record in Java 17? In which case should I use a record?
13
votes
4 answers

Can I extend a final class in Swift?

I'm using a third-party library for a new app that I'm making using Swift. The author of the class/library has made it final using the final keyword, probably to optimise and to prevent overriding its properties and methods. Example: final public…
metpb
  • 513
  • 8
  • 20
12
votes
2 answers

Final class with private constructor, what is the design principle

I was recently going through one of the Netflix open source project There I found use of both final class along with private constructor. I fully aware that final is to avoid inheritance private is to disallow instantiation But m just curious to…
Sudip7
  • 2,384
  • 3
  • 27
  • 35
3
votes
0 answers

How to get mock final classes mock-maker-inline to work?

I'm having issue getting the mock-maker-inline to work. I followed the instructions on However, i still can't mock out a final class. I also tried out the example in the document. I was wondering if there is anyway to debug what went…
darewreck
  • 2,576
  • 5
  • 42
  • 67
2
votes
3 answers

Protected Constructor in multilevel virtual inheritance in C++

How is the folloing code working? MakeFinal constructor is protected, so it should not be accessible to FinalUser class. But I didn't get any build or execution error. class MakeFinal { protected: MakeFinal(void) {}; public: …
Ravi
  • 37
  • 1
  • 3
1
vote
1 answer

Bytebuddy - subclass a final class

I'm trying to write a generic method that looks like this: private static Class immutableVersionOfClass(Class clazz) { return new ByteBuddy() .subclass(clazz) .method(not(returns(VOID))) …
k13i
  • 4,011
  • 3
  • 35
  • 63
1
vote
1 answer

Mock final class and inject it to autowired data member and overcome postConstruct method call

I want to unit test a java class with an autowired final class object, as well as another autowired class that has @PostConstruct method. While it is possible to test them individually, i am not able to combine them together. This question is an…
0
votes
1 answer

"Mockito cannot mock this class" error even if I am using powermockito to mock the class

I have a final class public final class A { private static final Set B = methodA(); private static Set methodA() { //does some processing and //returns a set } public static boolean methodB() { …
0
votes
1 answer

Invoke final class constructor from base class

I have an Exception class as follows: class ExtensionExceptionType; class Object; class Exception { public: explicit Exception () { } Exception( const std::string &reason ) { PyErr_SetString( _Exc_RuntimeError(),…
P i
  • 29,020
  • 36
  • 159
  • 267
-1
votes
1 answer

What is the best practice to handle related constant strings in Java?

Let us assume that I have to handle several constant strings that are closely related, for example types of messages ("ID1", "ID2", "ID3"). Currently I use a final class that only has public static final definitions: public final class MessageType…
molfo
  • 75
  • 7