Thursday, October 15, 2015

How To Use Access Specifier in Java Programming Language

How To Use Access Specifier in Java Programming Language

The Following script is the simplest way to declaring and using Access Specifier in Java programming language:

Class diagram:


Java program:

class Titik{ //titik = point = dot
 private int X;
 private int Y;

 protected void setX(int X){
  this.X = X;
 }

 public int getX(){
  return X;
 }

 protected void setY(int Y){
  this.Y = Y;
 }

 public int getY(){
  return Y;
 }
}

class Titik3D extends Titik{ //titik3D = 3Dpoint
 private int Z;

 protected void setZ(int Z){
  this.Z = Z;
 }

 public int getZ(){
  return Z;
 } 
}

class AccessSpecifier{
 public static void main(String args[]){
  Titik T2D = new Titik();
  Titik3D T3D = new Titik3D();

  T2D.setX(10);
  T2D.setY(20);
  System.out.println("Titik 2D ("+ T2D.getX() +","+ T2D.getY() +")");

  T3D.setX(10);
  T3D.setY(20);
  T3D.setX(30);
  System.out.println("Titik 3D ("+ T3D.getX() +","+ T3D.getY() +","+ T3D.getZ() +")");
 }
}

Output:

ShadowOfBdg© - All rights reserved. 

No comments:

Post a Comment