How To Implement Encapsulation In Java
Rate this article
0 out of 5
In this tutorial we will show you how to implement encapsulation in java. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code
 Encapsulation.java

import java.io.*;
class Encapsulation
{
  //declaring private members
  private String name;
  private int age;
 
  //declaring public members
  public void setName()
  {
    name="John";
  }
  public String getName()
  {
    return name;
  }
  public void setAge()
  {
    age=21;
  }
  public int getAge()
  {
    return age;
  }

  public static void main(String args[])
  {
    //creating object for the class 
    Encapsulation e=new Encapsulation();
    
    //initializing the variables
    e.setName();
    e.setAge();
    
   //getting values from the variables
   System.out.println("Name:"+e.getName());
   System.out.println("Age:"+e.getAge());
  }
}
    


Source Code

About The Author
TekTips Videos

Number Of articles Posted
27
Number Of Messages Posted
0
Joined Date
04/02/2013
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android