Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Final class

Posted By: Cindy Fisher     Category: Java     Views: 19380

This article explains about final class with example in java.

Final class cannot be extended. Final classes are created so the methods implemented by that class cannot be overridden. Best example is Math final class of java. If a class is final, all its methods are also final.

Syntax of final class

final class clsName
      // body of class

Examples of final class

Example 2 : Program that illustrates how to use final class in java using Colored3dPoint class

class Point 
     int x, y; 

class ColoredPoint extends Point 
     int color; 

// Colored3dPoint class cannot be extended further

final class Colored3dPoint extends ColoredPoint 
     int z; 

class FinalClassDemo
   public static void main(String args[])
         Colored3dPoint cObj = new Colored3dPoint();
         cObj.z = 10;
         cObj.color = 1;
         cObj.x = 5;
         cObj.y = 8

         System.out.println("x = " + cObj.x);
         System.out.println("y = " + cObj.y);
         System.out.println("z = " + cObj.z);
         System.out.println("Color = " + cObj.color);


x = 5
y = 8
z = 10
Color = 1


Didn't find what you were looking for? Find more on Final class Or get search suggestion and latest updates.

Cindy Fisher
Cindy Fisher author of Final class is from Wichita, United States.
View All Articles

Related Articles and Code:

Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

No Comment Found, Be the First to post comment!