The Artima Developer Community
Sponsored Link

Java Buzz Forum
Program on encapsulation

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.
Program on encapsulation Posted: Mar 5, 2015 9:28 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Program on encapsulation
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
  • Binding the data with its related functionalities known as encapsulation
  • Here data means variables and functionalities means methods.
  • So keeping the variable and related methods in one place.
  • That place is "class". class is the base for encapsulation.
  • Lets see example program on encapsulation, how the variables and methods defined in a class.

  1. package com.instanceofjava;
  2.  
  3. public class EncapsulationDemo {
  4.  
  5.   String name;
  6.   int rno;
  7.   String address;
  8.  
  9. public String getName() {
  10.        return name;
  11.  }
  12.  
  13. public void setName(String name) {
  14.         this.name = name;

  15. public int getRno() {
  16.        return rno;
  17. }
  18.  
  19. public void setRno(int rno) {
  20.      this.rno = rno;
  21. }
  22.  
  23. public String getAddress() {
  24.         return address;
  25. }
  26.  
  27.  public void setAddress(String address) {
  28.     this.address = address;
  29. }
  30.  
  31. public void showInfo(){
  32.  
  33.       System.out.println("Name: "+getName());
  34.       System.out.println("R.No: "+getRno());
  35.       System.out.println("Name: "+getAddress());
  36.  
  37.     }
  38. }

Class:

  • The above example program shows how the variables and methods will be there in a class.
  • So class is the base for encapsulation
  • class is user defined data type in java means by using class we can structure our own programs.
  • Lest see a example program on class.

  1. package com.instanceofjava;
  2.  
  3. public class ClassDemo {
  4.  
  5.     //variables of a class
  6.    String empName;
  7.    int empId;
  8.    String Designation;
  9.  
  10.  //getter and setter methods for variables 
  11.  //by using these methods we can set the value to the variable and gat the value from the
  12.  //variables
  13.  
  14.  public String getEmpName() {
  15.         return empName;
  16. }
  17.  
  18.  public void setEmpName(String empName) {
  19.       this.empName = empName;
  20. }
  21.  
  22. public int getEmpId() {
  23.        return empId;
  24. }
  25.  
  26. public void setEmpId(int empId) {
  27.         this.empId = empId;
  28. }

  29. public String getDesignation() {
  30.         return Designation;
  31. }
  32.  
  33.  public void setDesignation(String designation) {
  34.         Designation = designation;
  35. }

  36. }

Object:

  • Instance of class is known as object.
  • Instance means dynamic memory allocation. So dynamic memory allocation to class is called object.
  • By using "new" keyword the object will be created dynamically.
  • Class is a template. By using object we will get memory for all variables so that we can use them.
  • We can create number of objects for one class based on our requirements.
  • Below an example program on class and object.


  1. package com.instanceofjava; 
  2.  
  3. public class User {
  4.  
  5.    String Name;
  6.     int Id;
  7.     String address;
  8.  
  9.  public String getName() {
  10.      return Name;
  11. }
  12.  
  13. public void setName(String name) {
  14.         Name = name;
  15. }
  16.  
  17. public int getId() {
  18.       return Id;
  19. }
  20.  
  21. public void setId(int id) {
  22.        Id = id;
  23. }
  24.  
  25. public String getAddress() {
  26.         return address;
  27. }
  28.  
  29. public void setAddress(String address) {
  30.         this.address = address;
  31. }
  32.  
  33. public static void main(String [] args){
  34.  
  35.        User obj= new User();
  36.  
  37.         obj.setName("sai");
  38.         obj.setId(1);
  39.         obj.setAddress("xyz");
  40.  
  41.         System.out.println(obj.getName());
  42.         System.out.println(obj.getId());
  43.         System.out.println(obj.getAddress());
  44.  
  45.     }
  46. }

Output:

  1. sai
  2. 1
  3. xyz

Read: Program on encapsulation

Topic: Head first elastic search on java with spring boot and data features Previous Topic   Next Topic Topic: Java Bootstrap: Dropwizard vs. Spring Boot

Sponsored Links



Google
  Web Artima.com   

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