×

Java Programs

Java Practice

Java program to demonstrate example of final class

Final Class: A final class can not be inherited/extended.

Program:

import java.util.*;

final class Base {
    public void displayMsg() {
        System.out.println("I'm displayMsg() in Base class.");
    }
}

public class FinalClassExample extends Base {
    public void displayMsg1() {
        System.out.println("I'm displayMsg1() in Final class.");
    }

    public static void main(String[] s) {
        FinalClassExample FCE = new FinalClassExample();
        FCE.displayMsg();
        FCE.displayMsg1();
    }
}

Output

/FinalClassExample.java:9: error: cannot inherit from final Base
public class FinalClassExample extends Base {
                                       ^
1 error

After removing final keyword from Base class

import java.util.*;

class Base {
    public void displayMsg() {
        System.out.println("I'm displayMsg() in Base class.");
    }
}

public class FinalClassExample extends Base {
    public void displayMsg1() {
        System.out.println("I'm displayMsg1() in Final class.");
    }

    public static void main(String[] s) {
        FinalClassExample FCE = new FinalClassExample();
        FCE.displayMsg();
        FCE.displayMsg1();
    }
}

Output

I'm displayMsg() in Base class.
I'm displayMsg1() in Final class.

Java Final Variable, Class, & Method Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.