Examples of final class.

Examples of final class.

If you modify a class with a final keyword, it means, this class can not be subclassed. No class can inherit the features of the final class. If you try to this, an error will result. 

Example: 

 package p1;

 public final class FinalClass{public void show(){System.out.println(\"This is a final class.\"); } }

 class FinalSub extends FinalClass{ //errorpublic static void main(String[] args){FinalClass f=new FinalClass();f.show();}} 

 Output: The above program will result an error in compilation like \'can\'t subclassed a final class\'.

package p1; public final class FinalClass{ public void show(){ System.out.println(\"This is a final class.\"); } } class FinalSub extends FinalClass{ //error public static void main(String[] args){ FinalClass f=new FinalClass(); f.show(); } }

By:Jalees
Date:2011-01-09 00:00:00

If you modify a class with a final keyword, it means, this class can not be subclassed. No class can inherit the features of the final class. If you try to this, an error will result. 

Example: 

 package p1;

 public final class FinalClass{public void show(){System.out.println(\"This is a final class.\"); } }

 class FinalSub extends FinalClass{ //errorpublic static void main(String[] args){FinalClass f=new FinalClass();f.show();}} 

 Output: The above program will result an error in compilation like \'can\'t subclassed a final class\'.

package p1; public final class FinalClass{ public void show(){ System.out.println(\"This is a final class.\"); } } class FinalSub extends FinalClass{ //error public static void main(String[] args){ FinalClass f=new FinalClass(); f.show(); } }

By:Jyoti
Date:2028-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher