ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
What is meant by Encapsulation? Can you write a class to
explain encapsulation?
 Question Submitted By :: Maria
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 1
Encapsulation is process binding data with an object.  
Object consists data and methods, where methods helps to 
extract or set data. 

  All classes developed using OOPS concepts follow the 
encapsulation.
 
Is This Answer Correct ?    6 Yes 1 No
Bln
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 2
It is the ability of an object to hide its data and methods 
from the rest of the world.

A Class may contains a few members in the form of 
properties,events,fields or methods.These members of the 
class are not exposed to the outer world directly.Rather 
they are ENCAPSULATED by the Class.

Public Class Calculations
{
     Private void fnMultiply(int x, int y)
     {
        return x*y;
     }
}

Calculations Obj;
int Result;
Result = Obj.fnMultiply(5,10);
 
Is This Answer Correct ?    5 Yes 2 No
Rajasekhar
 
 
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 3
-Encapsulation is wrapping of data(Data members) and
associated methods(member functions) into a single unit in 
such a way that data can be accessed with the help of
associated methods.
--In a class we can specify the variables(Data members) as
private. So for other classes it will not be accessible. 
--Through methods(member functions) only we can access the
member variables or modify them. 
--It gives more security for our data. It is nothing but
data hiding.
 
Is This Answer Correct ?    5 Yes 0 No
Sivakishorereddy(badvel)
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 4
Wraping of objects under a class is known as Encapsulation.

class
{
public static void main(String a[])
{
int a,b,
....
....
}
}
 
Is This Answer Correct ?    0 Yes 0 No
Ajay Kumar Sharma
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 5
Binding the Function with its associated data together is
called encapsulation(Tough definition but here is example also)

Think this senario
public class Anjani{
public int size;
public int weight;
...
}
public class Jha {
public static void main (String [] args) {
Anjani a = new Anjani();
a.size = -5; // Legal but bad!!
}
}

if somebody change ur size from -5 to -10; so sad ..........
And now you're in trouble. How are you going to change the
class in a way that
lets you handle the issues that come up when somebody
changes the size variable
to a value that causes problems? Your only choice is to go
back in and write method
code for adjusting size (a setSize(int a) method, for
example), and then
protect the size variable with, say, a private access
modifier. But as soon as you
make that change to your code, you break everyone else's!

The ability to make changes in your implementation code
without breaking the
code of others who use your code is a key benefit of
encapsulation.

Now how u will do this............

Keep instance variables protected (with an access modifier,
often private).
and Make public accessor methods, and force calling code to
use those methods
rather than directly accessing the instance variable.
and For the methods, use the JavaBeans naming convention of
set<someProperty> and get<someProperty>.


so how u will write the code........

full code of incapsulation

class Anjani{
private int size;
public int setSize(int size)
{
    this.size=size;

return size;

}
public int getSize()
{
    this.size=size;
return size;
}

}
public class jha {
public static void main (String [] args) {
Anjani a = new Anjani();
int x=a.getSize();
a.setSize(10);
System.out.println(a.setSize(10));
}
}

Thanks and Regards
Anjani Kumar Jha
09623154095
CDAC PUNE
 
Is This Answer Correct ?    1 Yes 0 No
Anjani Kumar Jha
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 6
All the javabeans written in java follows encapsulation.
In java bean variables are declared as private for 
security. will be accessed using setter and getter method.
 
Is This Answer Correct ?    0 Yes 1 No
Srinivasa
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 7
Encapsulation=DataHidding+Abstraction is called
Encapsulation is called Encapsulation

EX:-
public class A
{
  private String name;
  private  int age;
publiic setName(String name)
{
  this.name=name;
}
public String getName()
{
  return name;
}
public setAge(int age)
{
  this.age=age;
}
public int age()
{
  return age;
}
}
 
Is This Answer Correct ?    0 Yes 0 No
Srinu
 
  Re: What is meant by Encapsulation? Can you write a class to explain encapsulation?
Answer
# 8
hai thanks for giving excellent answers...
 
Is This Answer Correct ?    0 Yes 0 No
Vinoth
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
what are depricated methods in threads and explain the lifecycle methods  5
what are depricated methods ? Satyam4
steps to connect with Oracle Databse using TYPE-2 Jdbc driver.  1
what is difference Between Core Java and advance java HCL17
is there any function in java to make the text to blink?  5
If I have 1000 objects and my requirement is to sort them quickly, then which collection would you recommend and why? KPIT2
Are nested try statements are possible?  2
What is the specification of ?CODEBASE? in an applet?  1
wHAT IS DEFAULT SPECIFIER IN JAVA wHAT IS DEFAULT CONSTRUCTOR IN JAVA wHAT IS DEFAULT METHOD IN JAVA IBM10
who was the founder of java HCL7
Difference between Web-based applications,Client- Server applications and Distributed applications? Infosys2
How to override a equals() method and what is the use?  1
String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ? Flextronics5
I have an HashMap object, which has with key and value pair. It has 10 keys and values in that object. Now the question is I want insert new key and value in middle or any where in that list but not at the end or at the top. Is it possible or not. If yes how can we achieve this one? Huawei1
how can u handle run time exception in java? explain with brief explanation with examples? CTS2
what is the purpose of the final in the try-catch-final  5
How many methods does cloneable interface contains?  2
How do u provide security in java  1
can we create instance for interface in java?  2
how many ways we can serialize the java object? Satyam1
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com