Java Program to Demonstrate Example of Final Class

You are currently viewing Java Program to Demonstrate Example of Final Class

Java program to demonstrate example of final class

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();
    }
}
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

/FinalClassExample.java:9: error: cannot inherit from final Base
public class FinalClassExample extends Base {
                                       ^
1 error
I'm displayMsg() in Base class.
I'm displayMsg1() in Final class.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply