MODULE -2
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.
Tuesday 26 November 2024 1
MODULE -2
Class Fundamentals The General Form of a Class
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.
Tuesday 26 November 2024 2
MODULE -2
Tuesday 26 November 2024 3
MODULE -2
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:
Tuesday 26 November 2024 4
MODULE -2
Tuesday 26 November 2024 5
MODULE -2
Tuesday 26 November 2024 6
MODULE -2
Tuesday 26 November 2024 7
MODULE -2
Tuesday 26 November 2024 8
MODULE -2
Declaring Objects
As just explained, when you create a class, you are creating a new data type.
Tuesday 26 November 2024 9
MODULE -2
Tuesday 26 November 2024 10
MODULE -2
Tuesday 26 November 2024 11
MODULE -2
Tuesday 26 November 2024 12
MODULE -2
Introducing Methods
• Here, type specifies the type of data returned by the method. This
can be any valid type, including class types that you create.
If the method does not return a value, its return type must be void.
The name of the method is specified by name. This can be any legal
identifier other than those already used by other
items within the current scope.
The parameter-list is a sequence of type and identifier pairs
separated by commas.
Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called. If the method has no
parameters, then the parameter list will be empty.
Tuesday 26 November 2024 13
MODULE -2
Tuesday 26 November 2024 14
MODULE -2
Adding a Method to the Box Class
Tuesday 26 November 2024 15
MODULE -2
Tuesday 26 November 2024 16
MODULE -2
Tuesday 26 November 2024 17
MODULE -2
Returning a Value
Tuesday 26 November 2024 18
MODULE -2
Adding a Method That Takes Parameters
Tuesday 26 November 2024 19
MODULE -2
Adding a Method That Takes Parameters
Tuesday 26 November 2024 20
MODULE -2
Tuesday 26 November 2024 21
MODULE -2
Constructors
A constructor initializes an object immediately upon creation.
It has the same name as the class in which it resides and is syntactically similar to a
method.
Once defined, the constructor is automatically called when the object is created,
before the new operator completes.
Constructors look a little strange because they have no return type, not even void.
This is because the implicit return type of a class’ constructor is the class type itself.
It is the constructor’s job to initialize the internal state of an object so that the code
creating an instance will have a fully initialized, usable object immediately.
Tuesday 26 November 2024 22
MODULE -2
Tuesday 26 November 2024 23
MODULE -2
Tuesday 26 November 2024 24
MODULE -2
Parameterized Constructors
Tuesday 26 November 2024 25
MODULE -2
Parameterized Constructors
Tuesday 26 November 2024 26
MODULE -2
The this Keyword
Sometimes a method will need to refer to the object that invoked it. To allow
this, Java defines the this keyword.
this 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.
You can use this anywhere a reference to an object of the current class’ type is
permitted.
Tuesday 26 November 2024 27
MODULE -2
Tuesday 26 November 2024 28
MODULE -2
Garbage Collection
Since objects are dynamically allocated by using the new operator, you might be wondering how
such objects are destroyed and their memory released for later reallocation.
In some languages, such as traditional C++, dynamically allocated objects must be manually released
by use of a delete operator.
Java takes a different approach; it handles deallocation for you automatically. The technique that
accomplishes this is called garbage collection.
It works like this: when no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object can be reclaimed. There is no need to explicitly
destroy objects.
Tuesday 26 November 2024 29
MODULE -2
A Closer Look at Methods and Classes
Overloading Methods
In Java, it is possible to define two or more methods within the same class that share the
same name, as long as their parameter declarations are different.
When this is the case, the methods are said to be overloaded, and the process is referred
to as method overloading.
Method overloading is one of the ways that Java supports polymorphism.
Tuesday 26 November 2024 30
MODULE -2
When an overloaded method is invoked, Java uses the type and/or number of
arguments as its guide to determine which version of the overloaded method to actually
call.
Thus, overloaded methods must differ in the type and/or number of their parameters.
While overloaded methods may have different return types, the return type alone is
insufficient to distinguish two versions of a method.
When Java encounters a call to an overloaded method, it simply executes the version of
the method whose parameters match the arguments used in the call.
Tuesday 26 November 2024 31
MODULE -2
Tuesday 26 November 2024 32
MODULE -2
Using Objects as Parameters
Tuesday 26 November 2024 33
MODULE -2
Tuesday 26 November 2024 34
MODULE -2
A Closer Look at Argument Passing
call-by-value. call-by-reference.
In general, there are two ways that a computer language can pass an argument to a subroutine.
The first way is call-by-value. This approach copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect
on the argument.
The second way an argument can be passed is call-by-reference. In this approach, a reference to an
argument (not the value of the argument) is passed to the parameter. Inside
the subroutine, this reference is used to access the actual argument specified in the call. This means that
changes made to the parameter will affect the argument used to call the subroutine.
Tuesday 26 November 2024 35
MODULE -2
Tuesday 26 November 2024 36
MODULE -2
Tuesday 26 November 2024 37
MODULE -2
A method can return any type of data, including class types that you create.
Returning Objects For example, in the following program, the incrByTen( ) method returns an
object in which the value of a is ten greater than it is in the invoking object.
Tuesday 26 November 2024 38
MODULE -2
Java supports recursion.
Recursion
Recursion is the process of defining something in terms of itself.
As it relates to Java programming, recursion is the attribute that allows
a method to call itself. A method that calls itself is said to be recursive.
Tuesday 26 November 2024 39
MODULE -2
Introducing Access Control
As you know, encapsulation links data with the code that manipulates it.
However, encapsulation provides another important attribute: access control.
Java’s access modifiers are public, private, and protected.
Tuesday 26 November 2024 40
MODULE -2
Java also defines
a default access level. protected
applies only when inheritance is
involved.
Tuesday 26 November 2024 41
MODULE -2
Understanding static
Tuesday 26 November 2024 42
MODULE -2
Tuesday 26 November 2024 43
MODULE -2
Introducing final
A field can be declared as final.
Doing so prevents its contents from being modified, making it, essentially, a constant.
This means that you must initialize a final field when it is declared.
You can do this in one of two ways:
First, you can give it a value when it is declared.
Second, you can assign it a value within a constructor.
Tuesday 26 November 2024 44
MODULE -2
The first approach is probably the most common. Here is an example:
In addition to fields, both method parameters and local variables can be
declared final.
Declaring a parameter final prevents it from being changed within the
method.
Declaring a local variable final prevents it from being assigned a value
more than once.
The keyword final can also be applied to methods, but its meaning is
substantially different than when it is applied to variables.
Tuesday 26 November 2024 45
MODULE -2
Introducing Nested and Inner Classes
It is possible to define a class within another class; such classes are known as
nested classes.
The scope of a nested class is bounded by the scope of its enclosing class. Thus, if class B is
defined within class A, then B does not exist independently of A.
A nested class has access to the members, including private members, of the class in which
it is nested.
However, the enclosing class does not have access to the members of the nested class. A
nested class that is declared directly within its enclosing class scope is a member of its
enclosing class. It is also possible to declare a nested class that is local to a block.
Tuesday 26 November 2024 46
MODULE -2
There are two types of nested classes: static and non-static.
A static nested class is one that has the static modifier applied.
Because it is static, it must access the non-static members of its enclosing class through
an object. That is, it cannot refer to non-static members of its enclosing class directly.
Because of this restriction, static nested classes are seldom used.
The most important type of nested class is the inner class.
An inner class is a non-static nested class. It has access to all of the variables and
methods of its outer class and may refer to them directly in the same way that other non-
static members of the outer class do.
Tuesday 26 November 2024 47
MODULE -2
Tuesday 26 November 2024 48