Java Chap 3
Java Chap 3
Introduction
Defining a class
o Adding variables
o Adding methods
Creating objects
Accessing class members
Constructors
Method Overloading
Static members
Nesting of methods
Final methods
Introduction:
Java is an Object-Oriented Language. That means it uses Object as a primary resource of
programming. Objects are seen by the viewer or user, performing tasks assigned by you. Object-
oriented programming aims to implement real-world entities in programming. The main aim of OOP
is to bind together the data and the functions that operate on them so that no other part of the code
can access this data except that function.
Java supports the following fundamental concepts:
Polymorphism Objects
Inheritance Instance
Encapsulation Method
Abstraction Message Passing
Classes
Defining a Class: - A class can be defined as a template/blueprint that describes the behavior/state
that the object of its type supports. Class is also a user-defined data type. So once a the class type
has been defined, we can create “variables” of that type. In Java, these variables are termed as
instances/ objects of classes. The basic form of class definition is:
Syntax:
class classname [extends superclassname]
{
Variable declaration
Method declaration
}
Adding Methods:
A class without methods has no meaning. The objects created by such a class cannot respond to
any messages.
We must have to add methods for manipulating the data contained in the class.
Methods are declared inside the body of the class.
A class can have any number of methods to access the value of various kinds of methods.
Syntax:
return_type methodname(parameter_list)
{
Method-body
}
Method declarations have four basic parts-
o The method name
o Return type
o Parameter List
o Body of the method
Example:
class Rectangle
{
int length, width;
Creating an Object
As mentioned previously, a class provides the blueprints for objects. So basically, an object is created
from a class. In Java, the new keyword is used to create new objects.
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The 'new' keyword is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the
new object.
Following is an example of creating an object:
class Rectangle
{
int length, width;
int Area()
{
int area = length * width;
return (area);
}
int Area()
{
int area = length * width;
return (area);
}
r1.getData(5,7);
int ans = r1.Area();
System.out.println(“Area = ” + ans);
}
}
Constructors:
Definiton: Constructors are special methods which get automatically called whenever an object is
created.
The constructor name and the class name are always same.
class ABC
{
ABC() // Also known as Default constructor
{
// Constructor definition
}
}
Constructors do not have any return type.
It gets implicitly called whenever an object gets created.
ABC s1 = new ABC();
Uses:
o Memory allocation for an object
o Object initialization, i.e. to construct set of values when the object is created.
Constructors can be overloaded. It means a class may contain more than one constructor with
different signatures.
class ABC
{
ABC()
{
// Overloaded Constructor1
}
ABC(int x)
{
// Overloaded Constructor2
}
}
ABC s1 = new ABC();
ABC s2 = new ABC(200);
Method Overloading:
Definition: If a class has multiple methods having same name but different parameters, it is known as
Method Overloading.
Method overloading is used when objects are required to perform similar tasks but using the
different parameters.
Method overloading increases the readability of the program.
When an overloaded method is get called by object, Java matches the exact the signature of the
method and then get executed. This process is called Polymorphism.
The exact method call is decided on the basis of –
i) Number of parameters
ii) Data type of parameters
iii) Sequence of data type of parameters
Method overloading cannot be performed on the basis of “return type” of the method.
Example:
void area(double p1)
void area(double p1, double p2)
Constructors are also the methods. So it can also be overloaded. Such constructors are known as
Overloaded Constructors.
Static Members:
Definition: When the class members can have the access without the instance of the class, that
members are known as static members.
Static members are used when we want to define a member which is common to all. That is these
members belong to the class as a whole rather than for each object separately.
The static keyword can be used with methods, variables, classes (inner/nested).
For example:
static int pi=3.14;
static void Display()
In above example, the variable pi is static. It is common to all objects. Similarly, Display() is static
method. It means this method can be accessed without any object.
The static members are called using class name.
class MyClass
{
static int pi=3.14
public static void Display()
{
System.out.println("Hello");
}
public static void main(String args[])
{
MyClass.Display();
System.out.println(MyClass.pi);
}
}
Nesting of methods:
Definition: A method can be called by using only its name by another method of the same
class that is called Nesting of Methods. Or
When a method is called by another method of the same class, it is known as Nesting of Methods.
A method of a class can be called only by an object of that class using the dot operator.
Java does not support “directly” nested methods. Many functional programming languages
support method within method. But you can achieve nested method functionality in Java.
Example:
class nest System.out.println("Java");
{ void Display() }
{
System.out.print("Hello"); public static void main(String args[])
} {
nest A = new nest();
void Show() A.Show();
{ }
Display(); }
Final methods:
All methods and variables can be overridden by default in subclasses. We can prevent the
subclasses from overriding the members of the super class. Such methods are known as final
methods.
The methods are declared as final by using final keyword.
Once you declare a method final it cannot be overridden. So, you cannot modify a final method
from a sub class.
The main intention of making a method final would be that the content of the method should not
be changed by any outsider.
Example:
final int n=100;
final void Show()
Just like methods, when classes are declared as final, they are known as final classes.
final class Test
*****