The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 10 Java interview questions on final keyword

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 10 Java interview questions on final keyword Posted: Mar 3, 2016 3:19 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 10 Java interview questions on final keyword
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1. What is the use of final keyword in java?  


  • By using final keyword we can make
  • Final class
  • Final method
  • Final variables
  • If we declare any class as final we can extend that class
  • If we declare any method as final it can not be overridden in sub class
  • If we declare any variable as final its value unchangeable once assigned.

2. What is the main difference between abstract method and final method?

  • Abstract methods must be overridden in sub class where as final methods can not be overridden in sub class
3. What is the actual use of final class in java?

  • If a class needs some security and it should not participate in inheritance in this scenario we need to use final class.
  • We can not extend final class.
4. What will happen if we try to extend final class in java?
  • Compile time error will come.
  1. package com.finalkeywordintweviewprograms;
  2.  public final  Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }


  1. package com.finalkeywordintweviewprograms;
  2.  public Class Sample  extends SuperDemo{  //The type Sample cannot subclass the final class
  3. SuperDemo

  4. }

5.Can we declare interface as final?

  • No We can not declare interface as final because interface should be implemented by some class so its not possible to declare interface as final.
final keyword interface in java


6. Is it possible to declare final variables without initialization?

  • No. Its not possible to declare a final variable without initial value assigned.
  • While declaring itself we need to initialize some value and that value can not be change at any time.

final variable in java
  1. package com.finalkeywordintweviewprograms;
  2. public final  Class Sample{ 
  3.  
  4. final int x=12,y=13;
  5.  
  6. public void Method() {

  7. x=25;// compile time error:The final field Super.x cannot be assigned
  8. y=33;// compile time error: The final field Super.y cannot be assigned
  9.  
  10. }
  11.  
  12. }

7. Can we declare constructor as final?
  • No . Constructors can not be final.

8.What will happen if we try to override final methods in sub classes?

  • Compile time error will come :Cannot override the final method from Super class
9.Can we create object for final class?
  • Yes we can create object for final class. 
10.What is the most common predefined final class object you used in your code? 
  • String (for example)

Read: Top 10 Java interview questions on final keyword

Topic: Vaadin GridLayout Example Previous Topic   Next Topic Topic: Data types in java with example programs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use