Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Class modifiers in java

Posted By: Fern Sanchez     Category: Java     Views: 3007

This article explains about different class modifiers available in java with examples.

There are three possible modifiers that may precede the class keyword as listed below.

 Keyword

Description 

 abstract

Cannot be instantiated  

 final

Cannot be extended 

 public

Can be accessed by any other class. If this keyword is missing, access to the class is limited to the current package. 



Syntax of class with abstract modifier

abstract class clsName
{
      // body of class
}


Syntax of class with final modifier

final class clsName
{
      // body of class
}


Syntax of class with public modifier

public class clsName
{
      // body of class
}


Examples with different class modifiers

Example 1 : Program that illustrates how to use abstract class in java using class shape

abstract class Shape
{
       void display()
       {
       }   
}

class Circle extends Shape
{
       void display()
       {
              System.out.println("You are using circle class");
       }
}

class Rectangle extends Shape
{
       void display()
       {
              System.out.println("You are using rectangle class");
       }
}

class Triangle extends Shape
{
       void display()
       {
              System.out.println("You are using triangle class");
       }
}

class AbstractClassDemo
{
       public static void main(String args[])
       {
               Shape sObj = new Circle();
               sobj.display();
               
               sObj = new Rectangle();
               sobj.display();

               sObj = new Triangle();
               sobj.display();
       }
}


Output 
You are using circle class
You are using rectangle class
You are using triangle 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);
   }
}


Output

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

Example 3 : Program that illustrates public class in java using DataStructure class

public class DataStructure // you can use this class instance in any package
{
    private final static int size = 15;
    int[] arrOfNum = new int[size];
    
    public DataStructure() 
    {
        for (int i = 0; i < size; i++) 
        {
            arrOfNum[i] = i;
        }
    }
    
    public void display() 
    {    
        for (int i = 0; i < size; i++) 
        {
            System.out.println(arrOfNum[i]);
        }
    }
}

class PublicClassDemo 
{
    public static void main(String args[]) 
    {
        DataStructure dsObj = new DataStructure();
        ds.display();
    }
}


Output
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

  
Share: 


Didn't find what you were looking for? Find more on Class modifiers in java Or get search suggestion and latest updates.

Fern Sanchez
Fern Sanchez author of Class modifiers in java is from Los Angeles, United States.
 
View All Articles

 
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!