0% found this document useful (0 votes)
10 views

Advanced Programing

Uploaded by

Abinet Bizuayehu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Advanced Programing

Uploaded by

Abinet Bizuayehu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Introducing Classes

• The class is at the core of Java.


• It is the logical construct upon which the entire Java
language is built because it defines the shape and
nature of an object.
• As such, the class forms the basis for object-oriented
programming in Java.
• Any concept you wish to implement in a Java program
must be encapsulated within a class.
• The class is so fundamental to Java.
• Here, you will be introduced to the basic elements of a
class and learn how a class can be used to create
objects
Class Fundamentals
• The classes created in the following program primarily exist
simply to encapsulate the main( ) method
• class Example {
• public static void main(String args[]) {
• System.out.println("This is a simple Java program.");
• }
• }
• Perhaps the most important thing to understand about a
class is that it defines a new data type.
• Once defined, this new type can be used to create objects of
that type.
• Thus, a class is a template for an object, and an object is an
instance of a class.
The General Form of a Class
• When you define a class, you declare its exact
form and nature.
• You do this by specifying the data that it contains
and the code that operates on that data.
• While very simple classes may contain only code
or only data, most real-world classes contain
both.
• A class is declared by use of the class keyword
Continued…
• The general form of a class definition is shown here:
• Class classname{
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;

type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
• }
• }
Continued…
• The data, or variables, defined within a class are
called instance variables.
• The code is contained within methods.
• Collectively, the methods and variables defined
within a class are called members of the class.
• In most classes, the instance variables are acted
upon and accessed by the methods defined for
that class.
• Thus, it is the methods that determine how a
class’ data can be used
Continued…
• Variables defined within a class are called
instance variables because each instance of
the class (that is, each object of the class)
contains its own copy of these variables.
• Thus, the data for one object is separate and
unique from the data for another.
Continued…
• All methods have the same general form as main( ),
which we have been using so far.
• However, most methods will not be specified as
static or public.
• Notice that the general form of a class does not
specify a main( ) method.
• Java classes do not need to have a main( ) method.
• You only specify one if that class is the starting point
for your program.
• Further, applets don’t require a main( ) method at
all.
A Simple Class
• Here is a class called Box that defines three
instance variables: width, height, and depth.
• Currently, Box does not contain any methods
• class Box {
double width;
double height;
double depth;
• }
Continued…
• As stated, a class defines a new type of data.
• In this case, the new data type is called Box.
• You will use this name to declare objects of
type Box.
• It is important to remember that a class
declaration only creates a template; it does
not create an actual object.
• Thus, the preceding code does not cause any
objects of type Box to come into existence
Continued…
• To actually create a Box object, you will use a
statement like the following:
• Box mybox = new Box(); // create a Box object
called mybox
• After this statement executes, mybox will be an
instance of Box.
• Thus, it will have “physical” reality.
• When you create an instance of a class, you are
creating an object that contains its own copy of
each instance variable defined by the class.
Continued…
• Thus, every Box object will contain its own copies
of the instance variables width, height, and
depth.
• To access these variables, you will use the dot (.)
operator.
• The dot operator links the name of the object
with the name of an instance variable.
• For example, to assign the width variable of
mybox the value 100, you would use the
following statement:
• mybox.width = 100;
Continued…
• This statement tells the compiler to assign the
copy of width that is contained within the
mybox object the value of 100.
• In general, you use the dot operator to access
both the instance variables and the methods
within an object.
Continued…
• Here is a complete program that uses the Box
class:
• // A program that uses the Box class. Call this
file BoxDemo.java
• class Box {
• double width;
• double height;
• double depth;
• }
Continued…
• // This class declares an object of type Box.
• classBoxDemo {
• public static void main(String args[]) {
• Box mybox = new Box();
• double vol;

• // assign values to mybox's instance variables
• mybox.width = 10;
• mybox.height = 20;
• mybox.depth = 15;

• // compute volume of box
• vol = mybox.width * mybox.height * mybox.depth;
• System.out.println("Volume is " + vol);
• }
• }
Continued…
• You should call the file that contains this
program BoxDemo.java, because the main( )
method is in the class called BoxDemo, not
the class called Box.
• When you compile this program, you will find
that two .class files have been created, one for
Box and one for BoxDemo.
• The Java compiler automatically puts each
class into its own .class file
Continued…
• It is not necessary for both the Box and the
BoxDemo class to actually be in the same source
file.
• You could put each class in its own file, called
Box.java and BoxDemo.java, respectively.
• To run this program, you must execute
BoxDemo.class.
• When you do, you will see the following output:
• Volume is 3000.0
Continued…
• Each object has its own copies of the instance
variables.
• This means that if you have two Box objects,
each has its own copy of depth, width, and
height.
• It is important to understand that changes to
the instance variables of one object have no
effect on the instance variables of another.
• For example, the following program declares
two Box objects:
Continued…
• // This program declares two Box objects.
• class Box {
• double width;
• double height;
• double depth;
• }
• class BoxDemo2 {
• public static void main(String args[]) {
• Box mybox1 = new Box();
• Box mybox2 = new Box();
• double vol;
Continued…
• // assign values to mybox1's instance variables
• mybox1.width = 10;
• mybox1.height = 20;
• mybox1.depth = 15;

• // assign different values to mybox2's instance variables
• mybox2.width = 3;
• mybox2.height = 6;
• mybox2.depth = 9;

• // compute volume of first box
• vol = mybox1.width * mybox1.height * mybox1.depth;
• System.out.println("Volume is " + vol);

• // compute volume of second box
• vol = mybox2.width * mybox2.height * mybox2.depth;
• System.out.println("Volume is " + vol);
• }
• }
Continued…
• The output produced by this program is shown
here:
• Volume is 3000.0
• Volume is 162.0
• As you can see, mybox1’s data is completely
separate from the data contained in mybox2.
Declaring Objects
• When you create a class, you are creating a new
data type.
• You can use this type to declare objects of that type.
• However, obtaining objects of a class is a two-step
process.
• First, you must declare a variable of the class type.
• This variable does not define an object. Instead, it is
simply a variable that can refer to an object.
• Second, you must acquire an actual, physical copy of
the object and assign it to that variable
Continued…
• You can do this using the new operator.
• The new operator dynamically allocates (that is, allocates
at run time) memory for an object and returns a
reference to it.
• This reference is, more or less, the address in memory of
the object allocated by new.
• Box mybox = new Box();
• This statement combines the two steps just described.
• It can be rewritten like this to show each step more
clearly:
• Box mybox; // declare reference to object
• mybox = new Box(); // allocate a Box object
Continued…
• The first line declares mybox as a reference to an object of
type Box.
• After this line executes, mybox contains the value null,
which indicates that it does not yet point to an actual
object.
• Any attempt to use mybox at this point will result in a
compile-time error.
• The next line allocates an actual object and assigns a
reference to it to mybox.
• After the second line executes, you can use mybox as if it
were a Box object.
• But in reality, mybox simply holds the memory address of
the actual Box object.
Continued…
• The effect of these two lines of code is
depicted in the following figure.
A Closer Look at new
• As just explained, the new operator dynamically allocates
memory for an object.
• It has this general form:
– class-var= new classname( );
• Here, class-var is a variable of the class type being created.
• The classname is the name of the class that is being
instantiated.
• The class name followed by parentheses specifies the
constructor for the class.
• A constructor defines what occurs when an object of a class is
created.
• Constructors are an important part of all classes and have
many significant attributes
Continued…
• Most real-world classes explicitly define their own
constructors within their class definition.
• However, if no explicit constructor is specified,
then Java will automatically supply a default
constructor.
• This is the case with Box.
• It is important to understand that new allocates
memory for an object during runtime.
• The advantage of this approach is that your
program can create as many or as few objects as it
needs during the execution of your program.
Continued…
• The distinction between a class and an object.
• A class creates a new data type that can be used
to create objects.
• That is, a class creates a logical framework that
defines the relationship between its members.
• When you declare an object of a class, you are
creating an instance of that class.
• Thus, a class is a logical construct.
• An object has physical reality. (That is, an object
occupies space in memory.)
Assigning Object Reference Variables
• Object reference variables act differently than you
might expect when an assignment takes place.
• For example, what do you think the following fragment
does?
• Box b1 = new Box();
• Box b2 = b1;
• You might think that b2 is being assigned a reference to
a copy of the object referred to by b1.
• That is, you might think that b1 and b2 refer to
separate and distinct objects
Continued…
• However, this would be wrong.
• Instead, after this fragment executes, b1 and b2 will
both refer to the same object.
• The assignment of b1 to b2 did not allocate any
memory or copy any part of the original object.
• It simply makes b2 refer to the same object as does
b1.
• Thus, any changes made to the object through b2
will affect the object to which b1 is referring, since
they are the same object.
Continued…
• This situation is depicted here:
Continued…
• Although b1 and b2 both refer to the same object, they
are not linked in any other way.
• For example, a subsequent assignment to b1 will simply
unhook(detach)b1 from the original object without
affecting the object or affecting b2.
• For example:
• Box b1 = new Box();
• Box b2 = b1;
• // ...
• b1 = null;
• Here, b1 has been set to null, but b2 still points to the
original object.
Example
• Class Account{
• Int accno;
• Int balance;
• Int a;
• Double balance;
• }
• Public class Demo{
• Public static void main(String[] args){
• Account a1, a2;
• A1= new Account();
• A2=A1;
• }
Continued…

You might also like