0% found this document useful (0 votes)
30 views24 pages

4 Introducing Classes

1) A class defines the common properties and behaviors of objects through variables and methods. An object is an instance of a class. 2) Constructors initialize objects and are called when an object is created. They have the same name as the class. 3) Methods define reusable behaviors through parameters and return values. They can access and modify the object's properties. 4) Encapsulation and access modifiers like public and private control visibility and prevent accidental changes to an object's state.
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)
30 views24 pages

4 Introducing Classes

1) A class defines the common properties and behaviors of objects through variables and methods. An object is an instance of a class. 2) Constructors initialize objects and are called when an object is created. They have the same name as the class. 3) Methods define reusable behaviors through parameters and return values. They can access and modify the object's properties. 4) Encapsulation and access modifiers like public and private control visibility and prevent accidental changes to an object's state.
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/ 24

Introducing classes

What is an Object?

• Real world objects are things that have:


1) state
2) behavior
Example: a dog:
• state – name, color, breed, sits?, barks?, wages tail?,
runs?
• behavior – sitting, barking, waging tail, running
• A software object is a bundle of variables (state) and
methods (operations).
What is a Class?

• A class is a blueprint that defines the variables


and methods common to all objects of a
certain kind.
• Example: ‘your dog’ is a object of the class
Dog.
• An object holds values for the variables
defines in the class.
• An object is called an instance of the Class
Object Creation

• A variable is declared to refer to the objects of


type/class String:
String s;
• The value of s is null; it does not yet refer to any
object.
• A new String object is created in memory with initial
“abc” value:
• String s = new String(“abc”);
• Now s contains the address of this new object.
Object Destruction

• A program accumulates memory through its


execution.
• Two mechanism to free memory that is no
longer need by the program:
1) manual – done in C/C++
2) automatic – done in Java
• In Java, when an object is no longer accessible
through any variable, it is eventually removed
from the memory by the garbage collector.
• Garbage collector is part of the Java Run-Time
Environment.
Class
• A basis for the Java language.
• Each concept we wish to describe in Java must
be included inside a class.
• A class defines a new data type, whose values
are objects.
• A class is a template for objects
• An object is an instance of a class
Class Definition
A class contains a name, several variable declarations
(instance variables) and several method declarations.
All are called members of the class.
General form of a class:
class classname
{
type instance-variable-1;

type instance-variable-n;
type method-name-1(parameter-list) { … }
type method-name-2(parameter-list) { … }

type method-name-m(parameter-list) { … }
}
Assigning Object Reference Variables
Box b1 = new Box();
Box b2 = b1;
• 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.
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
Example: Class Usage
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println ("Volume is " + vol);
} }
Constructor

• A constructor initializes the instance variables of an object.

• It is called immediately after the object is created but before the new

operator completes.

1) it is syntactically similar to a method:

2) it has the same name as the name of its class

3) it is written without return type; the “default” return

type of a class
• When the class has no constructor, the default constructor

automatically initializes all its instance variables with zero.


Example: Constructor

class Box
{
double width;
double height;
double depth;
Box()
{
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume()
{
return width * height * depth;
}
}
Parameterized Constructor

class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
double volume()
{ return width * height * depth;
}
}
Methods

General form of a method definition:


type name(parameter-list) {
… return value;

}
Components:
1) type - type of values returned by the method. If a method
does not return any value, its return type must be void.
2) name is the name of the method
3) parameter-list is a sequence of type-identifier lists
separated by commas
4) return value indicates what value is returned by the
method.
Example: Method

• Classes declare methods to hide their internal data


structures, as well as for their own internal use: Within a
class, we can refer directly to its member variables:
class Box {
double width, height, depth;
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}}
class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
double vol;
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
} } or System.out.println("Volume is " + mybox1.volume());
Parameterized Method

• Parameters increase generality and applicability of a


method:
• 1) method without parameters
int square() { return 10*10; }
• 2) method with parameters
int square(int i) { return i*i; }
• Parameter: a variable receiving value at the time the
method is invoked.
• Argument: a value passed to the method when it is
invoked.
Access Control: Data Hiding and
Encapsulation
• Java provides control over the visibility of variables
and methods.
• Encapsulation, safely sealing data within the capsule
of the class Prevents programmers from relying on
details of class implementation, so you can update
without worry.
• Helps in protecting against accidental or wrong
usage.
• Keeps code elegant and clean (easier to maintain)
Access Modifiers: Public, Private,
Protected

• Public: keyword applied to a class, makes it


available/visible everywhere. Applied to a method or
variable, completely visible.
• Default(No visibility modifier is specified): it behaves
like public in its package and private in other packages.
Access Modifiers: Public,
Private, Protected
• Private fields or methods for a class are only
visible within that class. Private members are
not visible within subclasses, and are not
inherited.
• Protected members of a class are visible
within the class, subclasses and also within all
classes that are in the same package as that
class.
Visibility
class Circle {
private double r;
public Circle (double r) {
this.r = r;
}
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
class circledemo {
public static void main(String ags[])
{Circle c1 = new Circle(1.0d);
System.out.println("Circmference is " +c1.circumference())
}
}
Keyword this

• Can be used by any object to refer to itself in any


class method
• Typically used to
– Avoid variable name collisions
– Pass the receiver as an argument
Keyword this

• Keyword this allows a method to refer to the object


that invoked it.
• It can be used inside any method to refer to the
current object:
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
} //local variable hides the class’s instance variable
Garbage Collection

• Garbage collection is a mechanism to remove objects from


memory when they are no longer needed.
• Garbage collection is carried out by the garbage collector:
1) The garbage collector keeps track of how many references an
object has.
2) It removes an object from memory when it has no longer any
references.
3) Thereafter, the memory occupied by the object can be
allocated again.
4) The garbage collector invokes the finalize method.
finalize() Method

• A constructor helps to initialize an object just after it


has been created.
• In contrast, the finalize method is invoked just before
the object is destroyed, just prior to garbage
collection, not when an object goes out of scope.
(compare with destructor in C++)
• 1) implemented inside a class as:
protected void finalize() { … }
• 2) implemented when the usual way of removing
objects from memory is insufficient, and some
special actions has to be carried out

You might also like