Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.
encapsulation in java
We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter...
Today we shall discuss about a very important object oriented concept, it is called
Encapsulation.So, our today’s topic is Encapsulation in Java. As we know the
concept of class is abasic things in any object oriented programming. So, we have
to write program in Java thismeans that we have to write a set of classes. All
classes then can be used to create objectsand this way the object orientation is
possible. So, today we will discuss about the basicconcept of class which basically
can be achieved with the concept of object oriented principlecalled encapsulation,
and the different elements that is possible in a class.So, first let us clear the
concept of class in Java.As I told you that class is a basic building blocks in any
Java program that mean usingthe class you can write the entire Java programs.So,
class basically what exactly a class is? So, a class in fact, is a template; that
means,it is basically gives a framework of an object. Now, how an object can be
described? So, inorder to describe an object we need many elements for that object.
So, the elements which arevery important in any objects are called fields, the
methods, constructor and sometimes specialitems called blocks and also class within
a class called the nested class. And anotherimportant concept also can be included
in an object is called the interface.So, mainly there are 6 different items to be
incorporated in a class. Now fields andmethods little bit we have familiarity in
our last few programs, but other 3 thingslike constructors and then interface and
everything is not known to us at the moment. So, we shallon all this thing slowly.
In today we will basically discussed or emphasized on fieldmethods and constructors
these 3 things.So, now a basic structure in its simplest form will look like which
is shown here. Aswe see in this fragments of the programs is a basically pseudo
program not exactly theprogram. So, any class can be defined with its own unique
name.So, it is called the class name usually it is given by the user, so user
defined name.So here so this is the name of the class that you have you are going
to build, so theseare name. And then it consists of 2 things which is mentioned
here as you see a set ofit is called the fields or member elements. All are member
elements method also a memberelements the particularly it is called fields and
other is called the methods.So, fields and methods are 2 very important things in
any class. A class can consist of1 or 0 or more methods and 0 or more fields. There
is quite possible that a class doesnot have any fields also possible that a class
does not have any methods, but which is reallynot useful is a useless thing without
any fields are any methods anyway.So, logically a class have 0 or more member
elements fields and methods. So, fields isbasically simplest of his form is
basically reference variables or some primitive variablesthat means objects and
other name of the other variables that can be define and then methodsare basically
the operations which are possible to manipulate all the fields variables arethere.
So, these are basically called data in a class and these are basically
calledoperations in a class. So, these are the 2 things data and operations when
they are punchedtogether it is call encapsulation which you have a little bit learn
in our previous lectures.So, here encapsulation means data and operation are to be
put together.So, this is a basic structure of the class and let us have a very
simple example. Nowsuppose a circle, a circle is an object. So, there are number of
objects all the objectsare different, because of its own centre location as well as
its radius, the 2 circles are 2objects, they are having either centre location or
radius or both are different.So, such an object can be defined by its own class.
So, here is an example how a classof type circle for you look like this. So, this
basically if this is the name of theclass a circle in this case the circle has 2
fields, one is declared as float type theradius r and another 2 fields are also
declared as float type x and y namely the coordinatesof the centre. So, here as we
see a circle is defined interms of 2 data, centre location x and y and then it is
radius. So, this basically completesthe definition of a circle. However, in this
definition we have not included any method,if we in if we include some methods in
it, it will basically makes a sense. Now let ussee how the different method that is
possible in this definition.So, here the circle class that we have discuss just now
has been augmented with 2 methods.The name of the methods as we see here one method
is called circumference another methodis called area. This means these are the 2
operations which can work with if the radiusis given to this method. So, they will
, these 2 methods will be able to calculate the circumferenceof the circle using this
is a formula and area of the circle using the standard formulaSo, here of course, r
is in use; however, other 2 variables mainly x and y is not inuse. So, we can pair
some other methods to utilizex, y as well as both all x, y and are there any others
details description of this thingswill take the program little bit lengthy, we just
now ignore it. And we will discussa detail usage of all member elements, all fields
in using some other operations or methods.Anyway, so this basically gives one a
form of a class called the Circle having 3 differentfields x , y ,r, 2 methods
namely circumference and area. So, this way we can define the objectthe class
namely Circle here in this case.Now, let us see what the uses of this class is. Now
once you declare a class Circle, weshall be able to use to declare its objects. So,
suppose this is the definition, supposethis is the definition of your class which
we have already discussed it and then we canuse it to create objects. So, for these
things we can create our main class program. So,here is the idea about the main
class program we dip. So, this is the class defining thecircle category and here is
another class we are defining which our program is.So, here we are defining the
program. The name of the program is the name of the programin this case is circle
demo 1. So, we have given the name of r 1 and we have alreadyfamiliar to the main
class declaration by virtue of public static void main String args.So, this is the
usual statement to declare a class as a main class using the main method,and here
you can see we have we have declared one object. We give the name of the objectas c
and the new is basically the usual operator who is basically create an objects of
typecircle. So, this is the standard sentence that youshould follow to create an
object of type in this case for example, of type class. So,the object namely c is
created here, this object c is created, once c is created wecan use its different
member elements, namely the different fields and then also its method.Now here we
see the c.x basically implies that it is the element x for the object clikewise
c.y, c.r and the values for this fields has been initialized as 0.0 for x 0.0for y
5.0 for r. So, this basically gives an initialization of an objects why are
thecentre is located 0 0 and having the radius 5. So, this way an object is now
created withits value in it. Once the object is created then we can use its method
to do certain operation.For example, in this case we use c dot circumference that
means, for the object c we call the methodcircumference; that means, to return the
result of its circumference.So, whenever we call it, it basically use or use this
code to calculate the circumferenceof a circle whose radius is r. In this case it
will calculate the circumference of thecircle whose radius is 5. Similarly, in the
next statement it will calculate area of thesame circle c and the result will be
printed using System.out.println().So, this is a one method, this is a one class
call the main class who is basically createsan object of type circle and the name
of the object is c and for this object we can accessthe different elements ,fields
and methods. Methods are basically operations on the datawhich is there in that
objects. So, this way we can use a create objects. Now with so,in this example, we
have created only 1 objects, but it is quite possible to create the multipleobjects
having a class definition ready.So, in this example we can see how we can create
more than one objects using the sameconcept that we have learned just now. So, here
if we see c1 and c2 are the 2 objectscreated a using this concept new and that
these are the 2 objects of type circle. Andso, here we see these the initialization
of the first circle having centre at 3.0 and4.0 and radius 5.0 and this is the
initialization of the another circle whose centre is at minus4.0 minus 8.0 and
radius is 10. So, the 2 circles are now built, 2 circles having the2 centers 2
different radius have been built. So, the 2 circles are created.Now, once the 2
circles are created, we can call the methods in them. So, for example,as we see in
these 2 statements System.out.println()., we use the circumference method to
returnthe circumference of the circle c1. In this method we see area of the circle
c one inanother statement we use the same thing, but for the circle c2. So, we just
now learn abouthow the multiple objects in this case 2 objects can be created and
all their member elementscan be accessed in from the program. So, this is the
concept about creating multipleobjects. And now we will discuss about if we can
include more than one classes in aprogram .So, our next example is towards
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller siddukarthik. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.49. You're not tied to anything after your purchase.