9

I have a big confusion in the usage of the "final" keyword between classes and methods. I.e., why do final methods only support inheritance, but not final classes?

final class A{
    void print(){System.out.println("Hello, World!");}
}

class Final extends A{
      public static void main(String[] args){
        System.out.println("hello world");
       }
}

ERROR:

cannot inherit from Final A class Final extennds A{ FINAL METHOD IS..

class Bike{
    final void run(){System.out.println("run");}
}

class Honda extends Bike{
    public static void main(String[] args){
        Honda h=new Honda();
        h.run();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sravani.s
  • 175
  • 1
  • 1
  • 10
  • `final` means, you can't "override" it. In a class, you "override" it with inheritance (two different objects with the same type). In the method context you just have a new method with the same name and parameters (signature). In the variable context it is the value or reference. – ctst Feb 17 '16 at 13:44
  • 1
    try to `@Override` the final method in a child class, you will see a very similar error message... – SomeJavaGuy Feb 17 '16 at 13:46
  • 3
    Possible duplicate of [Java \`final\` method: what does it promise?](http://stackoverflow.com/questions/5547663/java-final-method-what-does-it-promise) – khelwood Feb 17 '16 at 13:58
  • I never knew that methods could be inherited, in OOPS, the inheritance is the property of a class, a method/ function cannot be inherited. – Sreekanth Karumanaghat Dec 24 '19 at 04:34
  • @SreekanthKarumanaghat - That is incorrect. Instance methods of a class are inherited by its subclasses. Not just in Java. This is a fundamental property of OO. Indeed, if subclasses do not inherit (or equivalent) the methods of their superclasses, then one of the fundamental requirements of OO is not satisfied. (According to accepted definitions of OO). See https://en.wikipedia.org/wiki/Object-oriented_programming#Composition,_inheritance,_and_delegation – Stephen C Nov 18 '20 at 07:17
  • Languages that support objects and encapsulation without inheritance / polymorphism are referred to as Object-based, not Object Oriented. – Stephen C Nov 18 '20 at 07:24

3 Answers3

25

Why can't a final class be inherited, but a final method can be inherited?

Why? Because final means different things for classes and methods.

Why does it mean different things? Because that is how the Java language designers chose to design the language!

There are three distinct meanings for the final keyword in Java.

  • A final class cannot be extended.

  • A final method cannot be overridden.

  • A final variable cannot be assigned to after it has been initialized.

Why did they decide to use final to mean different things in different contexts? Probably so that they didn't need to reserve 2 or 3 distinct keywords. (In hindsight, that might not have been the best decision. However that is debatable ... and debating it is IMO a waste of time.)

It is worth noting that other keywords have multiple meanings in Java; e.g., static and default (in Java 8).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Final keyword is used to restrict the programmer.

A Final keyword can be a Variable, Method or Class.

When we use the final keyword:

  • with variable we cannot modify the value of the variable.
  • with method we cannot override the method.
  • with class we cannot inherit the class.
Michael
  • 2,631
  • 2
  • 24
  • 40
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 24 '21 at 17:44
-1
public class ParentDemoClass {

    public final void sayHello(){
        System.out.println(" Hello from parent ... PARENT");
    }
}


// Inheritance example to override final method from child class
public class ChildDemoClass extends ParentDemoClass{

    /* When tried to override gives compiler error "Cannot override the
       final method from ParentDemoClass" */

    public void sayHello(){

        System.out.println(" Hello from child ... CHILD");
    }

}

// Inheritance example to access final method from child class

public class ChildDemoClass extends ParentDemoClass{

    public void sayHelloFromChild(){
        System.out.println(" Hello from parent ... CHILD");
        sayHello();
    }

    public static void main(String as[]){
        new ChildDemoClass().sayHelloFromChild();
    }
}

// Output is:
Hello from parent ................................. CHILD
Hello from parent ................................. PARENT

Final method inheritance only allow access to the final method in the child class, but overriding is not allowed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagar Misal
  • 145
  • 1
  • 6