Iv. Lesson Proper: Applications Development and Emerging Technologies
Iv. Lesson Proper: Applications Development and Emerging Technologies
Iv. Lesson Proper: Applications Development and Emerging Technologies
Java is an object-oriented programming (OOP) language and adheres to few main concepts of OOP. In this
module, we will check out all the Object Oriented Concepts in Java explained with code and examples.
Object-Oriented Programming is a method of programming where programmers define the type of data as
well the operations that the data can perform.
The core of any programming language is to have method. In C, a method is called function and in
Common Business Oriented Language (COBOL) it is a subroutine. All of them are collection of statements
that are grouped together to perform an operation or specific task. When you call the
System.out.println() method, for example, the system actually executes several statements in order to
display a message on the console.
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
public static int methodName(int a, int b) {
// body
}
Here,
public static − modifier
int − return type
methodName − name of the method
a, b − formal parameters
int a, int b − list of parameters
Method definition consists of a method header and a method body. The same is shown in the
following syntax −
Syntax
modifier returnType nameOfMethod (Parameter List) {
Applications Development and Emerging Technologies Page 2 of 9
What are the building blocks of Object- Oriented Programming?
// method body
}
The syntax shown above includes −
modifier − It defines the access type of the method and it is optional to use.
returnType − Method may return a value.
nameOfMethod − This is the method name. The method signature consists of the method name
and the parameter list.
Parameter List − The list of parameters, it is the type, order, and number of parameters of a
method. These are optional, method may contain zero parameters.
method body − The method body defines what the method does with the statements.
Example
The method called minFunction(). This method takes two parameters num1 and num2 and returns the
maximum between the two −
/** the snippet returns the minimum between two numbers */
return min;
}
Method Calling
For using a method, it should be called. There are two ways in which a method is called i.e.,
method returns a value or returning nothing (no return value).
The process of method calling is simple. When a program invokes a method, the program control gets
transferred to the called method. This called method then returns control to the caller in two conditions,
when −
return min;
}
}
This will produce the following result −
Output
Minimum value = 6
The void Keyword
The void keyword allows us to create methods which do not return a value. Here, in the following
example we're considering a void method methodRankPoints. This method is a void method,
which does not return any value. Call to a void method must be a statement
i.e. methodRankPoints(255.7);. It is a Java statement which ends with a semicolon as shown in
the following example.
Example
Applications Development and Emerging Technologies Page 4 of 9
What are the building blocks of Object- Oriented Programming?
Live Demo
public class ExampleVoid {
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}}
This will produce the following result −
Output
Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
// for integer
public static int minFunction(int n1, int n2) {
Applications Development and Emerging Technologies Page 6 of 9
What are the building blocks of Object- Oriented Programming?
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}}
This will produce the following result −
Output
Minimum Value = 6
Minimum Value = 7.3
Overloading methods makes program readable. Here, two methods are given by the same name
but with different parameters. The minimum number from integer and double types is the result.
Now, we can start discussing clearly Classes and Objects. Are you ready with Object-
oriented programming using Java?
Let do this now.
Example
public class Dog {
String breed;
int age;
String color;
void bark () {
}
void hungry() {
}
void sleep () {
}
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 −
Example
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
If we compile and run the above program, then it will produce the following result −
Output
Passed Name is :tommy
Applications Development and Emerging Technologies Page 9 of 9
What are the building blocks of Object- Oriented Programming?
EXERCISE 1
A student is to create a simple Java program about student enrolment that will classify regular and
irregular students. The student type is either ‘R’ for regular and ‘I’ for Irregular. What could be the name
of the method besides the main() method in the program?
EXERCISE 2
Your school would like to create simple registration system to help students enrolled in the college.
What are the variables to be used in the program?