You are on page 1of 12

Chapter Two

Classes & Objects

Classes & Objects:


The java is constructed & based on the OOP ( Object Oriented Programming
) concepts. The java uses object oriented programming environment.
Programming languages:
There are two types:
Initial programming languages that uses machine language or binary language & called
Languages that uses alphabets, numbers & special characters so its called HIGH LEVEL
this type has sets of rules & regulations such as java
UNSTRUCTURED LANGUAGE: this type doesnt has sets of rules & regulations.

What is Object Oriented Programming?


programming language which is based on object or names . Unlike other examples of computer
programming language, object oriented programming focuses on the use of objects instead of
actions in order to carry out tasks. This use of objects to design applications instead of
actions also involves taking an approach that is more mindful of data and less concerned with
logic, which is more commonly the case in other paradigms.

This different approach taken by object oriented programming means that

the view of objects and actions is reversed. The emphasis is on the objects themselves rather than
on the execution of tasks that employ the objects. In that manner, the structure of object
oriented programming does not consider deciding on how to employ the logic, but on the
definition of the data that will be used in the programming.


Designing computer programs with the approach of OBJECT ORIENTED


1- Defining the objects that are to be manipulated by the program. Once the objects are
identified, the programmer will begin to
2- Identify the relationship between each object or Operations or calculations.
Essentially, the programmer is seeking to place the objects into a
classification, thus helping to define the data that is part of the inheritance brought to the task
by each object. In fact, the process of defining these classes and subclasses of data is normally
called inheritance.

DEFINITION: Object is a tangible and visible thing or entity. An object
can be considered a "thing" that can perform a set of activities. Real-world objects share two
characteristics: they all have STATE & BEHAVIOR .Objects are key to understanding objectoriented technology.
Software objects are modelled after real-world objects in that they too
have state and behavior. A software object maintains its STATE in variables and
implements its behavior with METHODS.
These real-world objects share two characteristics: they all have state and they all
have behavior. For example, dogs have state (name, color, breed, hungry) and dogs have behavior
(barking, fetching, and slobbering on your newly cleaned slacks). Bicycles have state (current gear,
current pedal cadence, two wheels, number of gears) and behavior (braking, accelerating, slowing
down, changing gears).


Classes in Java

A class is a blueprint from which individual objects are created. A class can have any number of
methods to access the value of various kinds of methods.
This class describes the data properties and behaviours alone. In
programming language, CLASS describes both the PROPERTIES (DATA) and BEHAVIOURS
(FUNCTIONS) of objects. Classes are not objects, but they are used to instantiate objects.
FEATURES OF CLASS: Classes contain data known as members and member
functions. As a unit, the collection of members and member functions is an object.
Therefore, this unit of objects make up a class.

In Java is a mechanism of wrapping the data (variables) and code acting on the data (methods)
together as a single unit. In encapsulation, the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their current class. Therefore, it is also
known as data hiding. { }

Advantages of encapsulation:
1. It improves maintainability and flexibility.
2. The fields can be made read-only.
3. User code would not know what is going on behind the scene. Encapsulation is
also known as data Hiding.

DEFINITION: Encapsulation is the process of combining data and
functions into a single unit called class. Using the method of encapsulation, the
programmer cannot directly access the data. Data is only accessible through the functions present inside
the class. Data encapsulation led to the important concept of data hiding. Data hiding is the
implementation details of a class that are hidden from the user. The concept of restricted access led
programmers to write specialized functions or methods for performing the operations on hidden members
of the class. Attention must be paid to ensure that the class is designed properly.


The Benefits of Encapsulation:

Encapsulating related variables and methods into a neat software bundle is a simple yet powerful idea that
provides two primary benefits to software developers:

Modularity--The source code for an object can be written and maintained independently of the source
code for other objects. Also, an object can be easily passed around in the system. You can give your
bicycle to someone else and it will still work.

Object-Oriented Programming (OOP) is a type of programming that

uses "objects" or names, commands & data to create programmes.

Object-oriented programming (OOP) is a programming paradigm based on the concept of
"objects", which may contain data, in the form of fields, often known as attributes; and code, in the
form of procedures, often known as methods.

This concept is very important when dealing with object-oriented programming languages such
as Java, this involves dividing the various codes that we have into separate classes & methods
that in order to maximize code separation in to other classes and methods and minimize coding to
be only in a single unit.

Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data
for the object that is why it is known as constructor.


Example Programme:
class OPT

public static void main(String args[])
Box M1 = new Box();
Box M2 = new Box();
double ADD;
M1.width = 10;
M1.height = 20;
M1.depth = 15;
M2.width = 3;
M2.height = 6;
M2.depth = 9;
ADD = M1.volume();
System.out.println("Volume is " + ADD);
ADD = M2.volume();
System.out.println("Volume is " + ADD);
class Box
double width;
double height;
double depth;
double volume()
return width * height * depth;


programmers to define their own functions. So Developers often need the ability to break
programs up into smaller chunks (parts) that are easier to develop & follow in the case of the
large programmes. Functions help us to classify our programmes to the necessary parts.
"Real world" programs can be many of thousands (or millions!) of lines long. Without this
ability to divide the program into parts, developing such large programs would quickly become
Always programmers to divide their code up into chunks (parts) known as functions. A function
with a simple description and a well-defined interface to the outside world can be written and
debugged without worrying about the code that surrounds it.

Types of functions:
1. Void function (function that returns no value and sends no value).
2. Function that receives values from main function (also called Passing
arguments by reference).
3. Function that returns value to main function (function that passes a parameter).

A function: which can also be referred to as subroutine, procedure, subprogram or even

method, carries out tasks defined by a sequence of statements called a statement block that
need only be written once and called by a program as many times as needed to carry out the
same task.


is a another part of code written out side the main function thus creating a
subprogram, the main function will be the first to work then the functions will work next , the
functions has t be called by the main programme so then it can work.
Functions may depend on variables passed to them, called arguments or parameters, and may pass
results of a task on to the main function of the function; this is called the return value.
It is important to note that a function that exists in the global scope can also be called global function
and a function that is defined inside a class is called a member function. (The term method is
commonly used in other programming languages to refer to things like functions, but this can lead to
dealing with java which supports functions.)


Calling function:
Calling function means starting the function or making the function start or work. Each function
call is responsible for its own function code that has the same name.

Defining function prototypes:

The content of the function is called the body of the function. The word void is a
keyword. Java keywords are reserved words, so cannot be used for any purpose other than
what they are meant for. On the other hand it means that the function will return no value or
the main function will return no value.
A function may be defined anywhere in the module or source code or programme. (A module
is another name for a source file.) However, something has to tell main() the full name of
the function before it can be called. That means we have to write the full code &
statements of the function so that the function can be called & used by the
main function. Consider the following code example:

The general form of a function definition is as follows:

function-type function-name ( parameter-list )

local variables definitions;
function-implementation or function statements of code;


1. If the function returns a value then the type of that value must be specified in functiontype. For the moment this could be int, float or char.
If the function does not return a value then the function-type must be void.
The function-name follows the same rules of composition as identifiers or variable.
The parameter-list lists the formal parameters of the function together with their types.
The local-definitions are definitions of variables that are used in the functionimplementation or function code. These variables have no meaning outside the function.
6. The function-implementation consists of executable statements that implement the
effect of the function.


Programme Example:
import java.util.Scanner;

public class AC
public static void main(String[] args)
Scanner input= new Scanner(;
int C;
System.out.println("1- Addittion");
System.out.println("Enter the Option : ");
case 1: ADD();
case 2: MUL();
case 3: DIV();
case 4: SUB();
default: System.out.println("Invalid Option select (1-4)");

public static void SUB()

Scanner input= new Scanner(;
double X,Y,Z;
System.out.println("Enter number : ");
System.out.println("Enter number : ");
System.out.println("Subtraction: " +Z);
public static void DIV()
Scanner input= new Scanner(;


double X,Y,Z;
System.out.println("Enter number : ");
System.out.println("Enter number : ");
System.out.println("Division: " +Z);
public static void MUL()
Scanner input= new Scanner(;
double X,Y,Z;
System.out.println("Enter number : ");
System.out.println("Enter number : ");
System.out.println("Multiplication: " +Z);
public static void ADD()
Scanner input= new Scanner(;
double X,Y,Z;
System.out.println("Enter number : ");
System.out.println("Enter number : ");
System.out.println("Addittion: " +Z);



An array is a group of like-typed variables that are referred to by a common name.
Arrays of any type can be created and may have one or more dimensions. A specific element in an
array is accessed by its index. Arrays offer a convenient means of grouping related information.

One-Dimensional Arrays
A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first
must create an array variable of the desired type. The general form of a one-dimensional
array declaration is

// Demonstrate a one-dimensional array.

class Array
public static void main(String args[])
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");

Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look and act like regular
multidimensional arrays.



Garbage Collection:
The garbage collector is a program which runs on the Java Virtual Machine which
gets rid of objects which are not being used by a Java application anymore. It is a
form of automatic memory management.