Chapter 3
Chapter 3
• Defining Class
• A class is defined by use of the class keyword.
class <classname>
{
<body of the class>
}
3.1.1 Defining Classes, Fields and Methods
• Defining Class
• The 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.
3.1.1 Defining Classes, Fields and Methods
• Defining Class
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
}
}
3.1.1 Defining Classes, Fields and Methods
• Defining Class
• Example:
class Test
{
int i;
void display()
{
System.out.print(“ i = “ + i);
}
}
3.1.1 Defining Classes, Fields and Methods
• Defining Class
• when you create a class, you are creating a new data type. You can use this
type to declare objects of that type.
3.1.2 Creating Objects
1. You need to 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.
2. You need to create an actual, physical copy of the object and assign it to
that variable. You can do this using the new operator.
• The new operator dynamically (run time) allocates memory for an object
and returns a reference to it.
Test obj;
null
obj
2. Write a program in JAVA that implements the class Box with instance
variables length, width, height and calculate the volume of the Box using
class method volume.
3. Write a program in JAVA that implements the class Box with instance
variable length, width, height and method setdata to assign the value to
these attributes and also calculate the volume of the Box using class
method volume.
Exercise (1)
class Test
{
int i;
void display()
{
System.out.print(“ i = “ + i);
}
}
Exercise (1)
public class Example
{
public static void main(String[] args)
{
Test obj1 = new Test();
obj1.i = 5;
obj1.display();
}
}
Output: i = 5
Exercise (2)
class Box
{
int length;
int width;
int height;
b1.length=2;
b1.width = 3;
b1.height = 5;
b1.volume();
}
}
b1.setdata(2,3,5);
b1.volume();
}
}
class Protection
{
}
3.1.3 Accessing Rules
}
}
Output:
n_pub = 1
n_pro = 2
n=3
3.1.4 this keyword
• Sometimes a method will need to refer to the object that invoked it.
• this keyword can be used inside any method to refer to the current object.
• That is, this is always a reference to the object on which the method was
invoked.
3.1.4 this keyword
class Box
{
int length;
int width;
int height;
b1.setdata(2,3,5);
b1.volume();
}
}
void getdata()
{
System.out.println("n=" + n);
}
}
3.1.4 this keyword
Output: n=0
3.1.4 this keyword
void getdata()
{
System.out.println("n=" + n);
}
}
3.1.4 this keyword
void getdata()
{
System.out.println("n=" + n);
}
}
3.1.4 this keyword
public class Example
{
public static void main(String[] args)
{
Test obj = new Test();
obj.setdata(5);
obj.getdata();
}
}
Output: n=5
3.1.5 static keyword
• Outside of the class in which they are defined, static methods and
variables can be used independently of any object.
• To do so, you need only specify the name of their class followed by the dot
operator :
• classname.method( )
• classname.variable
3.1.5 static keyword
• Static Variable:
• Instance variables declared as static are, essentially, global variables.
When objects of its class are declared, no copy of a static variable is
created.
• Instead, all instances of the class share the same static variable.
• Static Method:
• Methods declared as static have several restrictions:
■ They can only call other static methods.
■ They must only access static data.
■ They cannot refer to this or super.
3.1.5 static keyword
• Example:
class StaticDemo
{
static int a = 42;
Output:
a = 42
b = 99
3.1.5 static keyword
• Static Block:
• Static block is a set of statements, which will be executed by the JVM
before execution of main method.
static
{
a=42;
}
}
3.1.5 static keyword
• Example (Continue):
Output:
a = 42
3.1.6 final keyword
• In java language final keyword can be used in following way:
• This means that you must initialize a final variable when it is declared.
• Example:
• It makes a method final, meaning that sub classes can not override this
method.
• The compiler checks and gives an error if you try to override the method.
• Example:
• It makes a class final, meaning that the class can not be inherited by other
classes.
• Example:
• When this is the case, the methods are said to be overloaded, and the
process is referred to as method overloading.
void display(int a)
{
System.out.println("a: " + a);
}
void display(double a)
{
System.out.println("double a: " + a);
}
ob.display();
ob.display(10);
ob.display(123.25);
ob.display(10, 20);
}
}
3.1.7 Method Overloading
• Output:
No parameters
a: 10
double a: 123.25
a and b: 10 20
3.2.1 Constructors
• A constructor is a special kind of method that initializes an object when
created.
• A constructor has the same name as the class and do not have any return
type.
• Therefore, Every class has at least one constructor, which is called default
constructor.
new class_name( );
3.2.2 Default Constructor
class Box
{
int length;
int width;
int height;
Box()
{
System.out.println("Inside Default Constructor");
length = 10;
width = 10;
height = 10;
}
void volume()
{
int vol = length * width * height;
System.out.println("Volume of the Box: " + vol );
}
}
3.2.2 Default Constructor
public class Example
{
public static void main(String[] args)
{
}
}
3.2.3 Parameterized Constructor
• Since the Default constructor gives all the instance variables same value,
it is not very useful.
class Box
{
int length;
int width;
int height;
void volume()
{
int vol = length * width * height;
System.out.println("Volume of the Box: " + vol );
}
}
3.2.3 Parameterized Constructor
}
}
3.2.4 Copy Constructor
Box()
{
System.out.println("Inside Default Constructor");
length = 10;
width = 10;
height = 10;
}
3.2.4 Copy Constructor
Box(Box b)
{
System.out.println("Inside Copy Constructor");
length = b.length;
width = b.width;
height = b.height;
}
void volume()
{
int vol = length * width * height;
System.out.println("Volume of the Box: " + vol );
}
}
3.2.4 Copy Constructor
public class Example
{
public static void main(String[] args)
{
}
}
3.2.5 Passing Object as Parameter
• We can also pass object as a parameter to method in Java programming.
class Test
{
int a;
Test(int i)
{
a = i;
}
Box(Box b)
{
System.out.println("Inside Copy Constructor");
length = b.length;
width = b.width;
height = b.height;
}
void volume()
{
int vol = length * width * height;
System.out.println("Volume of the Box: " + vol );
}
}
3.2.6 Constructor Overloading
}
}
*******