The Artima Developer Community
Sponsored Link

Java Buzz Forum
Final method in java with example programs

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.
Final method in java with example programs Posted: Mar 18, 2017 11:26 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Final method in java with example programs
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
  • If we declare any method as final by placing final keyword then that method becomes final method.
  • The main use of final method in java is they are not overridden.
  • We can not override final methods in sub classes.
  • If we are using inheritance and we need some methods not to overridden in sub classes then we need make it final so that those methods can not be overridden by sub classes. 
  • We can access final methods in sub class but we can not overridden final methods.



Defining a final method in java:

  • Add final keyword to the normal method then it will become final method.
  1. public final void method(){
  2. //code
  3.  }

What happens if we try to override final methods in sub classes?


#1 : Java program to explain about final method in java

  1. package inheritance;
  2. /**
  3.  * final methods in java with example program
  4.  * @author www.instanceofjava.com
  5.  */
  6. public class A {
  7.  
  8.     int a,b;
  9.     
  10.     public final void show(){
  11.         System.out.println("A class show method");
  12.     }
  13. }

final method in java with example program

Can we access final methods in sub classes?

  • Yes we can access final methods in sub classes.
  • As mentioned above we can access all super class final methods in sub class but we can not override super call final methods.

#2 : Java program to explain about final method in java

  1. package inheritance;
  2. /**
  3.  * final methods in java with example program
  4.  * @author www.instanceofjava.com
  5.  */
  6. public class A {
  7.  
  8.     int a,b;
  9.     
  10.     public final void show(){
  11.         System.out.println("A class show method");
  12.     }
  13. }

  1. package inheritance;
  2.  
  3. /**
  4.  * final methods in java with example program
  5.  * @author www.instanceofjava.com
  6.  */
  7.  
  8. public class B {
  9.     
  10.  public static void main(String[] args){
  11.         
  12.         B obj = new B();
  13.         obj.show();
  14.        
  15.     }
  16. }

Output:
  1. A class show method

Top 10 Java interview questions on final keyword 

Static method vs final static method in java with example programs  

Final static string vs Static string in java  

Read: Final method in java with example programs

Topic: 17% off Ecobee3 Lite Wi-Fi Alexa Compatible Smart Thermostat - Deal Alert Previous Topic   Next Topic Topic: Webinar Recording: Composite Builds with Gradle and IntelliJ IDEA 2017.1

Sponsored Links



Google
  Web Artima.com   

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